This guide will help you read weather data (Temperature, Air Pressure and Humidity) from your PiicoDev Atmospheric Sensor and a Micro:bit.
To follow along, it's best to have:
- A Micro:bit v2 (v1 does not have enough memory for this project)
- A PiicoDev Atmospheric Sensor
- A PiicoDev adapter for Micro:bit
- A PiicoDev Cable
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
If you prefer not to use the Micro:bit adapter, there are other connection options in our PiicoDev Connection Guide.
Connect the PiicoDev sensor to your Micro:bit
Plug your Micro:bit into the PiicoDev adapter (buttons LED matrix facing up), connect your sensor to the adapter via the PiicoDev cable and connect your Micro:bit 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 PiicoDev Modules and Example Code
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 this PiicoDev Sensor
It will be best to keep this file wherever you like to keep your coding projects eg. Documents > PiicoDev
Open Thonny, connect to your micro:bit and upload the three files from the previous step.
Press Ctrl D to restart your micro:bit and run the main script - weather data should begin streaming up the shell
Above: Atmospheric data is streaming up the Shell. From left to right: Temperature (degC), Barometric Pressure (hPa) and Relative Humidity (%)
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!