Friday, May 16, 2014

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() 
{
  Serial.begin(9600);
}
void loop() 
{
  int chk = DHT11.read(DHT11PIN);
  //read temperature in celsius degree and print in the serial port
  Serial.print((float)DHT11.humidity, 2);
  Serial.print(",");
  //read humidity and print in the serial port
  Serial.print((float)DHT11.temperature, 2);
  Serial.println();
  delay(2000);
}

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)(Math.Round(Convert.ToDecimal(dataTempHumid[0]),0));
    int Temperature = 
    (int)(Math.Round(Convert.ToDecimal(dataTempHumid[1]),0));
    //draw temperature in the graphic
    drawTemperature(Temperature);
    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");
     serialPort1.Open();
}

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

29 comments:

  1. Great works, im using vb.net and i tried to follow your steps but im getting error, can you point my errors?

    ReplyDelete
  2. Great contribution!! Hey can you send your c# code to my email:
    jorge.cervantes.gtz@gmail.com
    I´ll be very thankful.

    ReplyDelete
  3. So awesome! Can you send me your C# code, please?
    My email: hpthinh28292@gmail.com
    Thank you very much!

    ReplyDelete
  4. Hi...could you please send me your gauge and needle image?
    my email : isuljug@gmail.com
    Thank you very much!

    ReplyDelete
  5. The DHT11 is a basic, ultra low cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermostat to measure ... temphumiditymeter.blogspot.com

    ReplyDelete
  6. Hello! I am using this kind of sensor for my diploma project and i would really appreciate if you could send me the C# project because the part with Arduino is already taken care of, Thanks. My email address is ursucristina1991@gmail.com

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I have project using Arduino Uno.
    If you could share and send me the C# project, i really appreciate it.
    Thanks in advance. Email me at jjomar1994@gmail.com

    ReplyDelete
  10. i understand arduino code but how i use visual studio code, were adding, need some program were run code or what need doing ???????

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Hi. What about the coding for visual basic 2012? Is it the Same? Can you share?

    ReplyDelete
  13. Hi. What about the coding for visual basic 2012? Is it the Same? Can you share?

    ReplyDelete
  14. great piece of work....can you send me the c# code please at bassi@live.co.uk

    ReplyDelete
  15. how can load and use gauge software my computer ? step py step help how adding software i no understand, arduino ino i understand but no C# program. i use before test DHT11 arduino and processing software and working good number come processing windows and show numbers temp+hum. but thats new and different need test how use gauge.

    ReplyDelete
  16. Hey, great stuff! Could you send me the c# code at djhelo@o2.pl ?
    Thanks!

    ReplyDelete
  17. Do you know the arduino herhab and C # to send a case directly to the finished database

    erdalalcan59@gmail.com

    ReplyDelete
  18. Wow awesome! If you don't mind can you send me your C# code, please?
    My email: dykwon@mirae.com
    Thank you very much!

    ReplyDelete
  19. can i get a zip faile please ?

    ReplyDelete
  20. hlasoy angel quiñonez y estoy haciendo un royecto parecido me puede emviar el proyecto completo hay para corregir errores

    ReplyDelete
  21. I have project using Arduino Uno.
    If you could share and send me the C# project, i really appreciate it.
    Thanks in advance. Email me at guilhermelourenco2005@gmail.com

    ReplyDelete
  22. That´s awesome
    Please could you send me the code for C# to my mail "mfnavarrete3@gmail.com"
    Thanks

    ReplyDelete
  23. sorry where can find the DHT11 library from?

    ReplyDelete
  24. Hi...could you please send me your gauge and needle image?
    my email : ahmedhassan19481@gmail.com
    Thank you very much!

    ReplyDelete
  25. Hi...could you please send me your gauge and needle image?
    my email : ahmedhassan19481@gmail.com
    Thank you very much!



    ReplyDelete
  26. hi. thanks for the project but can you give me gauge library? i need this? thanks. mail:berkinabanozlu@gmail.com

    ReplyDelete
  27. HI,跟您要個C#的CODE,謝謝您。 ^__^ hudog0215@gmail.com

    ReplyDelete