PiicoDev Atmospheric Sensor BME280 - Raspberry Pi Guide

Updated 06 April 2022

Introduction

This guide will help you read weather data from your PiicoDev® Atmospheric Sensor and a Raspberry Pi single-board computer

To follow along, it's best to have:

In the video we use a PiicoDev Platform to keep everything safe and secure.

For this tutorial, we'll assume you're familiar using a Raspberry Pi like a desktop computer - connected to a keyboard, mouse, monitor and with an internet connection. If you need help getting started, have a look at Chapter 1 of the Raspberry Pi Workshop for Beginners.

If you prefer not to use the Expansion Board for Raspberry Pi, there are other connection options in our PiicoDev Connection Guide.

Connect the PiicoDev sensor to your Pi

Mount the PiicoDev® Adapter on your Raspberry Pi and plug connect your Atmospheric Sensor to the Adapter via the PiicoDev cable.

The adapter connects to the 40-pin GPIO header on the Raspberry Pi -  ensure the header is mounted correctly, with the "Ethernet" label on the same side as the Pi's ethernet adapter.

connect-bm280-piicodev-sensor-to-raspberry-pi

Pictured above: A Raspberry Pi 4 Model B with USB Power and HDMI (two white leads). The PiicoDev Adapter is placed on the 40-pin header, and connects to the weather sensor with a PiicoDev Cable.

If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.

For sensors with an ASW switch: If your sensor has a switch labelled ASW, ensure it is in the OFF position before proceeding.

bme280-asw-switch

Enable I2C

Power on your Raspberry Pi. Open the Raspberry Pi Configuration Menu, select the Interfaces tab and ensure I2C is enabled.

You only need to do this step for your first PiicoDev project - from here on you probably won't have to repeat this step when using PiicoDev hardware.

raspberry-pi-enable-i2c

Install PiicoDev

Open Thonny (Pi Start Menu > Programming > Thonny IDE) and open the Manage Packages menu (Tools > Manage Packages)

open-thonnythonny-manage-packages

Search for 'piicodev' and install or upgrade if necessary.

search-for-piicodev-pypi     install-piicodev-package

Download the Example Script

Download this example script: main.py (right-click, "save link as")

Save this file wherever you like to keep your programming projects. For this tutorial I'll save mine in /home/pi/PiicoDev

Run the Example

Double click main.py to open in Thonny, then click the green "Run Current Script" button.

piicodev-example-atmospheric-data

Pictured above: The atmospheric data streams up the Shell . From left to right, the data is Temperature (degC), Barometric Pressure (hPa) and Relative Humidity (%RH).

All going well, weather data will begin streaming up the Shell window.

If you receive an error, it may be because the sensor is not connected properly - reconnect the sensor and try again.

Try uncommenting line 19, with another print statement involving altitude. This will print an approximate altitude difference from when the script started running - nice!

Using Multiple Sensors

It's possible to use up to two Atmospheric sensors connected together - they just need to be set up with different addresses. The default address for Atmospheric Sensors is 0x77 - If you've been following this tutorial this is the address being used by default when no address is specified. To add a second sensor, we need to change its address to 0x76, as follows.

For Sensors with an Address Switch ("ASW")

If your second sensor has an address switch (labelled "ASW"), switch it to the ON position. If your sensor does not have an ASW switch, follow these steps instead.

Use a pen, toothpick or other fine tip to make this easier. If your ASW switch is protected by a thin, orange film - carefully peel that off first before attempting to set the switch.

two-atmospheric-sensors-connected-together

Two PiicoDev Atmospheric Sensors being used on the same bus - this is made possible by setting each device to a unique address. The left sensor has ASW:OFF, and the right sensor has ASW:ON.

piicodev-atmospheric-sensor-address-switch

A closeup of the switch configuration.

For Sensors with no Address Switch

If your second sensor doesn't have an address switch you'll need to (carefully!) change its address. Find the ADR jumper on the back of the sensor. Cut the trace between the centre tab and the tab labelled 0x77. Next, use solder to join the centre tab and the tab labelled 0x76.

piicodev-atmospheric-sensor-adr-jumper

Code

The following code will collect and print data from each Atmospheric Sensor individually. The first sensor (with address=0x77) is initialised as sensorA. The second sensor (with address=0x76) is initialised as sensorB. Once we have two sensors initialised, we can call the values() function for each to read them individually.

# PiicoDev Atmospheric Sensor BME280 - Using multiple sensors
# Connect two sensors, each with a different address. They can be initialised and read independently.

from PiicoDev_BME280 import PiicoDev_BME280
from PiicoDev_Unified import sleep_ms # cross-platform compatible sleep function

sensorA = PiicoDev_BME280() # initialise the first sensor (defaults to 0x77). This is identical to "PiicoDev_BME280(address=0x77)"
sensorB = PiicoDev_BME280(address=0x76) # initialise the second sensor at address 0x76

while True:
    dataA = sensorA.values() # read all data from the first sensor
    dataB = sensorB.values() # read all data from the second sensor
    
    myString = "A: "   str(dataA)   "   B: "   str(dataB) # create output string with both data
    
    print(myString)
    
    sleep_ms(100)

When you run the above code, the Python Shell should print three values for A, and three values for B. These are the Temperature, Pressure and Humidity as measured by each sensor. In the first sample, we can see that sensorA reads 28.24 degrees C, while sensorB reads 28.11 degrees C.

piicodev-atmospheric-sensor-multiple-device-data

If you have any questions or uncertainty, start the discussion below. We're full-time makers and here to help!

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.