Let's read atmospheric data like temperature, humidity, and air pressure from our PiicoDev atmospheric sensor using a Raspberry Pi Pico. I'll show you how to connect these two together, run some example code to get those readings, and did you know you can also infer changes in altitude based off air pressure? Really easy, I'll show you how we do that as well. Let's get started. To follow along, you'll need a Raspberry Pi Pico with pins soldered facing down, a PiicoDev atmospheric sensor and expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect everything together. Start by connecting your Pico to the expansion board, making sure that the USB connection is on the same side as the two-pin battery connector. You can double-check that pin number zero on the expansion board is to the left side of that USB connector. Connect your PiicoDev cable into the port at the bottom of the board and connect the other end to your atmospheric sensor. I'll use a PiicoDev platform to keep everything nice and secure. And connect to your computer with a USB lead. Find the download section in the article for this tutorial and download the three files you'll need. Right-click each link and select save link as. Save them somewhere that makes sense, I'm using a folder that I've made in my documents folder. Open Thonny and use the file pane to navigate to where you saved your files. If you need help getting started with Thonny, we have a tutorial for that too. Just briefly, the PiicoDev unified file and the BME280 file are drivers for driving these PiicoDev modules. The main.py file is the user code for this project. Upload all three files to your Pico with a shift-click, a right-click, and an upload to. And now we can see the three files appear on our Pico. Press control D to restart the Pico and we should immediately see weather data streaming.Up the shell. So, in the studio today it's 18-degrees, the air pressure is at about 1004 hectopascals, which is the same as millibar. And we're at 70% relative humidity. Press control C to stop the execution of the script and we'll edit the file that's directly on the Pico. So coming down to the Raspberry Pi Pico menu, double click main.py. And this is the example code for this tutorial. We start off by importing the PiicoDev atmospheric sensor module and also a function to sleep so that we can create a delay. We initialize the sensor using the initialization function and also zero altitude. So we take an altitude reading from the sensor and we assign that to a variable called zero alt. More on that later. In the infinite loop, we just read temperature in degrees C, pressure in pascals, and humidity in percent relative humidity by calling the sensor.values function. Remember that sensor is the instance of our atmospheric sensor. Then we do a little conversion to convert from pressure in pascals to pressure in hectopascals by dividing it by 100. And then we just print the data to the console. So you can see this print statement, we print temperature C, but we convert it to a string first and then concatenate it with degrees Celsius. And we repeat that for pressure and for percent relative humidity. Finally, there's just a 100-millisecond sleep. So what's going on with this zero altitude here? Well, in the second part of the infinite loop, we have a different print statement. If I uncomment that with alt 4 and I'll comment out the first print statement with alt 3, that means that our plot will look a bit nicer. We can take a look at what's happening in this print statement. We're once again calling sensor.altitude, which we were calling before in the initialization. But this time, we're taking the difference of the current altitude with the zero altitude.So it's like we're taking the relative altitude measurement from when we first started the Pico, when we first started executing the script. If we save the script with control S and then reboot with control D, now we're reading difference in altitude. And you can see there's a little bit of noise in the measurement, but in general the line is quite flat. Once this rescales, we can see the noise. It looks like it's between 25 centimetres and negative 25 centimeters, maybe up to half a meter in either direction. We can actually initialize the sensor with a little bit of filtering. When we initialize the sensor, we can pass it the argument IIR, and that's for an infinite impulse response filter. And I'm going to set the IIR filter parameter to three. I'll save that again. And when we rerun the script, there's a lot less jagged noise in the signal now. And that's going to be really useful because now if I raise the sensor, the scale's just rescaled, but if I raise the sensor, we can see that line trends upwards. And then as I lower it, we can see it trends back down. That's pretty amazing. We just raised the sensor by what, half a meter or something? Maybe we can see. We're at about 25 centimeters, and now we're at about, where does it become stable? We're at about 90 centimeters. And that seems about right. That means that we can infer altitude differences of less than a meter using just air pressure. We've been editing the file that is on the Raspberry Pi Pico, and you can tell because the main.py title is wrapped in these square brackets. Square brackets means it's on the device. If we open the file that's on our computer, it still looks like it did when we downloaded. We're still just running the first print statement. If you want to save your changes, then you can stop the script with control C, right click that main.py file.To download the file from our Pico, we simply need to click the "Download" button in Thonny and save it to our local computer. We'll get a warning that we're going to overwrite the existing file, but that's okay for now. When we open the file on our computer, we'll see the changes that we created. The first print is commented and the second print is uncommented. That's it! We've successfully downloaded the file from our Pico and can now see the atmospheric data we collected using a PiicoDev atmospheric sensor and Raspberry Pi Pico. If you make something cool with this starter project, or if you have any questions, let us know over on the Core Electronics forums. Thanks for watching!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.