BMP388 altimeter on an Arduino Uno
A BMP388 barometer on the Uno’s I2C pins, read with Adafruit_BMP3XX.h: the serial monitor prints the pressure, temperature and altitude every second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
What you'll learn
- Sensors
- School
- Using the Pressure sensor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 2
How it works
A BMP388 barometer on the Uno’s I2C pins, read with Adafruit_BMP3XX.h: the serial monitor prints the pressure, temperature and altitude every second.
Arduino Uno R3 runs the sketch below. The circuit around it: Pressure sensor, BMP388. delay waits 1000 milliseconds. Serial.begin opens the serial port at 9600 baud.
Parts list
2 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Pressure sensor, BMP388 pin VIN | Arduino Uno R3 pin 5V |
| Pressure sensor, BMP388 pin GND | Arduino Uno R3 pin GND |
| Pressure sensor, BMP388 pin SDA | Arduino Uno R3 pin A4 |
| Pressure sensor, BMP388 pin SCL | Arduino Uno R3 pin A5 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A BMP388 barometer as an altimeter.
//
// The BMP388 answers at 0x77 (0x76 with SDO to GND). Each reading is raw
// counts that Adafruit_BMP3XX turns into degrees and pascals with the 21
// trimming bytes Bosch stores in every chip.
//
// BMP388 VIN -> 5V, GND -> GND, SCL -> A5, SDA -> A4
//
// The board has its own regulator and pull-ups, so 5 V on VIN is fine.
// Every second the sketch prints the pressure, the temperature and the
// height above the sea-level pressure below. Drag the pressure slider down:
// about 12 Pa (0.12 hPa) less is a meter higher.
//
// THINGS TO TRY. Set SEA_LEVEL_HPA to today's sea-level pressure from a
// weather report for a true altitude. Then zero it: read the pressure once
// in setup() and use that, and the sketch shows height above the desk.
#include <Wire.h>
#include <Adafruit_BMP3XX.h>
const float SEA_LEVEL_HPA = 1013.25;
Adafruit_BMP3XX bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin_I2C()) {
Serial.println("No BMP388 at 0x77: check SDA, SCL and power.");
while (1) {}
}
Serial.println("BMP388 found at 0x77");
// 8x pressure and 2x temperature: about 21 ms a reading.
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_2X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);
}
void loop() {
if (!bmp.performReading()) {
Serial.println("Reading failed");
delay(1000);
return;
}
Serial.print("pressure ");
Serial.print(bmp.pressure / 100.0, 2);
Serial.print(" hPa, temperature ");
Serial.print(bmp.temperature, 2);
Serial.print(" C, altitude ");
Serial.print(bmp.readAltitude(SEA_LEVEL_HPA), 1);
Serial.println(" m");
delay(1000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(9600);
if (!bmp.begin_I2C()) {
Serial.println("No BMP388 at 0x77: check SDA, SCL and power.");
while (1) {}
}
Serial.println("BMP388 found at 0x77");
// 8x pressure and 2x temperature: about 21 ms a reading.
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_2X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
if (!bmp.performReading()) {
Serial.println("Reading failed");
delay(1000);
return;
}
Serial.print("pressure ");
Serial.print(bmp.pressure / 100.0, 2);
Serial.print(" hPa, temperature ");
Serial.print(bmp.temperature, 2);
Serial.print(" C, altitude ");
Serial.print(bmp.readAltitude(SEA_LEVEL_HPA), 1);
Serial.println(" m");
delay(1000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.