Advertisement
learnelectronics

DHT11 Grove Sensor and LCD

Mar 9th, 2019
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. //add DHT sensor library
  2. #include <DHT.h>
  3. //add LCD library
  4. #include <rgb_lcd.h>
  5.  
  6. //set digital pin2 as DHTPIN
  7. #define DHTPIN 2
  8. //set the sensor type as DHT 11
  9. #define DHTTYPE DHT11
  10.  
  11. /*assign dht as the name of DHT sensor
  12.   set the sensor pin as DHTPIN(pin2),
  13.   set the sensor type as DHTTYPE(DHT11)
  14. */
  15. DHT dht(DHTPIN, DHTTYPE);
  16.  
  17. //assign lcd as the name of rgb_lcd screen
  18. rgb_lcd lcd;
  19.  
  20. void setup() {
  21.   //initialise the dht sensor
  22.   dht.begin();
  23.   //initialise the lcd screen;
  24.   //set up the lcd's number of columns and rows:
  25.   lcd.begin(16, 2);
  26.   //wait for 2s
  27.   delay(2000);
  28. }
  29.  
  30. void loop() {
  31.   //store the humidity value to h
  32.   int h = dht.readHumidity();
  33.   //store the temperature value to t(in Celsius)
  34.   int t = dht.readTemperature();
  35.  
  36.   //set the LCD cursor to column 0, line 0
  37.   lcd.setCursor(0, 0);
  38.   //Print text temperature: to the LCD
  39.   lcd.print("Temperature:");
  40.   //set the LCD cursor to column 12, line 0
  41.   lcd.setCursor(12, 0);
  42.   //Print temperature value t to the LCD
  43.   lcd.print(t);
  44.   //set the LCD cursor to column 14, line 0
  45.   lcd.setCursor(14, 0);
  46.   //Print temperature ยบ is character 223 on lookup table
  47.   lcd.write(223);
  48.   //Print C to the LCD
  49.   lcd.print("C");
  50.  
  51.   //set the LCD cursor to column 0, line 1
  52.   lcd.setCursor(0, 1);
  53.   //Print text Humidity: to the LCD
  54.   lcd.print("Humidity: ");
  55.   //set the LCD cursor to column 10, line 1
  56.   lcd.setCursor(10, 1);
  57.   //Print humidity value h to the LCD
  58.   lcd.print(h);
  59.   //set the LCD cursor to column 12, line 1
  60.   lcd.setCursor(12, 1);
  61.   //Print sign % to the LCD
  62.   lcd.print("%");
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement