← Beginner's guide to ESP32

Build an ESP32 Wi-Fi weather station

Read temperature, humidity and air pressure from a tiny sensor and see them on a web page you can open from any phone on your network. You will learn how I2C sensors and libraries work, which unlocks hundreds of other parts.

What you will learn

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.

Weather station Temperature21.5 °C Humidity48 % Pressure1013 hPa refreshes every10 seconds any phone orcomputer on Wi-Fi
What you will see when it works.

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.

Red = power, black = ground, yellow = clock (SCL), blue = data (SDA). Pin numbers are for a classic ESP32 DevKit.
BME280 pinConnects to (classic ESP32)
VCC (or VIN)3V3
GNDGND
SCL (clock)GPIO 22
SDA (data)GPIO 21
Using an ESP32-S3 or ESP32-C3?
SignalClassic ESP32ESP32-S3ESP32-C3
SDAGPIO 21GPIO 8GPIO 8
SCLGPIO 22GPIO 9GPIO 9

Change SDA_PIN and SCL_PIN at the top of the sketch.

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

  1. In the Arduino IDE, open Sketch, Include Library, Manage Libraries (or click the library icon on the left).
  2. 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

  1. Open a new sketch and replace everything with the code below. Fill in your Wi-Fi name and password.
  2. 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

Test it

  1. The Serial Monitor should say BME280 found, then an address like http://192.168.1.57, then a line of readings every 5 seconds.
  2. Open that address on a phone or computer on the same Wi-Fi. You should see the three readings.
  3. Breathe gently on the sensor. The humidity jumps up within a few seconds and the temperature rises slightly. That proves it is live.
  4. Compare the temperature with a thermometer you trust. If it reads high, set TEMP_OFFSET to 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

ProblemWhat 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 failsYou 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 numberSame cause: a BMP280 cannot measure humidity.
Wi-Fi never connectsCheck name and password for typos and that your network has 2.4 GHz turned on.
Page loads but never updatesYour browser may block refresh. Reload manually, or try another browser.
Temperature seems wrongSee the tip above about heat from the board, and use TEMP_OFFSET.

Make it better

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

This guide was written from standard Arduino and ESP32 practice and the Adafruit BME280 library documentation. It has not been built on every ESP32 variant. Not affiliated with the part makers.