Let's measure barometric pressure using a PiicoDev pressure sensor with our Micro:Bit V2. We'll connect these together, get some example code working to measure barometric pressure, and we'll even be able to measure altitude using air pressure. Let's do it.
We're going to program our Micro:Bit using Thonny today. If you've never used Thonny with Micro:Bit before, we have a getting started guide for that. And if you prefer using microBit's online MicroPython editor, we have a guide for that too. You can still follow along with this tutorial, the steps will be very similar.
Now to follow along you'll of course need a Micro:Bit V2, a PiicoDev pressure sensor and adapter for Micro:Bit, and a PiicoDev cable. Connect your Micro:Bit to the adapter, making sure the buttons are facing up. Plug your PiicoDev cable into one of the sockets, and connect the other end to your pressure sensor. And I'll use a PiicoDev platform today to keep everything in one place. And connect to your computer with a USB cable.
In the article for this tutorial, find the download section and right click each of the three links we need to save and select save link as. This will download the main.py example and two driver files. Save these somewhere that makes sense, I've saved mine to a PiicoDev folder that I've made in my documents. Open Thonny, navigate to where you saved your files in the file pane, and also make sure that we're running the Micro:Bit interpreter.
Select the first file and hold shift, select the last file, right click and upload to microBit. And we are ready to run. In the shell, press control D to reboot the Micro:Bit, and you should see air pressure data in hectopascals streaming up the shell. That's the same as millibar if you're more familiar with millibar. We also have a plot off to the right showing that data.
Now using a thin rubber tube, if I cover the sensor hole on the top of our PiicoDev pressure sensor and we watch the altitude data in the plot pane, we can see that it decreases as we cover the hole because the air pressure is increasing, simulating a higher altitude. A very, very cool feature.
So now you know how to measure barometric pressure and altitude with a Micro:Bit and PiicoDev breakout.
Let us know if you have any questions.That pressure sensor and blow or suck gently, I can increase the pressure and decrease that pressure. If you are going to do something like this, make sure the diameter of this straw is much smaller than the sensor itself. You wouldn't want to accidentally suck it off the board.
Let's have a look at the code. Press control C to stop the code and we'll open the file that's saved on the microBit. Double click main.py to open. And we can see the first line, we import the device driver. The second line, we import a function to sleep in milliseconds. And then we initialize the sensor as an object called pressure.
In the infinite loop, we call pressure.readpressure to read the barometric pressure. And we assign that to a variable called pressHPA. We also call read altitude and assign that to altitudeM. Next in the print statement, we convert pressHPA to a string so we can concatenate it easily with the unit HPA. And that's how we get this nice print statement where we have the floating point number for the pressure and then the unit HPA.
Now, if I comment out this print statement with alt three, we can uncomment the next one with alt four, and that will now print the altitude above sea level in meters. Let's save the script and reboot. And now we're reading the altitude above sea level in meters.
And you might notice this is actually reading negative. What's going on here? I'm clearly not underwater. Well, since we're inferring altitude from air pressure, the local weather conditions will actually affect this reading. I've just performed a search for the air pressure at sea level in Newcastle, Australia, and I can see that it is 1019 hectopascals. So I can actually pass that as an argument into the read altitude function to correct for the current air pressure at sea level. That's pressure underscore sea underscore level equals 1019.Now, if I stop the script, save my change and reboot, now we read something that makes a little more sense. Now we're getting about 16 meters above sea level. And I think that's about right.
We've been modifying the file that's stored directly on the micro bit. If you'd like to save those changes onto your computer to make a backup, you can always right click that file and download to. This will download the file from the Micro:bit to the directory on your computer.
And now we've overwritten the file in our PiicoDev directory. So there, you have it, reading barometric pressure and also inferring altitude from air pressure using a Micro:bit and a PiicoDev pressure sensor.
If you make anything cool from this starter project, we'd love for you to share it on the Core Electronics forums. That's also the best place if you have any technical questions.
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.