Temperature and Humidity Sensor DHT11 in Arduino with C#

Temperature and Humidity Sensor DHT11 in Arduino with C#

1. Problem Statement

In this tutorial we will learn how use sensor to get temperature and humidity from the environment using DHT11 Sensor in Arduino and process the information in C# via the serial Port. We will show the temperature in Celsius and Humidity.

2. 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

3. 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

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.

4. 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
Serial.print((float)DHT11.humidity, 2);
//read humidity and print in the serial port
Serial.print((float)DHT11.temperature, 2);

5. Designing Project in C# to Show the Temperature and Humidity

To implement this project using C# we require to draw a gauge control and a needle, in my case I draw them using photoshop for displaying temperature and put the gauge picture in a PictureBox control and for movement of the needle we will make programmatically. Also we need a couple of TextBox to show the temperature, humidity and time. Every 10 seconds we will update the temperature and humidity so we need to use Timer controls and finally we will need a SerialPort control to read the information from the serial port. The design of the interface is shown in the next figure.

C# Source Code

As we can see this is the main part of the c# source code for this project, we split the information we get from arduino separate by commas into temperature and humidity and then draw that temperature in the gauge control.
private void timer1_Tick(object sender, EventArgs e)
//read temperature and humidity every 10 seconds
timer1.Interval = 10000;
//read information from the serial port
String dataFromArduino = serialPort1.ReadLine().ToString();
//separete temperature and humidity and save in array
String[] dataTempHumid = dataFromArduino.Split(',');
//get temperature and humidity
int Humidity =
int Temperature =
//draw temperature in the graphic
txtHumidity.Text = Humidity.ToString()+" %";
txtTemperatureCelsius.Text = Temperature.ToString()+" °C";
private void timer2_Tick(object sender, EventArgs e)
txtTime.Text = DateTime.Now.ToLongTimeString();
private void frmTemperatureHumidity_Load(object sender, EventArgs e)
needle = Image.FromFile("needle2.png");

6. Running Project

Just press F5 to run the project and you will see the program start to read from the serial port and show in the gauge control, and every 10 seconds it will update with the new information getting from the DHT11 sensor.

you could see all the implementation in youtube here
Admin Semoga Informasi Yang Diberikan Melalui Web Ini Dapat Bermanfaat Bagi Saudara/i

No comments for "Temperature and Humidity Sensor DHT11 in Arduino with C#"