This guide will help you read atmospheric data from your PiicoDev® Atmospheric Sensor and a Raspberry Pi Pico
To follow along, it's best to have:
- A Raspberry Pi Pico with pins soldered (pointing down)
- A PiicoDev Atmospheric Sensor BME280
- A PiicoDev Expansion Board for Raspberry Pi Pico
- A PiicoDev Cable
If you prefer not to use the Expansion Board for Raspberry Pi Pico, there are other connection options in our PiicoDev Connection Guide.
- Connect the PiicoDev Sensor to your Pico
- Download the PiicoDev modules and example code
- Example Code
- Using Multiple Sensors
Plug your Pico into the Expansion Board, connect your atmospheric sensor to the Expansion Board via the PiicoDev cable, and finally connect your Pico to your computer with a USB lead.
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.
Download the following files and save them to your working directory (Right Click > "Save Link As")
- PiicoDev_Unified.py - The PiicoDev Unified Libraries: Drives I2C communications for PiicoDev modules
- PiicoDev_BME280.py - The device driver.
- main.py - an example script for the PiicoDev Atmospheric Sensor
It will be best to keep this file wherever you like to keep your coding projects eg. make a directory Documents > PiicoDev
Open Thonny, connect to your Pico and upload the three files from the previous step.
Press Ctrl D to restart your Pico and run the main script - atmospheric data should begin streaming up the shell
If you're unfamiliar with Thonny, read our setup guide.
With your code running, you should see atmospheric data being printed in the shell. The sensor is capable of measuring temperature, air pressure and humidity. In the example above, the temperature is about 28 degrees Celsius, 998 hPa (or "milli-bar" if you prefer), and about 39% Relative humidity. If you get an error message, it could mean your Atmospheric Sensor has been set to an alternative address. See Using Multiple Sensors for help changing the address.
Comment-out the first print statement, and uncomment the Altitude print statement. Then save main to your Pico and restart with Ctrl D. The shell now displays an altitude as a difference from when we started the script. Raise the sensor about a meter and you ought to see this altitude figure increase by about a metre too!
Open the Plotter (View > Plotter) to see a nice plot of your altitude data, though it may be quite noisy. We can include some noise filtering by initialising the sensor with an additional argument, iir=3. Larger numbers will filter noise better, but the filter will be slower to respond to changes in the data too.
sensor = PiicoDev_BME280(iir=3)
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 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.
A closeup of the switch configuration.
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.
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.
If you have any questions or uncertainty, start the discussion below. We're full-time makers and here to help!