Let's connect our PiicoDev Precision Temperature Sensor to a Raspberry Pi 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

I'm going to show you how to read temperature using a Raspberry Pi and a PiicoDev Precision Temperature Sensor. We'll read the temperature, change the units from Celsius to Fahrenheit, do a little decision making and see some nice plots. Let's get started.

To follow along, you'll need a Raspberry Pi, a PiicoDev Precision Temperature Sensor, a PiicoDev Adapter for Raspberry Pi and a PiicoDev Cable. I recommend cables 100mm or longer for Raspberry Pi projects. It's also best if you're comfortable using a Raspberry Pi like you would a normal desktop computer. If you need help with that, check out the Raspberry Pi for Beginners Workshop.

Let's assemble the parts. Connect your PiicoDev Adapter to the Raspberry Pi, making sure the Ethernet arrow is pointing towards the Ethernet connector, or the side of the board that the Ethernet connector is on. Next, plug your PiicoDev Cable into any of the PiicoDev connectors on the adapter, and connect the other end to your temperature sensor. And I've just mounted everything to this PiicoDev platform for Raspberry Pi to keep it secure for the rest of this tutorial.

With your Raspberry Pi powered up, 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.

Navigate to the article, find the example code for this tutorial, right click the main.py link, and select Save Link As. I'll save this to a new directory under my user home directory, and I'll call that PicoDev. I'll also rename the file to temperature to give it some context.

Double click the file you just downloaded to open it in Thonny.Let's make sure we have PiicoDev installed. Go to Tools, Manage Packages, and then in the search box type PicoDev, that's with two I's. There's our result, and just make sure you are running the latest version by either clicking Install or Upgrade.

This script is ready to run, so all we need to do is click the green Run Current Script button, and we should see some temperature streaming up the shell. And if I warm up my hands and touch the sensor, I should be able to change the temperature that's being displayed.

If you don't see this plot on the right hand side or this file tree on the left, you can always turn them on by going to View Files and View Plotter.

Let's take a quick tour of the code. The first lines are just to import the drivers to run the temperature sensor, and also a function that allows us to create a delay so our script doesn't run too fast. We create an instance of the temperature sensor, and then in an infinite loop, we just read the temperature in degrees Celsius, Fahrenheit, and Kelvin. And here we're just printing temperature in degrees Celsius.

But if I copy that line and paste it, and I'll just comment this one out with a hash, then we can print the temperature F for Fahrenheit, change the units, change the units, and hit run again. And now the units have been changed to degrees Fahrenheit. I'll undo those changes because I'm more used to working in degrees C.

Let's create a little bit of logic in our script. It looks like we're sitting at about 20-degrees Celsius. So I'll include an if condition or an if statement. If temp C is greater than, let's say, 21-degrees to make it nice and easy, then we can print it's hot.If we run the script again, warm up my hands and heat up the temperature sensor, the print statement is now saying every loop that the temperature is high, it's saying it's hot. And then as this cools down again, that message should disappear. Getting closer.

And as we dip below 20-degrees, the message it's hot disappears. And there you have it, a quick example script to read temperature in different units using the PiicoDev precision temperature sensor and a Raspberry Pi.

If you have some questions, then just leave a comment at the bottom of this article. We're full-time makers and here to help. Catch you next time.

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.