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
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
Arduino is awesome ;)
ReplyDeleteGreat works, im using vb.net and i tried to follow your steps but im getting error, can you point my errors?
ReplyDeleteGreat contribution!! Hey can you send your c# code to my email:
ReplyDeletejorge.cervantes.gtz@gmail.com
I´ll be very thankful.
So awesome! Can you send me your C# code, please?
ReplyDeleteMy email: hpthinh28292@gmail.com
Thank you very much!
Hi...could you please send me your gauge and needle image?
ReplyDeletemy email : isuljug@gmail.com
Thank you very much!
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
ReplyDeleteHello! 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
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI have project using Arduino Uno.
ReplyDeleteIf you could share and send me the C# project, i really appreciate it.
Thanks in advance. Email me at jjomar1994@gmail.com
i understand arduino code but how i use visual studio code, were adding, need some program were run code or what need doing ???????
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi. What about the coding for visual basic 2012? Is it the Same? Can you share?
ReplyDeleteHi. What about the coding for visual basic 2012? Is it the Same? Can you share?
ReplyDeletegreat piece of work....can you send me the c# code please at bassi@live.co.uk
ReplyDeletehow 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.
ReplyDeleteHey, great stuff! Could you send me the c# code at djhelo@o2.pl ?
ReplyDeleteThanks!
Do you know the arduino herhab and C # to send a case directly to the finished database
ReplyDeleteerdalalcan59@gmail.com
Wow awesome! If you don't mind can you send me your C# code, please?
ReplyDeleteMy email: dykwon@mirae.com
Thank you very much!
can i get a zip faile please ?
ReplyDeletehlasoy angel quiñonez y estoy haciendo un royecto parecido me puede emviar el proyecto completo hay para corregir errores
ReplyDeleteI have project using Arduino Uno.
ReplyDeleteIf you could share and send me the C# project, i really appreciate it.
Thanks in advance. Email me at guilhermelourenco2005@gmail.com
That´s awesome
ReplyDeletePlease could you send me the code for C# to my mail "mfnavarrete3@gmail.com"
Thanks
sorry where can find the DHT11 library from?
ReplyDeleteNice blog...
ReplyDeleteThanks for sharing...
ph controller, Humidity meter, Controller, Digital counter
Hi...could you please send me your gauge and needle image?
ReplyDeletemy email : ahmedhassan19481@gmail.com
Thank you very much!
Hi...could you please send me your gauge and needle image?
ReplyDeletemy email : ahmedhassan19481@gmail.com
Thank you very much!
hi. thanks for the project but can you give me gauge library? i need this? thanks. mail:berkinabanozlu@gmail.com
ReplyDeleteHI,跟您要個C#的CODE,謝謝您。 ^__^ hudog0215@gmail.com
ReplyDelete