We're going to read atmospheric information using our PiicoDev atmospheric sensor and a Micro:Bit v2. We'll connect these two together and run some example code that will measure temperature, barometric pressure, and relative humidity. And using air pressure, we can even detect changes in altitude. More on that to come.
To follow along, you'll of course need a Micro:Bit v2, a PiicoDev atmospheric sensor and adapter for Micro:Bit, and a PiicoDev cable. Connect your Micro:Bit into the adapter. Make sure the buttons are facing up. Connect one end of the lead to either of the connectors on the adapter and connect the other end to your PiicoDev sensor. I'm going to use a PiicoDev platform to keep everything nice and secure. And connect to your computer with USB.
We're going to download the three files that we need for this example. Find the download section in the article, right click each link and select save link as. Save these somewhere that makes sense. I'm using a folder called PiicoDev in my documents.
Open Thonny, connect to your Micro:Bit, and navigate to where we save those three files. If you need help getting started with using Thonny for Micro:Bit, we have a guide for that too. If you prefer, you could follow along using python.MicroBit.org instead of using Thonny. We have a guide for that too. I prefer Thonny though because you get the nice plots. Linked in the article.
I'll just restart the back end and I can see that Micro:Bit has connected. I'll select all the files by selecting the first, hold shift, select the last, right click and upload to Micro:Bit. And this is all we need to do to run the example.
In the shell, press ctrl d to reboot the Micro:Bit and we now have atmospheric data streaming up the shell. It looks very busy but let's have a look at it.In the left column, we have the temperature in degrees C. In the middle column, we have air pressure in hectopascals - that's the same as millibar if you're more familiar with millibar. We have 52 percent relative humidity in the studio today.
If I move that up just a little bit, you can see that I can actually change the air pressure that we read. Down here, I'm reading about 1012.8, and up here I'm reading 1012.7. So just small changes in altitude can actually change the air pressure that you read.
That gives me an idea. I'll stop the script with ctrl c because that could be a little bit distracting, and we'll open up the main.py file that's stored on our computer. These other two files are for driving the sensor. Okay, this is our example.
So we can see the first line imports the device module that's to drive the atmospheric sensor. We also import a sleep function; this is to create a delay. We initialize the sensor calling the initialization function, and we also perform an altitude measurement and assign that to a variable called zero alt. More on that later.
In the infinite loop, we call sensor dot values and assign the results to temp c, press pa for pascals, and hume rh for humidity relative. Pressure is returned in pascals, so we convert that to hectopascals by dividing by 100, and then we just print the data.
In the print statement, we convert temp c, which is a number, into a string for easy printing, and then we just concatenate that with the unit degree c. We do the same thing for pressure in hectopascals with hpa, and finally, we concatenate the humidity with percent rh.
Now, did you know you can also infer altitude or at least changes in altitude by using air pressure. If I comment out this print statement with alt three, and I'll uncomment this.In this tutorial, we'll be exploring how to read the atmospheric conditions and measure altitude using a PiicoDev atmospheric sensor and a micro bit. We can see this is going to call sensor dot altitude to read the altitude in meters and it takes the difference with the very first altitude reading we took at the start of the script.
If we save this file and then right-click to upload it to the Micro:Bit, we'll overwrite the file in the Micro:bit and press control d. Now, we're reading a rather noisy altitude. By noisy, we mean that it's fluctuating around a little bit.
The driver for this sensor actually includes some filtering capability. When we initialize the sensor, we can add the argument "iir," which is an infinite impulse response. It's just a type of filter, and we'll pass that the parameter three, which is the amount of filtering.
Larger numbers provide smoother results, but they're slower to respond. If we stop the script, save our file with control s, upload again, and reboot with control d, we can see that the data is jumping around a lot less. If we raise the sensor up, the altitude actually climbs, and if we bring it back down, the altitude should fall again.
It's pretty amazing that we're able to detect very small differences in altitude from just the air pressure. Of course, if you blew on the sensor or sucked air through a straw over it, you could change that reading dramatically as well, so give it a go.
So there you have it, reading some atmospheric conditions and even measuring altitude. If you make anything cool out of this starter project, it'd be great if you shared it on the Core Electronics forums. That's also the best place to get any technical advice for these tutorials. Until next time, thanks for watching.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.