ESP32 BME280 Simulator: Wiring, Code and a Live Circuit
This is a BME280 temperature, humidity and pressure sensor wired to an ESP32 DevKit over I2C, read live with the Adafruit BME280 library once a second.
- 3.3 V logic
- Library: Adafruit_BME280.h
- I2C address 0x76
- SDA 21, SCL 22
Wiring the BME280 to the ESP32
Four wires: 3.3 V, GND, SDA to GPIO 21 (D21) and SCL to GPIO 22 (D22). In the editor, drag the sensor’s temperature, humidity and pressure sliders and the next reading follows.
Every connection in the circuit above, with the board’s own pin names.
| BME280 pin | ESP32 pin | Why |
|---|---|---|
| VIN | 3V3 | The BME280 chip is a 3.3 V part; many breakouts have no regulator. |
| GND | GND | Common ground. |
| SCL | GPIO 22 (D22) | I2C clock. |
| SDA | GPIO 21 (D21) | I2C data. |
Power
On an ESP32 DevKit, Wire.begin() with no arguments puts SDA on GPIO 21 and SCL on GPIO 22, labeled D21 and D22. The ESP32 and the BME280 are both 3.3 V parts, so no regulator and no level shifter is needed.
Pins to leave alone on the ESP32
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
On a real bench
The thirty-pin DevKit straddles a breadboard with one row of holes free on each side, which is where the BME280’s jumpers go. Many ESP32 boards are wider and cover both rows; then use two breadboards side by side. Hold BOOT while uploading if the IDE cannot connect.
The code, and what it does.
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // on I2C: SDA GPIO 21 (D21), SCL GPIO 22 (D22)
void setup() {
Serial.begin(115200);
if (!bme.begin(0x76)) {
Serial.println("No BME280 at 0x76");
for (;;) {}
}
Serial.println("BME280 found");
}
void loop() {
Serial.print("Temperature: ");
Serial.print(bme.readTemperature(), 1);
Serial.print(" C Humidity: ");
Serial.print(bme.readHumidity(), 1);
Serial.print(" % Pressure: ");
Serial.print(bme.readPressure() / 100.0F, 1);
Serial.println(" hPa");
delay(1000);
}
bme.begin(0x76) reads the chip’s ID and its factory calibration. It returns false when nothing answers at that address. Adafruit’s own board is at 0x77 and begin() with no argument tries that one; most purple modules are at 0x76.
readTemperature() is degrees Celsius, readHumidity() is percent relative humidity and readPressure() is pascals, so dividing by 100 gives hectopascals. All three come from one chip, which is the difference from a BMP280: it adds the humidity.
Serial.begin(115200) opens the serial monitor. Serial is UART0 on the USB bridge. ESP32 sketches usually run it at 115200 baud, which this one does.
Common mistakes with the BME280 on the ESP32
The wrong address
Adafruit’s board answers at 0x77, the cheap purple ones at 0x76, and bme.begin() with no argument tries 0x77. If it fails, try the other address before suspecting the wiring.
Using a BMP280 library
A BMP280 has no humidity and a different chip ID (0x58 against 0x60). Adafruit_BMP280 refuses a BME280 and the reverse, so match the library to the chip printed on the module.
Powering it from VIN (5 V from USB)
A breakout without a regulator is a bare 3.3 V chip: 5 V on VIN can kill it. Use 3V3.
Questions and answers
Can I simulate a BME280 with an ESP32 DevKit?
Yes. The circuit at the top of this page is an ESP32 DevKit with a BME280, running in your browser on a simulated ESP32 (Xtensa LX6). The temperature, humidity and pressure sliders set what the simulated chip measures, and the library applies the real calibration math to it. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Where does the BME280 connect on the ESP32?
On an ESP32 DevKit, Wire.begin() with no arguments puts SDA on GPIO 21 and SCL on GPIO 22, labeled D21 and D22.
What is the difference between the BME280 and the BMP280?
The BME280 adds a humidity sensor. The two use different libraries and report different chip IDs.
Why does it read a little warm?
On a real board the chip’s own heat and the board around it warm the sensor, so the temperature reads a degree or two high. The simulated chip reports exactly what its slider says.
Build your own BME280 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.