Let's connect our PiicoDev Precision Temperature Sensor to a Raspberry Pi Pico and perform some temperature readings. This tutorial will guide you through wiring, uploading example MicroPython code in Thonny, and remixing the example to include new functionality.

Transcript

Let's read temperature data from our PicoDev Precision Temperature Sensor using a Raspberry Pi Pico. We'll connect these two together, run some example code, and then even remix that code to change how it behaves. Let's get started. To follow along, you'll need a Raspberry Pi Pico with pins soldered facing downwards, a PicoDev Precision Temperature Sensor, an expansion board for Raspberry Pi Pico, and a PicoDev cable.

First plug the Pico into the expansion board, making sure that the USB connector shares the same side as the larger battery connector. Pin 0 on the expansion board should be to the left of the USB connector. Connect your PicoDev cable to the PicoDev connector at the bottom, and connect the other end to your temperature sensor. Now use this PicoDev platform to keep everything in one place. And connect your Pico to your computer with a USB cable.

In the article for this video, find the three files that we need to download. Right click each link and select Save As. Save these somewhere that makes sense. I'm using a directory called PicoDev in my documents. Open up Thonny and navigate to the directory where you saved your project files. If you need help getting started with Thonny, we have a tutorial linked in the article. Select all three files by shift clicking and Upload To. And you'll see them appear on the Raspberry Pi Pico. Now if I restart the Pico with Ctrl D, we'll see some temperature data being printed up the shell, and also a plot showing the temperature data on the right. If I warm up my hands and touch the temperature sensor, we should be able to change the temperature that's being plotted. You can see it bumps up when I touch it with my thumb, and then if I release, it slowly decreases again.

Let's take a tour of the code for this project. The PicoDev Unified Library and the PicoDev TMP117 package are just for driving the device. The user code is in main.py. If we stop the device and double click the file on the Pico, we can edit the file that's directly on the Pico. Let's take a look at what's going on. We start by importing the device module, that's PicoDev TMP117, and also importing the sleep function so we can create a delay. We initialize the temperature sensor using a variable called temp sensor, and just calling the initialization function. Then in an infinite loop, we read the temperature in degrees C, in degrees F, and degrees K, or Kelvin. So Celsius, Fahrenheit, and Kelvin from each of these three different functions. And then we print just the temperature as degrees C. So here we take that number, turn it into a string using the str function, and concatenate that with degrees C. That means that if I change this string that gets printed to temp F, and I'll change this unit as well, I can save that with control S, reboot my Pico with control D, and now the temperature is being printed in degrees Fahrenheit. You can do the same with Kelvin as well. I'll stop the code again with control C in the REPL, and I'll revert those changes. I'll just use control Z to undo back to C, because I'm used to working with degrees Celsius.

Now let's remix the code. Then we can insert an if statement, or an if condition, if temp C is greater than, let's go for 22 degrees, then we can print, it's too hot. I'll save and reboot the Pico with control D. So we're at about 21.9 degrees, and if I warm my hands up and heat that sensor just a little, it crosses over 22 degrees, and we get the message, it's too hot. Now of course we've been modifying the file directly on the Pico, and you can tell because the name of the file is wrapped in these square brackets in the top tab here. If I open up main.py that's stored on my computer, you can see that it's still the original file that we downloaded. So if you want to back up your file from your Pico, you can always select that main.py file, and download it to your working directory. And it'll show you that you're about to overwrite that file with one that is just a little bit bigger, and that makes sense, we just added a few lines of code for that print statement. So I'll hit okay, and now my main.py on my computer is backed up and it's in the same state as the one on my Pico. So there you have it, reading temperature using a Raspberry Pi Pico and a Picodev temperature sensor.

If you make something cool out of this little starter project, we'd love it if you shared it over at the Core Electronics forums. It's also the best place for technical questions. 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.