STANNUM
Open the simulator

Tutorials / Sensors

BME280 environmental sensor

Temperature, pressure and humidity from one I²C chip, read by the real Adafruit library — calibration and all.

Parts in the example:
  • BME280
The BME280 block as it appears on the canvas.

What it is

The Bosch BME280 measures the three quantities of a weather station in a chip the size of a grain of rice. It talks I²C at 0x76 or 0x77 (an SDO pin on the breakout chooses), and it is not read directly: the library reads raw values and calibration constants from the chip and runs Bosch's compensation formulas. The cheaper BMP280 looks the same and lacks humidity.

In the simulator

The block has three sliders: temperature (−40 to 85 °C), pressure (300 to 1100 hPa) and humidity. Settings hold the address and the Chip variant: switch it to BMP280 and the BME280 library refuses the sensor in begin(), exactly as it does when you buy the look-alike.

Pins

PinWhat it is
SDAI²C data — to a GPIO
SCLI²C clock — to a GPIO
3V3power — to a 3V3 symbol
GNDground — to a GND symbol

Wiring

The circuit below is the example Weather station — BME280 from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
BME280 pinGoes to
SDAD21 on the board (GPIO 21)
SCLD22 on the board (GPIO 22)
3V3a 3V3 symbol
GNDa GND symbol

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the BME280.
SettingAcceptsDefaultNotes
I²C address0x76 (SDO to GND) · 0x77 (SDO to VCC)0x76 (SDO to GND)The BME280 has two possible addresses, chosen by the SDO pin on the board.
ChipBME280 (with humidity) · BMP280 (no humidity)BME280 (with humidity)The BMP280 looks identical and does not measure humidity. The BME280 library rejects its id.

Code

sketch.cpp
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_BME280.h>

// SDA and SCL can be ANY GPIOs on the ESP32 — Wire.begin is what picks them.
const int SDA_PINO = 21, SCL_PINO = 22;
const float NIVEL_MAR_HPA = 1013.25;

Adafruit_BME280 bme;

void setup() {
  Serial.begin(115200);
  delay(300);
  Wire.begin(SDA_PINO, SCL_PINO);
  if (!bme.begin(0x76)) {
    Serial.println("BME280 not found — check SDA, SCL and the address");
    return;
  }
  Serial.println("BME280 ready");
}

void loop() {
  float t = bme.readTemperature();
  float p = bme.readPressure() / 100.0F;      // Pa -> hPa
  float u = bme.readHumidity();

  // pressure drops ~1 hPa every 8 m: the sensor doubles as an altimeter
  float altitude = bme.readAltitude(NIVEL_MAR_HPA);

  Serial.printf("%.1f C | %.1f hPa | %.0f %% | ~%.0f m\n", t, p, u, altitude);

  if (u > 80) Serial.println("  high humidity: is it going to rain?");
  if (t < 10) Serial.println("  cold!");
  delay(1000);
}

Wire.begin(21, 22), bme.begin(0x76), then readTemperature(), readPressure() and readHumidity(). readAltitude(1013.25) turns pressure into an altitude — the sensor doubles as an altimeter. Libraries: Adafruit BME280, Adafruit Unified Sensor, Adafruit BusIO (the example lists them).

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Drag any slider and the next line in the serial monitor follows, with the library's own formatting. Set the chip to BMP280 in Settings and press restart: “BME280 not found”.

How the simulation models it

Try this

  1. Show the three readings on the OLED display (add it to the circuit on the same SDA/SCL — I²C is a bus).
  2. Compute the dew point from temperature and humidity.

See also