Temperature and Humidity Sensor DHT11 in Arduino with Java and Jfreechart

Temperature and Humidity Sensor DHT11 in Arduino with Java and JfreeChart

1. Problem Statement

In this tutorial we will learn how use temperature and humidity sensor from the environment using DHT11 in Arduino and process the information in Java via serial Port. We will show the temperature in Celsius degree and Humidity in percentage using JFreeChart open source library.

2. JFreeChart

JFreeChart is an open-source framework library for the programming language Java, which allows the creation of a wide variety of both interactive and non-interactive charts. A free Java chart library. JFreeChart supports pie charts (2D and 3D), bar charts (horizontal and vertical, regular and stacked), line charts, scatter plots, time series charts, high-low-open-close charts, candlestick plots, Gantt charts, combined plots, thermometers, dials and more. JFreeChart can be used in applications, applets, servlets, JSP and JSF. You can download from the official web site jfreechart

3. Temperature and Humidity Sensor DHT11

The DHT11 is a basic, ultra low cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermostat to measure the surrounding air, and spits out a digital signal on the data pin. It's fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds. In the market you will find many models, some of them have 3 or 4 pins, so be carefully and read the technical specifications.
The technical specification of DHT11 are:
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm
  • 3 pins with 0.1" spacing
Temperature Sensor DHT11

4. Sketch for this project

For implement this project we will required the following materials:
  • DHT11 temperature and humidity sensor
  • Wires
  • Arduino UNO R3
The next picture show the sketch for this project
Arduino Sketch DHT11
In the next figure we can see the first pin is connected to pin digital 2 in arduino board, the middle one to 5v and the last one is connected to GND.
Arduino Temperature DHT11

5. Arduino Source Code to get Temperature and Humidity from DHT11

The source code in arduino is not so complicated, as we can see first you need to add the library dht11.h to start to use DHT11 temperature and humidity sensor and then define the digital pin you will use to read the information from the sensor and finally read the information from dht11 sensor (temperature and humidity) and print in the serial port for further reading from C#.
#include <dht11.h>
//Declare objects
dht11 DHT11;
//Pin Numbers
#define DHT11PIN 2

void setup()

void loop()
int chk = DHT11.read(DHT11PIN);
//read temperature in celsius degree and print in the serial port
//read humidity and print in the serial port

6. Designing Project in Java to Show Temperature and Humidity using JFreeChart

First create a java project in Netbeans, in your project go to Libraries folder, right click choose Add JAR/Folder and add the following libraries. Please replace the version numbers with the version you are using.
  • jcommon-1.0.21.jar
  • jfreechart-1.0.17.jar
Add a JFrame to your project and Design and interface like shown in the next figure
Arduino JfreeChart Temperature DHT11
Java Source Code
In this part we need to create a Class for manage serial Port in Java, for this you can see in this link the java class provide by arduino web site, where also you find the explanation, I modified a little bit that class according to what I want to do, the source code of my class is the following.
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;

* @author george serrano, develop for http://georgehk.blogspot.com
public class SerialPortCommunication {

public SerialPort serialPort;
//The port we're normally going to use.
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM4", // Windows
//input and output Stream
private BufferedReader input;
private OutputStream output;
//Milliseconds to block while waiting for port open
public static final int TIME_OUT = 1000;
//Default bits per second for COM port.
public static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier)
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
if (portId == null) {
System.out.println("Could not find COM port.");
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
// set port parameters
// open the streams for reading and writing in the serial port
input = new BufferedReader(new
output = serialPort.getOutputStream();
} catch (Exception e) {
public void writeData(String data) {
try {
//write data in the serial port
} catch (Exception e) {
System.out.println("could not write to port");

public String getData() {
String data = null;
try {
//read data from the serial port
data = input.readLine();
} catch (Exception e) {
return e.getMessage();
return data;

Also we need to instantiate an object of this class in JFrame's constructor
public frmTemperatureArduinoUNO() {

//initialize serial Port
serialPortCommunication = new SerialPortCommunication();

The last steep is implement the source code for Get Temperature Humidity Button

private void jbtnShowTemperatureHumidityActionPerformed
(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//set temperature in 0
//set humidity in 0
try {
int temperature, humidity;
//get temperature and humidity from serial port
//sent by arduino and save in temporal string
String temperatureHumidityArduino =
//split temperature and humidity
//divided by commas and save in array
String[] temperatureHumidity =
//get temperature from array and convert to integer
temperature = Integer.parseInt(temperatureHumidity[0]);
//get humidity from array and convert to integer
humidity = Integer.parseInt(temperatureHumidity[1]);
//show temperature and humidity in JTextBoxes
jtxtTemperature.setText(String.valueOf(temperature) + " °C");
jtxtHumidity.setText(String.valueOf(humidity) + " %");

//draw temperature
drawDataSetJfreeChart(temperature, datasetTemperature,
//draw humidity
drawDataSetJfreeChart(humidity, datasetHumidity,
} catch (Exception e) {

Once you run the project it will show the next window, where we can see two thermometers, one of them for showing the temperature and another to show humidity, also two extra JSliders to probe the right measurement of those thermometers, and finally two JTextBoxes for showing the temperature and humidity in numbers.

7. Running Project

Before run the project make sure the Arduino in writing the temperature and humidity to serial port. Just press F6 to run the project and you will see the program start to looking for serial port open. After press Get Temperature Humidity Button, our program will read temperature and humidity from serial port and draw them in thermometer and humidity chart like depicted in the next figure.

video tutorial in youtube

No comments :