Get started quickly with the BME280 sensor module with the Raspberry Pi Pico board, programmed using the Arduino IDE, to get temperature, humidity, and pressure readings. This brief tutorial will teach you how to use the BME280 sensor with your Raspberry Pi Pico board. We’ll show you how to wire the sensor, install the required libraries, and write a simple sketch to display sensor readings.
New to the Raspberry Pi Pico? Get started with the Raspberry Pi Pico here.
Table of Contents:
- Raspberry Pi Pico with Arduino IDE
- Introducing the BME280 Sensor Module
- Wiring the BME280 to the Raspberry Pi Pico
- BME280 Library for Arduino IDE
- BME280 Pressure, Temperature, and Humidity – Arduino Sketch
Do you prefer to program the Raspberry Pi Pico using MicroPython firmware? Check this tutorial instead: Raspberry Pi Pico – BME280 Get Temperature, Humidity, and Pressure (MicroPython).
Raspberry Pi Pico with Arduino IDE
You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven’t already:
Introducing the BME280 Sensor Module
The BME280 sensor module reads barometric pressure, temperature, and humidity. Because pressure changes with altitude, you can also estimate altitude. There are several versions of this sensor module, but we’re using the one shown in the figure below.
This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use any Raspberry Pi Pico I2C pins to connect the BME280 sensor. We’ll be using GPIO 4 (SDA) and GPIO 5 (SCL)—learn more about the Raspberry Pi Pico GPIOs.
BME280 | Raspberry Pi Pico |
Vin | 3.3V (OUT) |
GND | GND |
SCL | GPIO 5 |
SDA | GPIO 4 |
Parts Required
For this project, you need to wire the BME280 sensor module to the Raspberry Pi Pico I2C pins. Here’s a list of parts you need for this tutorial:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Wiring the BME280 to the Raspberry Pi Pico
Wire the BME280 to any combination of the Pico I2C pins—we’ll be using GPIO 4 (SDA) and GPIO 5 (SCL) which are the default Raspberry Pi I2C pins when programmed with Arduino IDE.
Recommended reading: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained
Installing the BME280 Library for Raspberry Pi Pico
To get readings from the BME280 sensor module, we’ll use the Adafruit_BME280 library. Follow the next steps to install the library in your Arduino IDE:
Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
Search for “adafruit bme280 ” in the Search box and install the library by Adafruit.
A pop-up window will open asking you to install the Adafruit Unified Sensor library. Install that library. If you don’t get that pop-up window, you need to go to Sketch > Include Library > Manage Libraries and type “adafruit unified sensor”. Scroll down and install the Adafruit Unified Sensor library.
BME280 Pressure, Temperature, and Humidity – Arduino Sketch
To show you how to read temperature, humidity, and pressure from the BME280, we’ll use a simple sketch that prints the current readings every second to the Serial Monitor.
Copy the following code to the Arduino IDE. The following example is adapted from one of the examples provided by the Adafruit_BME280 library.
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-bme280-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Adapted from the Adafruit BME280 examples: https://github.com/adafruit/Adafruit_BME280_Library
*********/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C (default pins for Raspberry Pi Pico: GPIO 4 (SDA), GPIO 5(SCL)
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
unsigned long delayTime;
void setup() {
Serial.begin(115200);
Serial.println("BME280 with Raspberry Pi Pico");
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
delayTime = 1000;
Serial.println();
}
void loop() {
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
// Convert temperature to Fahrenheit
/*Serial.print("Temperature = ");
Serial.print(1.8 * bme.readTemperature() + 32);
Serial.println(" *F");*/
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
How the Code Works
Continue reading this section to learn how the code works, or skip to the Demonstration section.
Libraries
The code starts by including the needed libraries: the wire library to use I2C, and the Adafruit_Sensor and Adafruit_BME280 libraries to interface with the BME280 sensor.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
SPI communication
As we’re going to use I2C communication, the following lines that define the SPI pins are commented:
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
Sea level pressure
A variable called SEALEVELPRESSURE_HPA is created.
#define SEALEVELPRESSURE_HPA (1013.25)
This variable saves the pressure at the sea level in hectopascal (is equivalent to millibar). This variable is used to estimate the altitude for a given pressure by comparing it with the sea level pressure. This example uses the default value, but for more accurate results, replace the value with the current sea level pressure at your location.
I2C Communication
This example uses I2C communication protocol by default. As you can see, you just need to create an Adafruit_BME280 object called bme. It will automatically use GPIO 4 as SDA and GPIO 5 as SCL.
Adafruit_BME280 bme; // I2C
setup()
In the setup(), start a serial communication:
Serial.begin(115200);
The following lines initialize the sensor. Our sensor uses I2C address 0x76. Most BME280 sensors will use that address, but if yours has a different one, make sure you modify that on the code.
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
You can follow this tutorial to find the address of your I2C devices: Raspberry Pi Pico: I2C Scanner (Arduino IDE) – Finding the Address of I2C Devices.
Printing Temperature, Humidity, and Pressure values
In the loop(), the printValues() function reads the values from the BME280 and prints the results in the Serial Monitor.
void loop() {
printValues();
delay(delayTime);
}
Reading temperature, humidity, pressure, and estimate altitude is as simple as using the following methods on the bme object:
- bme.readTemperature() – reads temperature in Celsius;
- bme.readHumidity() – reads absolute humidity;
- bme.readPressure() – reads pressure in hPa (hectoPascal = millibar);
- bme.readAltitude(SEALEVELPRESSURE_HPA) – estimates altitude in meters based on the pressure at the sea level.
If you also want to display the temperature in Fahrenheit degrees, you need to uncomment the following lines of code:
/*Serial.print("Temperature = ");
Serial.print(1.8 * bme.readTemperature() + 32);
Serial.println(" *F");*/
Uploading the Code to the Raspberry Pi Pico
To upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.
If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.
For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.
Now, select your COM port in Tools > Port. It may be the case that the COM port is grayed out. If that’s the case, don’t worry it will automatically find the port once you hit the upload button.
Upload the code.
You should get a success message.
Demonstration
After uploading the code to the Raspberry Pi Pico board, open the Serial Monitor at a baud rate of 115200.
New sensor readings will be displayed in the Serial Monitor every second.
Troubleshooting
Your BME280 is not working? It might be: incorrect wiring, wrong I2C address, broken sensor or you got a fake BME280 sensor. Take a look at this BME280 troubleshooting guide to find the cause and solution for your problem.
Wrapping Up
This tutorial was a quick getting started guide on how to get temperature, humidity, and pressure from a BME280 sensor with the Raspberry Pi Pico using Arduino IDE.
We hope you’ve found this tutorial useful. We have tutorials for other popular environmental sensors:
- Raspberry Pi Pico: DS18B20 Temperature Sensor (Arduino IDE) – Single and Multiple
- Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (Arduino IDE)
You can check all our Raspberry Pi Pico projects on the following link:
Finally, if you would like to interface the BME280 with other microcontrollers, we have tutorials for ESP32, ESP8266, Arduino, and even for the Raspberry Pi board:
- ESP32: Getting Started with the BME280 Sensor
- ESP8266: Getting Started with the BME280 Sensor
- Arduino: Getting Started with the BME280 Sensor
- ESP32/ESP8266 (MicroPython): Getting Started with the BME280 Sensor
- Raspberry Pi: BME280 Temperature, Humidity and Pressure Sensor (Python)
Thanks for reading.
This is one of my favorite sensor parts. In my use of them with these generic purple breakout boards over the years, I’ve noticed a couple of things that might confuse some people.
As is evident in your article’s photo, the same breakout board is used for both the BME280 and the BMP280. Although there should be some kind of marking on the board to say which is which, there often is nothing. Worse yet, many “no name” sellers of the parts will advertise one but actually provide the other. Typically, they advertise BME280 (the more expensive part) and supply BMP280. I don’t attribute this to dishonesty as much as to carelessness.
The BME280 can have either of 2 I2C addresses: 0x77 or 0x76. I’ve seen both.
With the combination of those two confusing situations, I usually run some code that scans the I2C bus to see what address actually responds for a particular device. Then I try to program it as a BMP280 and a BME280 and see which one works. There’s some kind of device ID or something that the library code can check for a mismatch.
Once I get all the confusion worked out, these devices are great. Thanks for the write-up.
Hi.
Thanks for pointing that out.
We have an article addressing those two situations: https://randomnerdtutorials.com/solved-could-not-find-a-valid-bme280-sensor/
Maybe it’s a good idea to include a note to this guide.
Regards,
Sara
That’s a pretty good article you already had. I see you are way ahead of me. 🙂
I’ve 3 BME280 running, on different boards, with different libraries. With all i always get a pressure reading of ca. 960 hPa, with only little change. Are these fake parts?
Hi.
Sometimes, vendors sell BMP280 instead of BME280.
The downside of having a BMP280 is that it doesn’t measure humidity, it only measures temperature and pressure.
So, it shouldn’t be the issue here.
Regards,
Sara
Great tutorial.
I am enjoying the Pico and programming it in the Arduino IDE,
The BME280 are a great device to experiment with. On one of my Pico’s I have a BME280 connected along with an OLED screen to output the details, and an RTC on the I2C bus aolng with an SD card module to record the temp, humidity and pressure with date and time.
Has been a fun project to prototype (program still needs some tweaks, error checks and tidying up) and I have a small tin (Smaller than an Altoids) I plan to put the final project into.
That’s great.
Thanks for sharing the details of the project you’re working on.
Regards,
Sara
Thank you for providing great tutorial.
I used PlatformIO.
During compiling, I got an error for ‘Wire.h’.
I’ve searched the head file. I do not know what I have to apply for this coding.
Can you please advise?
Thanks,
Your Raspberry pi python code for the BME280 no longer works
Install has been changed and no longer matches the parameters in your code for BME280