ArduinoArduino UnoExact chip

Weather station

A DHT22 read every 2 s, shown on an I2C LCD and the serial port.

Weather station in Argynix Lab

The code

sketch.inoC++
// A DHT22 on pin 2 and a 16x2 LCD on I2C (address 0x27, SDA A4, SCL A5).
// Select the sensor to change the temperature and humidity.
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

DHT dht(2, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  dht.begin();
  lcd.init();
  lcd.backlight();
  lcd.print("ARGYNIX Weather");
  delay(1000);
}

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t) || isnan(h)) {
    Serial.println("Sensor error");
    delay(2000);
    return;
  }
  lcd.clear();
  lcd.print("Temp: ");
  lcd.print(t, 1);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(h, 0);
  lcd.print("%");
  Serial.print("temp:");
  Serial.print(t, 1);
  Serial.print(",humidity:");
  Serial.println(h, 1);
  delay(2000);
}