Weather station (BME280)
Read temperature, humidity and air pressure from one BME280, work out the dew point and altitude, and light an LED when the air is damp.
Sensors · Beginner
In a real product: Home weather station (Smart home): Temperature, humidity and pressure give the forecast, the dew point and the altitude.
A BME280 measures three things at once over I²C: temperature, relative humidity and air pressure. From those a sketch can work out more: the dew point (how close the air is to condensing) and, from the pressure, the height above sea level.
What you'll learn
- Reading a combined sensor through its library, at the address the breakout uses
- Turning raw measurements into derived ones: dew point with the Magnus formula, altitude from pressure
- Why one chip's calibration values matter: the library applies them to every reading
How it works
bme.begin(0x76) checks the chip id (0x60 for a BME280; a BMP280, with no humidity, answers 0x58), reads the calibration values stored in the chip, and starts it measuring continuously. Each read...() call reads the raw counts and runs them through the datasheet's compensation formulas.
The dew point uses the Magnus approximation: g = ln(RH/100) + 17.62 T / (243.12 + T), then dew = 243.12 g / (17.62 - g). Above 70 %RH, mould starts to grow in a closed room, so the LED on pin 7 comes on.
Try this
- Click the sensor and raise the humidity past 70 %: the LED lights, and the dew point climbs towards the temperature.
- Lower the pressure by 12 hPa: the altitude rises by about 100 m.
- Change the sensor's address to 0x77 and see what the sketch prints, then fix the sketch.
sketch.ino
// A weather station: a BME280 measures temperature, humidity and pressure every two seconds; the sketch works out
// the dew point and the altitude, and lights the LED on pin 7 when the air is damp enough for mould (over 70 %RH).
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
const int DAMP_LED = 7;
void setup() {
Serial.begin(9600);
pinMode(DAMP_LED, OUTPUT);
if (!bme.begin(0x76)) {
Serial.println("No BME280 at 0x76: check the wiring, or try 0x77");
while (true) {}
}
}
void loop() {
float t = bme.readTemperature();
float rh = bme.readHumidity();
float hpa = bme.readPressure() / 100.0;
// Magnus formula: the temperature at which this air would start to condense.
float g = log(rh / 100.0) + 17.62 * t / (243.12 + t);
float dew = 243.12 * g / (17.62 - g);
Serial.print(t, 1);
Serial.print(" C, ");
Serial.print(rh, 1);
Serial.print(" %RH, ");
Serial.print(hpa, 1);
Serial.print(" hPa, dew point ");
Serial.print(dew, 1);
Serial.print(" C, altitude ");
Serial.print(bme.readAltitude(1013.25), 0);
Serial.println(" m");
digitalWrite(DAMP_LED, rh > 70 ? HIGH : LOW);
delay(2000);
}