What you will learn
- What I2C is: two wires that can carry data for many sensors at once.
- How to install and use an Arduino library.
- How to show live sensor readings on a web page that refreshes itself.
Before you start: do steps 1 to 5 of the beginner's guide. It helps to have done the Wi-Fi relay switch, since this project reuses its web server.
Parts you need
Some links are affiliate links, so I may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Details. Buy buttons open a store search; check the product matches before you order.
Wire it up
Board unplugged from USB while you wire. The sensor needs only four wires.
Power it from 3.3V. Most BME280 modules are 3.3V parts. Connecting VCC to 5V can destroy some of them. Also check the labels on your module: pin order differs between makers.
| BME280 pin | Connects to (classic ESP32) |
|---|---|
| VCC (or VIN) | 3V3 |
| GND | GND |
| SCL (clock) | GPIO 22 |
| SDA (data) | GPIO 21 |
Using an ESP32-S3 or ESP32-C3?
| Signal | Classic ESP32 | ESP32-S3 | ESP32-C3 |
|---|---|---|---|
| SDA | GPIO 21 | GPIO 8 | GPIO 8 |
| SCL | GPIO 22 | GPIO 9 | GPIO 9 |
What is I2C? It is a two-wire bus. SDA carries data and SCL carries a clock that keeps both sides in step. Many different sensors and displays can share the same two wires because each has its own address. The BME280's address is usually 0x76 or 0x77.
Install the sensor library
- In the Arduino IDE, open Sketch, Include Library, Manage Libraries (or click the library icon on the left).
- Search for Adafruit BME280 and click Install. When asked about dependencies, choose Install all. This also installs Adafruit Unified Sensor and Adafruit BusIO.
Optional: check the sensor is wired correctly with an I2C scanner
This tiny sketch lists every I2C device it finds. If your BME280 shows up at 0x76 or 0x77, the wiring is right. Upload it and open the Serial Monitor at 115200.
The code
- Open a new sketch and replace everything with the code below. Fill in your Wi-Fi name and password.
- Upload it, open the Serial Monitor at 115200 baud, and press the board's EN button. It prints the address to open.
How the code works
#includepulls in the libraries.Adafruit_BME280 bme;creates a sensor object you can ask for readings.bme.readTemperature(),readHumidity()andreadPressure()do all the hard work. Pressure comes back in pascals, so we divide by 100 to get hectopascals (hPa).String(tC, 1)turns a number into text with 1 decimal place.- The line with
http-equiv='refresh'tells the browser to reload the page every 10 seconds, so the numbers stay fresh with no extra code. while (true) delay(1000);deliberately stops the program if the sensor is missing, so you see the error message instead of silent failure.
Test it
- The Serial Monitor should say BME280 found, then an address like
http://192.168.1.57, then a line of readings every 5 seconds. - Open that address on a phone or computer on the same Wi-Fi. You should see the three readings.
- Breathe gently on the sensor. The humidity jumps up within a few seconds and the temperature rises slightly. That proves it is live.
- Compare the temperature with a thermometer you trust. If it reads high, set
TEMP_OFFSETto a negative number such as-1.5.
For accurate readings, move the sensor a few centimetres away from the ESP32 on its wires. The board and its Wi-Fi radio generate heat, which can make the temperature read 1 to 3 degrees high when the sensor sits right next to it.
Troubleshooting
| Problem | What to try |
|---|---|
| "BME280 not found" | Check all four wires, and that VCC is on 3V3. Run the I2C scanner above. If it finds an address other than 0x76 or 0x77, use that number in bme.begin(). If it finds nothing, swap SDA and SCL, check each wire, and try a different module. |
| Scanner finds 0x76 but BME280 still fails | You may have a BMP280 (no humidity sensor). They look identical. Install the Adafruit BMP280 library, or buy a BME280. |
| Humidity shows 0 or a strange number | Same cause: a BMP280 cannot measure humidity. |
| Wi-Fi never connects | Check name and password for typos and that your network has 2.4 GHz turned on. |
| Page loads but never updates | Your browser may block refresh. Reload manually, or try another browser. |
| Temperature seems wrong | See the tip above about heat from the board, and use TEMP_OFFSET. |
Make it better
- Add the OLED display from project 4 so the readings show on a screen.
- Log the readings with timestamps to a microSD card, or send them to a service like ThingSpeak.
- Run it on a battery with deep sleep, waking every few minutes. Use a low-power board.
- Add a second sensor, such as a light sensor, on the same two I2C wires.
Open this project in the Build Finder →
Where to next
Project 4: Wi-Fi clock on an OLED. Use a small display and the internet's time. · ← Project 2: Wi-Fi relay switch