Skip to content

Interfacing BME280 Sensor with Raspberry Pi

Introduction

In this guide, we will explore the BME280 sensor and its usage with a Raspberry Pi. The BME280 is a versatile environmental sensor that measures temperature, humidity, and barometric pressure. By connecting the BME280 sensor to the Raspberry Pi and utilizing Python programming, we can gather accurate environmental data. This data can be used for various applications such as weather monitoring, indoor climate control, and IoT projects. The BME280 sensor provides reliable measurements and can be easily integrated into Raspberry Pi projects through I2C communication. Let's dive in and discover how to wire the BME280 sensor to the Raspberry Pi and read sensor data using Python.

Problem Statement

The objective is to create a circuit using a BME280 sensor that can detect temperature, pressure and humidity in the vicinity.

Here we will print 20 readings taken with a time interval of 0.1s. Each set of reading will be printed as a dictionary and all the 20 readings will be printed as a list.

Requirements

  1. Raspberry Pi
  2. BME280 sensor module
  3. Jumper wires

Working

The BME280 sensor is a versatile environmental sensor that can measure temperature, pressure, and humidity. When used with the Raspberry Pi 3B+, the BME280 sensor communicates with the Pi using the I2C (Inter-Integrated Circuit) interface. To utilize the BME280 sensor with the Raspberry Pi 3B+, you need to establish the I2C connection by configuring the appropriate I2C parameters. This involves initializing the I2C bus and defining the sensor's address on the bus. Once the I2C connection is established, you can read the sensor data using specific functions provided by libraries such as the Adafruit_BME280 library. These functions communicate with the sensor over the I2C bus and retrieve temperature, pressure, and humidity values from the BME280.

Circuit Diagram

  • Connect the 3.3V of BME280 sensor to 3V3 power (Pin 1) of RPi
  • Connect the GND of BME280 sensor to GND (Pin 6) of RPi
  • Connect the SDA of BME280 sensor to GPIO2 (Pin 3) of RPi
  • Connect the SCL of BME280 sensor to GPIO3 (Pin 5) of RPi
Raspberry Pi

Enabling I2C

Open the terminal and run the command sudo raspi-config and follow the given steps

While you are within this tool, you can use the ARROW keys to navigate and the ENTER key to select.

Raspberry Pi
Raspberry Pi
Raspberry Pi

Navigate to FINISH and press ENTER.

For our changes to take effect, we need to restart our Raspberry Pi. To restart the Raspberry Pi, all we need to do is use the command below.

cmd
sudo reboot

I2C is enabled.

Installing Libraries

  1. Open the terminal
  2. Update the package list then upgrade the packages, by using the command
cmd
sudo apt-get update && sudo apt-get install -y python-smbus i2c-tools
  1. Once everything is downloaded, shut down the RPi and restart again. Then we need to install the BME280 library for RPi
cmd
pip install bme280pi

Code

Now let's write the Python code to get temperature, humidity and pressure in a vicinity. Follow the steps below:

  1. Connect to your Raspberry Pi using VNC or Desktop environment.
  2. Launch the Geany Python IDE or open a text editor to write the code.
  3. Create a new file named bme280.py and save the following code into it
  4. Open the raspberrypi terminal and install the Adafruit_MCP3008 using pip install Adafruit_MCP3008 command.
  5. Open the raspberrypi terminal and install the SMBus using pip install SMBus command.
  6. You can copy and paste the following code from here into the file.
py

import time
import datetime
from bme280pi import Sensor

sensor = Sensor()

measurements = []
number_of_measurements = 20

for i in range(number_of_measurements):
    measurements.append(sensor.get_data())
    time.sleep(0.1)

print(measurements)
  1. After saving the file execute it by clicking on messenger like icon on geany editor or open terminal and navigate to the folder you saved the file, execute it by typing python bme280.py

Output

The BME280 sensor provides three outputs temperature, humidity, and barometric pressure with Raspberry Pi.

Conclusion

By following this guide, you have learned how to use a BME280 sensor with a Raspberry Pi. You have successfully wired the sensor to the Raspberry Pi and written Python code to read temperature, humidity, and barometric pressure data. With this knowledge, you can now monitor and analyze environmental conditions in your projects. Whether you're interested in weather monitoring, indoor climate control, or IoT applications, the BME280 sensor provides accurate and reliable data. You can further enhance your projects by integrating the BME280 sensor with other sensors or implementing actions based on environmental conditions. Enjoy exploring and experimenting with the BME280 sensor on your Raspberry Pi!