Weather station on an Arduino Uno
A DHT22 on one wire and a 16x2 I2C display on two. Forty bits and a checksum, a first reading that fails, and a sensor that will not be hurried. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
IntermediateRuns in your browser. Free, and no account needed.
Step by step, with the wiring and the common mistakes: Arduino DHT22 Temperature and Humidity Sensor
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Weather station: a DHT22 on one wire, the reading on an I2C display.
//
// pin 2 the DHT22's DATA. The module carries its own pull-up.
// LCD SDA -> A4 SCL -> A5 VCC -> 5 V GND -> GND
//
// Two wires for the display and *one* for the sensor, which is why this pair
// is in every kit. There is a great deal going on under those three wires and
// almost none of it is visible from the sketch, so it is worth writing down.
//
// # One wire, and it is all timing
//
// There is no bus standard on the sensor's pin. The board pulls the line down
// to ask, lets go, and the sensor answers with forty pulses whose *widths* are
// the bits: 50 us of low, then 27 us of high for a nought and 70 us for a one.
// The driver (firmware/lib/mokxi_dht.h) is a stopwatch and nothing else.
//
// Five bytes come back (humidity high, humidity low, temperature high,
// temperature low, checksum), and the checksum is the low eight bits of the
// sum of the other four. If it does not match, the reading is thrown away.
// That happens on a real bench and it happens here.
//
// # The first reading always fails
//
// Both datasheets say to leave the sensor alone for a second after power comes
// up, and Mokxi's part does exactly that: a request inside that second is not
// answered at all. So the first loop prints "waiting" rather than a number,
// and `setup()` does not hide it behind a delay, because meeting it once is
// worth more than never seeing it.
//
// # Ask it twice in two seconds and it repeats itself
//
// The DHT22 measures every two seconds and hands back the last reading in
// between. Drag the temperature slider and watch: the display does not move
// until the sensor has looked again. The sketch asks every two and a half
// seconds, which is the rate the sheet wants.
//
// # No floating point
//
// This board has none. Everything here is tenths (23.4 C is 234), and the
// display's printFixed() puts the point back in. A float would be a library
// call per reading for accuracy that was never in the sensor.
#include "mokxi_dht.h"
#include "mokxi_lcd1602_i2c.h"
const int DHT_PIN = 2;
const int SDA_PIN = A4;
const int SCL_PIN = A5;
// Every two and a half seconds: past the sensor's own two.
const unsigned long PERIOD_MS = 2500;
Dht sensor(DHT_PIN, DHT22);
Lcd1602I2c lcd(SCL_PIN, SDA_PIN);
unsigned long askedAt = 0;
int readings = 0;
int failures = 0;
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Uno: DHT22 on pin 2, reading on the I2C display");
sensor.begin();
lcd.begin();
lcd.print("DHT22 weather");
lcd.setCursor(0, 1);
lcd.print("waiting...");
}
void loop() {
// The very first loop asks straight away, on purpose: that lands inside the
// sensor's settling second and gets no answer, which is what a real one does
// and is worth seeing once.
if (readings + failures > 0 && millis() - askedAt < PERIOD_MS) {
return;
}
askedAt = millis();
unsigned char frame[5];
if (!sensor.read(frame)) {
failures++;
Serial.print("no answer (");
Serial.print(failures);
Serial.println(" so far)");
lcd.setCursor(0, 1);
lcd.print("no answer");
lcd.padLine();
return;
}
readings++;
int celsius = sensor.temperatureTenths(frame);
int humidity = sensor.humidityTenths(frame);
lcd.setCursor(0, 0);
lcd.printFixed(celsius, 1);
lcd.print(" C");
lcd.padLine();
lcd.setCursor(0, 1);
lcd.printFixed(humidity, 1);
lcd.print(" %RH");
lcd.padLine();
Serial.print("reading ");
Serial.print(readings);
Serial.print(": ");
Serial.print(celsius / 10);
Serial.print(".");
Serial.print(celsius < 0 ? -(celsius % 10) : celsius % 10);
Serial.print(" C, ");
Serial.print(humidity / 10);
Serial.println(" %RH");
}
Parts list
4 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 2; Temperature and humidity, DHT22 pin DATA
- Arduino Uno R3 pin 5V; Temperature and humidity, DHT22 pin VCC; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Temperature and humidity, DHT22 pin GND; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.