G'day, I'm going to show you how to get started reading atmospheric data from your PiicoDev atmospheric sensor on a Raspberry Pi. We'll hook these up together, download the packages we need, and get some atmospheric data. That's temperature in degrees C, the barometric pressure, and also percent relative humidity. Let's get started.
To follow along, you'll need a Raspberry Pi. I'm using a Raspberry Pi 4 today, a PiicoDev adapter for Raspberry Pi, of course the PiicoDev atmospheric sensor, and a PiicoDev cable. I recommend cables 100mm or longer for Raspberry Pi projects. You'll need to have your Raspberry Pi set up to run like a desktop computer. If you're unsure how to get started, check out our Raspberry Pi workshop for beginners.
Connect your adapter to the Raspberry Pi's GPIO header, making sure that the Ethernet arrow is pointing towards the Ethernet jack on a Raspberry Pi 4, or it could be pointing to the USB sockets on a Raspberry Pi 3. Plug one end of your PiicoDev cable into any of the ports on the adapter, and connect the other end to your atmospheric sensor. And I've just mounted everything to a PiicoDev platform to keep it stable for this tutorial.
Make sure you have the I2C interface enabled on your Raspberry Pi. Go to the Pi menu, Preferences, Raspberry Pi Configuration. Under the Interfaces tab, make sure you have I2C enabled, and navigate to this tutorial article.
Continue down the article until you find the download link for the example script. Right click and Save Link As, and I'm going to save that to the PiicoDev directory that I've created in my home directory. And I'll rename this atmospheric.py just for some context.
Open the File.Manager, and navigate to where you saved your file. Double click it, and that should open in Thonny.
Let's make sure we have the PiicoDev packages installed. Go to Tools, Manage Packages, and then search for PiicoDev with two I's. That's our link. And just make sure you have it installed or upgrade to the latest version if you need to.
And this script is ready to run. Just click the Run Current Script button, that's the green play button, and you should have atmospheric data streaming out the console. If I halt the script, we can look at it a bit more closely.
We have temperature in degrees C, we have barometric pressure in hectopascals or HPA, and we have percent relative humidity. Let's take a tour of the code and see what's happening.
On the first line, we import the functionality to drive the sensor, and we also import a module to create a delay. We initialize the sensor and just call it sensor. We're initializing the atmospheric sensor. And we also take an altitude reading and set that to zero. More on that later.
And then in the infinite loop, we call sensor.values, and that reads back the temperature, air pressure, and humidity. And they get saved into three variables here. Temp C, press PA for pascals, and humidity, and that's relative, percent relative humidity.
The next line just converts pascals into hectopascals, which is the same as millibar, if you're more comfortable working with millibar. And then we print the data.
We print temp C, but we convert that number to a string, and we concatenate it with the degree C unit. And we repeat that for pressure in hectopascals and percent relative humidity.
I've connected a much longer PicoDevHere's the formatted transcript:
Lead for this next bit. Hopefully, I can show you the altitude demo. I'll comment out this print statement with alt 3, or just inserting a hash at the beginning of the line. And I will uncomment this line by removing the hash.
Now, when we run the script, the number that's on the in the console here is the altitude in meters. And if I open up this plot, you can see that there's a bit of noise.
It seems to be oscillating around, you know, between zero and negative 0.5 meters, which is the difference from when it started. If I raise it up, that should trend upwards. And then if I pull it back down again, it's going back down. It is quite noisy, though.
The driver for this device does support some inbuilt filtering. If we go to where the sensor is declared, we can insert an argument IIR equals three. Now, when we run the script, there should be significantly less noise. However, it will be slower to respond.
We're settling at about negative 0.5 meters. If I raise this up, not very much. This is about half a meter that I've raised it. And you can see the difference has been about half a meter. And then if I lower it down again, that altitude should fall. Now, that's pretty amazing.
We were able to infer that altitude was changing over just such a small difference. And that's a pretty impressive demonstration that you can infer altitude or at least changes in altitude from air pressure.
So there you have some quick readings from your PiicoDev atmospheric sensor. We read temperature, pressure, humidity, and we also did some altitude readings as well. If you have any questions, just leave a comment at the bottom of this article or open a thread.On the Core Electronics forums.
We're full-time makers and here to help.
Catch you next time.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.