A few months ago i’ve presented a workshop about Arduino and decided to build a simple meteorological station. The station measure the temperature and humidity level, showing the values on a LCD and on a visual indicator. This indicator as three leds: one blue, one green and one red.
If the temperature drops below 20ºC the blue led was turned on. If the temperatur value was between 20ºC and 30ºC the green turn on. Above 30ºC the red turn on.
Material:
To build this project was necessary:
- One breadboard
- One Arduino Nano
- One LCD 1602
- One DHT22 Sensor
- One Blue led
- One Red led
- One Green led
- One 10kohm potentiometer
- Jumper Cables
Code:
To use the DHT22 and the LCD we use the “DHT.h” and “LiquidCrystal.h” libraries. On the “LiquidCrystal.h” is necessary to define what Arduino connectors the LCD is going to use.
On the “DHT.h”, when is initialised, is necessary chosse what sensor we are using. The library supports multiple sensors.
/* * Tiago Santos * dark_storm@groundzero.com.pt * https://www.themakersstuff.com */ #include "DHT.h" #include "LiquidCrystal.h" #define DHTPIN 6 #define DHTTYPE DHT22 #define blue 8 #define green 9 #define red 10 DHT dht(DHTPIN, DHTTYPE); LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Em seguida, iniciamos o setup, onde definimos que os pinos dos led’s são de saída, iniciamos o dht22 e definimos que o LCD tem 16 colunas por 2 linhas.
void setup() { Serial.begin(9600); pinMode(blue,OUTPUT); pinMode(green, OUTPUT); pinMode(red,OUTPUT); dht.begin(); lcd.begin(16, 2); }
On the Setup section, is defined that the led connectors are on OUTPUT mode, what is the type of the temperature sensor and that the LCD has 16 columns by 2 lines.
void setup() { Serial.begin(9600); pinMode(blue,OUTPUT); pinMode(green, OUTPUT); pinMode(red,OUTPUT); dht.begin(); lcd.begin(16, 2); }
On the program section, the humidity and temperature values are readed and showed on the LCD. the humidity value is showed in line 0 and the temperature in line 1. After that is defined what led to turn on.
void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t) ){ Serial.println("Failed to read from DHT sensor!"); return; } lcd.setCursor(0, 0); lcd.print("Humid.: "); lcd.print(h); lcd.print(" %"); lcd.setCursor(0, 1); lcd.print("Temp. : "); lcd.print(t); lcd.print(" C"); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.println(" *C "); if( t < 20 ) { digitalWrite(green, LOW); digitalWrite(red, LOW); digitalWrite(blue, HIGH); } else { if(t > 30) { digitalWrite(green, LOW); digitalWrite(blue, LOW); digitalWrite(red, HIGH); } else { digitalWrite(blue, LOW); digitalWrite(red, LOW); digitalWrite(green, HIGH); } } }
This code is also available on my GitHub Account ( https://github.com/d4rks70rm/Arduino-DHT22-Sensor-with-LCD ).
Comments are closed.