ESP8266 Pressure Sensor Project
This project demonstrates how to interface an ESP8266 microcontroller with a BMP280 pressure sensor. The BMP280 sensor measures atmospheric pressure, temperature, and altitude. These values are read from the sensor and displayed on the Serial Monitor.
How It Works
Barometric sensors such as the BMP180, BMP280 and BME280 use a MEMS piezoresistive element: a tiny silicon diaphragm whose resistance changes as atmospheric pressure flexes it. The chip digitises that internally and exposes calibrated readings over I²C, so no analog conditioning is needed.
These parts are genuinely precise. A BMP280 resolves pressure to about ±0.12 hPa, which corresponds to roughly a one-metre change in altitude — enough to detect moving between floors of a building. Pressure and altitude are related through the barometric formula, which the libraries implement for you.
The catch is that altitude derived from pressure is only as good as your sea-level reference. Weather systems shift sea-level pressure by tens of hectopascals, so an altitude calculated with the standard 1013.25 hPa constant can be tens of metres out. For relative measurements — how far up did I move — it is excellent; for absolute altitude it needs a current local reference.
All three parts speak I²C, which on the ESP8266 (NodeMCU) means D2 (SDA, GPIO4) and D1 (SCL, GPIO5) by default. Addresses are typically 0x76 or 0x77 depending on how the SDO pin is tied.
Components Needed
- ESP8266 (NodeMCU)
- Barometric Pressure Sensor module
- ESP8266 (NodeMCU)
- USB cable for programming and power
- ESP8266 (NodeMCU)
Wiring to the ESP8266 (NodeMCU)
Connect SDA and SCL to D2 (SDA, GPIO4) and D1 (SCL, GPIO5) by default, with VCC and GND to the supply rails. This board's 3.3 V logic matches the sensor natively, so no level shifting is involved.
I²C needs pull-up resistors on both lines. Nearly all breakout boards fit them, but chaining several modules stacks those pull-ups in parallel and can over-load the bus — remove the extras if communication becomes unreliable.
Shield the sensor from draughts and direct sun. The BME280's humidity and temperature channels in particular respond to airflow, and a sensor in sunlight reads several degrees high.
| Module pin | ESP8266 (NodeMCU) pin | Function |
|---|---|---|
| SDA | D2 (SDA, GPIO4) | I²C data |
| SCL | D1 (SCL, GPIO5) by default | I²C clock |
| VCC | 3V3 | Supply — the bare chip is 3.3 V |
| GND | GND | Common ground |
Example Code
Reading pressure, temperature and relative altitude from a BMP280. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.
#include <Wire.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
float referencePressure = 1013.25; // hPa — replace with your local value
void setup() {
Serial.begin(115200);
Wire.begin();
if (!bmp.begin(0x76)) { // try 0x77 if this fails
Serial.println("BMP280 not found — check wiring and address");
while (1) delay(1000);
}
// Capture the starting pressure so altitude is measured relative to here
delay(100);
referencePressure = bmp.readPressure() / 100.0;
Serial.print("Reference set to ");
Serial.print(referencePressure, 2);
Serial.println(" hPa");
}
void loop() {
float hPa = bmp.readPressure() / 100.0;
float degC = bmp.readTemperature();
float rel = bmp.readAltitude(referencePressure);
Serial.print("Pressure: "); Serial.print(hPa, 2); Serial.print(" hPa ");
Serial.print("Temp: "); Serial.print(degC, 1); Serial.print(" C ");
Serial.print("Rel height: ");Serial.print(rel, 1); Serial.println(" m");
delay(1000);
}
Applications
A barometric pressure sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Weather stations and short-term forecasting from pressure trends
- Altitude hold and height estimation on drones and model rockets
- Floor detection in indoor navigation
- Detecting whether a sealed enclosure has lost its seal
- Compensating other sensors whose readings vary with air density
Working with the ESP8266 (NodeMCU)
The ESP8266 (NodeMCU) is built around the ESP8266 and runs on 3.3 V logic with roughly 80 KB usable of SRAM and 4 MB on most NodeMCU boards of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The ESP8266 is a 3.3 V part and is not 5 V tolerant — feeding a 5 V sensor output straight into a GPIO can damage the chip. Use a divider or level shifter.
There is only one ADC channel, so reading several analog sensors needs an external multiplexer such as a CD4051 or an ADS1115.
D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are strapping pins sampled at boot; holding them at the wrong level stops the board starting.
Built-in WiFi means the same sketch can publish readings to a dashboard without extra hardware.
| ESP8266 (NodeMCU) characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 3.3 V | Sensor outputs above this level need a divider or level shifter |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | a single analog channel, A0 | Determines how many analog sensors can share the board |
| PWM outputs | any GPIO via software PWM | Needed for brightness, speed and tone control |
| I²C pins | D2 (SDA, GPIO4) and D1 (SCL, GPIO5) by default | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | any GPIO except D0 (GPIO16) | Required for counting fast or asynchronous events |
| Serial | one hardware UART plus a transmit-only second port | Monitor runs at 115200 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The sensor is not found — try the other address (0x76 versus 0x77), and run an I²C scanner to see what is actually on the bus.
- Altitude drifts over hours while the sensor sits still — that is real weather changing the pressure, not a fault.
- Temperature reads 2–3 °C high — the chip self-heats, and on combined modules the regulator adds more. Offset it, or read less often.
- Readings are unstable — an unshielded sensor near a fan or window responds to every gust. Enclose it with a small vent.
- Nothing works on a Mega after copying Uno wiring — I²C is on D20/D21 here, not A4/A5.
- The sketch compiles but the board resets or behaves erratically — a 5 V module output is being driven into a 3.3 V pin. Measure the signal before connecting it.
- Readings differ from an Arduino tutorial for the same part — the 10-bit ADC returns 0–1023, not 0–1023, so any constant copied from an Uno example needs rescaling.
Taking It Further on the ESP8266 (NodeMCU)
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the ESP8266 (NodeMCU) specifically:
The obvious extension on an ESP8266 is to stop printing to Serial and publish instead. A few lines using ESP8266WiFi and an HTTP client will push each reading to a dashboard such as ThingSpeak, or to an MQTT broker for home automation.
Running an onboard web server turns the board into its own display: serve a small HTML page that fetches the latest reading over AJAX, and any phone on the network becomes the instrument panel.
For battery operation, deep sleep is essential — the ESP8266 draws around 70 mA with WiFi active but under 20 µA asleep. Wire D0 (GPIO16) to RST so the board can wake itself, take a reading, publish and sleep again.
Notes and Practical Limits
The BMP280 measures pressure and temperature; the BME280 adds humidity for a small extra cost and is usually the better buy for weather work. They share the same library and address scheme, so the code barely changes.
For relative altitude, capture a reference at startup as the sketch does. That removes the weather-dependence entirely and gives genuinely useful centimetre-to-metre resolution.