Let's use a PiicoDev Pressure Sensor to measure barometric pressure with a Raspberry Pi. 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. We'll connect these two together, download some example code to measure pressure, and then we'll even modify that to measure altitude using that air pressure. Let's get started.

To follow along, it's best to know how to use a Raspberry Pi just like a desktop computer connected to the internet. Check out our workshop if you need help getting started.

To follow along, you'll of course need a Raspberry Pi. Today I'm using a Raspberry Pi 4B, a PiicoDev pressure sensor and adapter for Raspberry Pi, and also a PiicoDev cable. 100 millimetres or longer is best for Raspberry Pi projects.

Connect your adapter to your Raspberry Pi GPIO header, making sure that the ethernet arrow is pointing towards the ethernet socket on a Raspberry Pi 4. On a Raspberry Pi 3B, this will be the USB socket instead. Connect your PiicoDev cable to one of the sockets in the adapter and connect the other end to your pressure sensor. And I've just mounted everything to this PiicoDev platform to keep it nice and stable for this tutorial.

Now it's time to plug in and power up. Once your Pi is powered on, navigate to the tutorial for this article and download the example script. Find the link, right click, and select save as. I'll rename this to pressure.py just to give it some context. I'm saving this to a PiicoDev directory that I have in my home folder. In the file manager, navigate to where you saved your script, right click, and open Thonny Python IDE.

Before we get started, I'll go to the tools, manage packages menu, and just make sure we have PiicoDev installed. Just use the upgrade or install button if necessary. My PiicoDev package is already up to date, so I'll just press the green run current script button and we can see we have barometric pressure data streaming up the shell.It looks like I've got about 1015 hectopascals, which is the same as millibar if you're used to working with millibar. Using this thin rubber tube, if I suck or blow on the pressure sensor, very gently, there I sucked and the pressure went down. And if I blow, the pressure will go up a bit. If you do do this, make sure that your tube is so thin that you can't accidentally suck the pressure sensor off the board.

I'll stop the script and let's take a look at what it's doing. The first line, we import the package to read from the sensor and then we import a sleep function to sleep in milliseconds for a delay. We initialize the pressure sensor and call it pressure. And then in the infinite loop, we call pressure dot read pressure and assign that to a variable press HPA for hectopascals. We also call pressure dot read altitude. More on that in a bit. In the print statement, we convert the pressure in hectopascals to a string from a number to a string, so that we can concatenate it with the unit HPA. And that's how we get this nice printout where we have the floating point number and then HPA following.

Now, if I comment out this print statement with alt 3 and I uncomment the next one with alt 4, we can see that now we'll print that altitude in meters measurement. So let's give that a run. I'll just press control R to run. Now something's going on here. I know I'm definitely not below sea level, but I'm reading a negative altitude value. What's going on here?

Well, since we're inferring altitude from air pressure, that means that the altitude we read will actually be changed by the current local weather conditions. I've just performed a search for sea level pressure, Newcastle, New South Wales, that's where I am. And I can see that the mean sea level pressure is a thousand and eighteen hectopascals. We can use this information to adjust the altitude measurement that we're receiving from the sensor.This means sea level pressure as an argument for the read altitude function to account for any local weather conditions. When we call read altitude, pass in pressure_sea_level equals, and it was one thousand and eighteen hectopascals.

Now, if we rerun the script, we get a more sensible value of around 20 meters. So there you have it - reading barometric pressure, also inferring altitude from that pressure and offsetting for local weather conditions, all using a PicoDerf pressure sensor and a Raspberry Pi.

If you make something cool out of this little 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 that you need help with.

Until next time, thanks for watching.

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.