Let's use a PiicoDev Pressure Sensor to measure barometric pressure with a Raspberry Pi Pico. Did you know you can also infer altitude from air-pressure? We'll modify the example to measure altitude, correcting for local weather conditions.

Transcript

Let's measure barometric pressure using a PiicoDev pressure sensor and a Raspberry Pi Pico. We'll connect these two together and get some example code working to measure barometric pressure and then we'll even infer altitude using barometric pressure. Let's get started.

To follow along you'll of course need a Raspberry Pi Pico, this with the pin soldered facing down, a PiicoDev pressure sensor and expansion for Raspberry Pi Pico, and a PiicoDev cable. First plug your Pico into the expansion, making sure that the USB connector is on the same side as the two pin battery connector. You can double check that pin number zero is to the left of the USB connector. Connect your PiicoDev cable to the socket at the bottom of the board and connect the other end to your pressure sensor. And I'll secure everything in place on a PiicoDev platform. And connect to your computer by USB.

Now for the device driver files and the example code for this tutorial. Find the download section in the article, right click each link and select save link as. Save these three files somewhere that makes sense, I'm saving mine to a PiicoDev folder in my documents. Open Thonny, find where you saved those documents and also connect to your Pico. If you need help getting started with Thonny for Pico we have a guide for that too.

Here we have main.py, that's the example code for this tutorial, and these other two files are device drivers to drive the pressure sensor. Select all three files by clicking on the first, hold down shift, and then select the last. We'll right click and upload to. And they will upload to our Raspberry Pi Pico you can see in the bottom here.

This script is ready to run, so in the shell just press ctrl d to reboot your Pico and you should see some atmospheric pressure data streaming up the shell in HPA or hectopascals. That's the same as millibar.If you're more familiar with millibar, using this rubber straw, I can place it over the pressure sensor. If I blow gently, I can increase the pressure, and if I suck gently, I can decrease the pressure. Make sure you're going to use a straw with a smaller diameter than your pressure sensor. If you break it off somehow, you don't want it coming up the straw.

Let's take a tour of the example code. I'll halt the script with ctrl c and we'll load the file that's saved on the Pico. So under the Raspberry Pi Pico menu, we can double click main.py. You can see on the first line we import the driver to run the pressure sensor, and we also import a sleep function to sleep in milliseconds. We initialize the pressure sensor as pressure, and then in the infinite loop, we call pressure.readpressure and store that in a variable called pressHPA for pressure in hectopascals.

We also call the readaltitude function and store that in altitudeM. Next, in the print statement, we convert the floating-point number pressHPA to a string for easy concatenation with the unit HPA. That's how in the shell, we can print the number and the unit together in the single statement. If I comment this line out with alt 3 and I uncomment this other print statement with alt 4, we can see that we can print the altitude in meters.

Let's give that a go. Save the script and press ctrl d. Now I'm getting about negative 35 meters altitude, and I'm clearly not below sea level here in Adams Town, New South Wales. I'd be underwater. So what's going on here? We're inferring altitude from air pressure, and that means that the altitude reading that you get will differ based on the local weather conditions.

I've just performed an internet search for what the air pressure at sea level is in Newcastle, Australia, and I can see that the current air pressure at sea level is 1019 hectopascals.Returning to the script, I can add that as an argument to the read altitude function. Pressure underscore sea underscore level equals 1019.

If I stop the script and save this change, and then rerun the script, now I'm getting something that's a lot more sensible. I've adjusted for the local weather conditions in Newcastle, Australia to correct the altitude, and this is about right.

Now we've been modifying the file that's saved directly on the Pico. If you make changes to the file that's saved on the Pico, and you'd like to save them to your computer as well, you can always stop the script, right-click the file that you've modified, and download it back to your computer, and that will overwrite the example as it was when we downloaded it.

So there you have it, reading barometric pressure, and even altitude, using 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 to ask for any technical help with this project.

Until next time, see you later.

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.