Last active
August 29, 2015 14:26
-
-
Save lvidarte/899f4f8133af40bc5284 to your computer and use it in GitHub Desktop.
Arduino DHT21 Temperature & Humidity Sensor on LCD 2x16 display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <LiquidCrystal.h> | |
#include <DHT.h> | |
#define DHT_PIN 5 | |
#define DHT_TYPE DHT21 | |
#define LCD_LINE_LENGTH 17 | |
// Initialize library with the numbers of the interface pins: | |
// (RS, Enable, DB4, DB5, DB6, DB7) | |
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); | |
DHT dht(DHT_PIN, DHT_TYPE); | |
void setup() | |
{ | |
lcd.begin(16, 2); | |
dht.begin(); | |
} | |
void lcdPrintLine(char *buffer, byte lineNumber) | |
{ | |
lcd.setCursor(0, lineNumber); | |
byte i = 0; | |
while (i < LCD_LINE_LENGTH && buffer[i] != '\0') | |
{ | |
lcd.write(buffer[i]); | |
i++; | |
} | |
} | |
void loop() | |
{ | |
static char buffer[LCD_LINE_LENGTH]; | |
float t = dht.readTemperature(); | |
float h = dht.readHumidity(); | |
// Check if any reads failed | |
if (isnan(t) || isnan(h)) | |
{ | |
return; | |
} | |
lcd.clear(); | |
int tInt = (int) t; | |
int tDec = (int) (t * 10) % 10; | |
sprintf(buffer, "Temp %d.%dC", tInt, tDec); | |
lcdPrintLine(buffer, 0); | |
int hInt = (int) h; | |
int hDec = (int) (h * 10) % 10; | |
sprintf(buffer, "Hume %d.%d%%", hInt, hDec); | |
lcdPrintLine(buffer, 1); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment