⭐⭐ Advanced beginner
⏱️ 20 minutes
Mini weather station
You make a mini weather station. The DHT11 measures the temperature and the humidity, and the little OLED screen shows them.
🧰 What do you need?
🗺️ Wiring diagram
Follow the wires from the Arduino to each part. You can choose the wire colors yourself.
| From | To |
|---|---|
Arduino UNO · D2 |
Temperature sensor DHT11 · DAT |
Arduino UNO · 5V |
Temperature sensor DHT11 · VCC |
Arduino UNO · GND |
Temperature sensor DHT11 · GND |
Arduino UNO · A4 |
OLED screen 0.91" · SDA |
Arduino UNO · A5 |
OLED screen 0.91" · SCL |
Arduino UNO · 5V |
OLED screen 0.91" · VCC |
Arduino UNO · GND |
OLED screen 0.91" · GND |
🛠️ This is how you build it
- Connect the DHT11: DAT to pin 2, VCC to 5V and GND to GND.
- Connect the OLED screen: SDA to A4, SCL to A5, VCC to 5V and GND to GND.
- Tip: two parts need 5V and GND. Use the + and − strips of your breadboard.
💻 The code
// Mini weather station
// The DHT11 measures the temperature and the humidity. The little OLED screen shows it.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
const int DHT_PIN = 2; // DAT of the DHT11 is on pin 2
DHT dht(DHT_PIN, DHT11);
Adafruit_SSD1306 display(128, 32, &Wire, -1); // OLED of 128 x 32 pixels (SDA = A4, SCL = A5)
void setup() {
Serial.begin(9600);
dht.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the address of the display
Serial.println(F("Display not found. Check the SDA and SCL wires."));
while (true); // Stop: without a display it makes no sense
}
display.setTextColor(SSD1306_WHITE);
}
void loop() {
float temperature = dht.readTemperature(); // In degrees Celsius
float humidity = dht.readHumidity(); // In percent
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
if (isnan(temperature) || isnan(humidity)) { // Did measuring not work?
display.setTextSize(1);
display.print(F("No DHT11 found"));
} else {
display.print(temperature, 1);
display.print((char)247); // The degree sign
display.println(F("C"));
display.print(humidity, 0);
display.print(F("% hum"));
Serial.print(F("Temperature: "));
Serial.print(temperature);
Serial.print(F(" C Hum: "));
Serial.print(humidity);
Serial.println(F(" %"));
}
display.display(); // Only now does everything really appear on the display
delay(2000); // The DHT11 can measure only once every 2 seconds
}
🧠 What does the code do?
- The DHT11 is on pin 2. With
readTemperature()andreadHumidity()we read the values. - The OLED screen talks through SDA (A4) and SCL (A5). With
print()we put text on the screen. - Every 2 seconds there is a new measurement. Does measuring not work? Then you see a message.
🎯 Try this…
- Also show the temperature in Fahrenheit
- Turn on an LED when it is warmer than 25 degrees
- Remember the highest temperature of today and show that too
🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/weather-station and click MAKE THIS. Then the code and the wiring diagram are ready right away.
Codey Online is a product of OTRONIC