Arduino Weather Station using DHT11
- Nick Koumaris
- http://educ8s.tv/
- info@educ8s.tv
- 22.460 Views
- moderate
- Tested
Introduction
Using a display to view the temperature and humidity of your environment can be possible using the DHT11 or DHT22 sensor with the easy to use Arduino microcontroller platform and that’s the goal of this project. For this project, we will be using the 16×2 LCD display module to display the temperature and humidity readings gathered from the environment using the DHT11 temperature and humidity sensor.
Project Parts
The components/parts needed for this tutorial are:
- DHT11 Temperature and humidity sensor
- 16×2 LCD keypad shield
- Arduino Mega
- Breadboard
- Jumper wires
- Resistors
The DHT11 is a low cost temperature and humidity sensor that operates using the 1-wire protocol, costs about $2.
This sensor has three pins, the first pin is the power pin VCC, the middle pin is the signal out or DATA pin and the last pin is GND.
Although the DHT11 temperature and humidity sensor isn’t the fastest temperature and humidity sensor around, it has fair level of accuracy, +-5% for humidity readings and +-2% for temperature readings. With the DHT we will be able to measure temperature and humidity of the environment with a very fair degree of accuracy.
The LCD Keypad shield makes connecting the 16×2 LCD module to any system quite easy as it simply just plugs on the Arduino mega which is being used for this project.
An ordinary 16×2 LCD module (not shield) can also be used. Just ensure the pins are connected as described in the code and schematic below.
Schematic
Connect the LCD and the DHT11 sensor as shown in the image below.
Putting the component together is very easy if using the LCD keypad shield. All you just need to do is plug the LCD in as shown below and connect the DHT as described in the schematics above.
For proper understanding of the connections, here is a breakdown of all the components connected.
LCD Connection
LCD - Arduino pin 1(VSS) - GND pin 2(VDD) - 5V PIN 3(Vo) - Potentiometer middle pin pin 4(RS) - D8 PIN 5(RW) - GND PIN 6(E) - D9 PIN 7-10 - GND OR FLOAT PIN 11 - D4 PIN 12 - D5 PIN 13 - D6 PIN 14 - D7 PIN 15 - 5V PIN 16 - GND
Note that the RW pin is connected to GND because we will be writing only to the LCD and not to read from it, for this to be possible the RW pin has to be pulled LOW
DHT11 Connection
Pin connection of the dht11 to the arduino is as illustrated below. All DHT11 sensors have three main functional pins. The DHT types with four pins always have a void pin which is never connected to anything.
DHT11 - Arduino VCC - 5V DATA - D22 GND - GND
with the schematics handled, lets move to the code.
Code
Before we start, we have to download DHT library and set its type. The required library is available on github. Download it and extract it into Arduino libraries folder, then open Arduino IDE. Its probably important to note at this point that the library will not be visible to an Arduino IDE instance that was already running before installation, you have to restart the Arduino IDE after installing the library.
With our library installed, we can move to the code analysis:
The first thing to be done is to include all the dependencies the code needs to run fine, libraries in this case.
//written by Nick Koumaris //info@educ8s.tv //educ8s.tv #include "DHT.h" #include <LiquidCrystal.h>
Next we define the pin the DHT11 DATA pin is connected to and also include the type of the DHT sensor we are using which is DHT11.
#define DHTPIN 22 // what pin we're connected to #define DHTTYPE DHT11
On line 4 we create a DHT object and then pass in the pin number (DHTPIN) and the sensor type (DHTTYPE).
DHT dht(DHTPIN, DHTTYPE);
With the next line, we create an LCD object, passing in the Arduino pin numbers to which our LCD pins are connected as follows in the format lcd (RS, E, D4, D5, D6, D7).
LiquidCrystal lcd(8,9,4,5,6,7);
with this done, we are ready to move into the setup function.
In the setup function, we call the LCD begin method, passing in the LCD size which is a 16×2. Next, we print a message to indicate the device has commenced reading data from the sensor on the first line of the LCD then call the DHT begin method.
void setup(void) { lcd.begin(16, 2); lcd.print("Reading sensor"); dht.begin(); }
That’s all for the setup() function.
Moving on to the loop() function, we create two variables of type float which will hold the temperature and humidity value, give it a delay of two seconds after reading values into them and then clear the LCD.
void loop() { float temperature, humidity; humidity = dht.readHumidity(); temperature = dht.readTemperature(); delay(2000); lcd.clear();
Next we create two character arrays both of size six and then we use the dtostrf function to convert our temperature and humidity value from type float to string and then we print it on the LCD. Note that the explicit typecasting used in the lcd.print() function ((char)223) is used to print the degree symbol on the display.
char tempF[6]; char humF[6]; dtostrf(temperature, 5, 1, tempF); dtostrf(humidity, 2, 0, humF); lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C "); lcd.print("H: "); lcd.print(humF); lcd.print("%"); }
That’s it for the loop and the end of the code.
so here is the complete code:
//written by Nick Koumaris //info@educ8s.tv //educ8s.tv #include "DHT.h" #include <LiquidCrystal.h> #define DHTPIN 22 // what pin we're connected to #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); LiquidCrystal lcd(8,9,4,5,6,7); void setup(void) { lcd.begin(16, 2); lcd.print("Reading sensor"); dht.begin(); } void loop() { float temperature, humidity; humidity = dht.readHumidity(); temperature = dht.readTemperature(); delay(2000); lcd.clear(); char tempF[6]; char humF[6]; dtostrf(temperature, 5, 1, tempF); dtostrf(humidity, 2, 0, humF); lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C "); lcd.print("H: "); lcd.print(humF); lcd.print("%"); }
Save your code, connect your Mega to your computer and make sure under your tools menu, the board picked is “Arduino/Genuino Mega or Mega 2560” and also ensure the right COM port is selected. Click upload when done and you should have something like the Image below.
That’s it guys, if you have any issue leave it in the comment section.
Visit here to download the code for this tutorial.
You can also watch the video tutorial on this topic on youtube.
Does it tell the rain percent?
No, it is only showing temperature and humidity levels.
If we adding a rain sensor in this project so what was the code for that
When is it published
This is published in Oct 2017, thanks for visiting.
Can you Link the Library
The sensor library is linked on the article, under Code section. The LCD library is already available on Arduino IDE.