Sensors
Weather station
Temperature, humidity and air pressure on a small screen, with a note of whether the pressure is rising or falling.
- Medium
- about 45 min
- ESP32-S3
- 6 parts
- 8 wires
- 72 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
Falling pressure often comes before rain. The station compares the pressure now with half an hour ago, which is the oldest method of forecasting there is.
Before you start
For outdoors, put it under cover where rain cannot reach it, with the sensor in moving air and out of direct sun.
What you learn
Two I2C devices on one bus, and finding a trend by comparing with an earlier reading.
How it works
input
BME280
decides
ESP32-S3
output
OLED 0.96 in
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 6 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 8
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
weather-station.ino
// Weather station
// Temperature, humidity and pressure on a small screen, with a note of
// whether the pressure is rising or falling: falling often means rain.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BME280.h>
const int PIN_BME_SDA = 8;
const int PIN_BME_SCL = 9;
const int PIN_OLED_SDA = 8;
const int PIN_OLED_SCL = 9;
const unsigned long TREND_EVERY_MS = 30UL * 60UL * 1000UL; // compare pressure every 30 minutes
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
Adafruit_BME280 bme;
bool sensorOk = false;
float trendPressure = 0;
unsigned long trendTime = 0;
const char* trend = "steady";
void setup() {
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
sensorOk = bme.begin(0x76, &Wire) || bme.begin(0x77, &Wire);
}
void loop() {
oled.clearDisplay();
if (!sensorOk) {
oled.setTextSize(1);
oled.setCursor(0, 24);
oled.print("BME280 not found");
oled.display();
delay(2000);
return;
}
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0f;
if (trendTime == 0) {
trendPressure = pressure;
trendTime = millis();
} else if (millis() - trendTime > TREND_EVERY_MS) {
float change = pressure - trendPressure;
trend = change > 1.0f ? "rising" : change < -1.0f ? "falling" : "steady";
trendPressure = pressure;
trendTime = millis();
}
oled.setTextSize(3);
oled.setCursor(0, 0);
oled.print(temperature, 1);
oled.setTextSize(1);
oled.print(" C");
oled.setTextSize(2);
oled.setCursor(0, 30);
oled.print(humidity, 0);
oled.print("%");
oled.setTextSize(1);
oled.setCursor(64, 30);
oled.print(pressure, 0);
oled.print(" hPa");
oled.setCursor(64, 42);
oled.print(trend);
oled.display();
delay(2000);
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit SSD1306: install it from Sketch > Include Library > Manage Libraries.
- Adafruit BME280 Library: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.