Introduction
This guide will help you read barometric pressure data from your PiicoDev® Pressure Sensor with a Raspberry Pi single-board computer.
To follow along, it's best to have:
- A Raspberry Pi (model 3, 4, Zero or Zero W) This tutorial will use a Raspberry Pi 4, Model B
- A PiicoDev Pressure Sensor MS5637
- A PiicoDev Adapter for Raspberry Pi
- A PiicoDev Cable (100mm or longer is best for Raspberry Pi)
In the video, we use a PiicoDev Platform to keep everything safe and secure.
For this tutorial, we'll assume you're familiar with using a Raspberry Pi like a desktop computer - connected to a keyboard, mouse, monitor and with an internet connection. If you need help getting started, have a look at Chapter 1 of the Raspberry Pi Workshop for Beginners.
If you prefer not to use the Expansion Board for Raspberry Pi, there are other connection options in our PiicoDev Connection Guide.
Contents
- Connect the PiicoDev sensor to your Pi
- Enable I2C
- Install / Update PiicoDev
- Download the Example Script
- Example
- Remix
Connect the PiicoDev sensor to your Pi
Mount the PiicoDev® Adapter on your Raspberry Pi and plug connect your pressure sensor to the adapter via the PiicoDev cable.
The adapter connects to the 40-pin GPIO header on the Raspberry Pi - ensure the header is mounted correctly, with the "Ethernet" label on the same side as the Pi's ethernet adapter.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Pictured above: A Raspberry Pi 4 Model B with USB Power and HDMI (two white leads). The PiicoDev Adapter is placed on the 40-pin header and connects to a sensor with a PiicoDev Cable.
Enable I2C
Power on your Raspberry Pi. Open the Raspberry Pi Configuration Menu, select the Interfaces tab and ensure I2C is enabled.
You only need to do this step for your first PiicoDev project - from here on you probably won't have to repeat this step when using PiicoDev hardware.
Install PiicoDev
Open Thonny (Pi Start Menu > Programming > Thonny IDE) and open the Manage Packages menu (Tools > Manage Packages)
Search for 'piicodev' and install or upgrade if necessary.
Download the Example Script
Download this example script: main.py (right-click, "save link as")
Save this file wherever you like to keep your programming projects. For this tutorial, I'll save mine in /home/pi/PiicoDev and change the name to pressure.py to give it some context.
Run the Example
Open the example script in Thonny and run the script with the green play button. (Hint: View the files menu with View > Files). You should see barometric pressure measurements in hectopascal, which is equivalent to millibar - a commonly used unit for describing barometric pressure: 1hPa = 100Pa = 1mbar.
Above: Pressure data streams up the Shell, while the Plot shows historic data.
Remix
Did you know you can also infer altitude from air pressure? Comment out the pressure print statement and uncomment the altitude print statement. Run the script again and now the Pico will infer altitude-above-sea-level from air pressure. There can be a bit of error here since your local weather conditions will affect the reading. You can optionally pass in the local air pressure at sea level to the read_altitude() function to correct for weather effects. You can find the current air pressure at sea level for your area with an internet search.
Conclusion
We can now measure air pressure from our PiicoDev Pressure Sensor, and use it to infer altitude.
If you make something cool with this starter project we'd love for you to share it on our forums! And if you'd like some help with this guide, start the conversation below - We're full-time makers and here to help!