Let's connect our PiicoDev Precision Temperature Sensor TMP117 to a Micro:bit and perform some temperature readings. This tutorial will guide you through wiring, uploading example MicroPython code, and remixing the example to include new functionality.

Transcript

Let's measure temperature using a PiicoDev Precision Temperature Sensor and a Micro:bit V2. We'll connect these two together, get an example working, and then remix the example a little bit to do some decision making. Let's get started.

To follow along, you'll need a Micro:Bit. a V2 is best, a PiicoDev Precision Temperature Sensor and adapter for Micro:Bit. and a PiicoDev cable to hook them together. Connect the Micro:bit to the adapter with the buttons facing up. Plug your PiicoDev cable into one of the connectors on the adapter, and connect the other end to your temperature sensor. I'm going to use a PiicoDev platform to keep everything in place. And connect to your computer with a USB lead.

In the article for this tutorial, find the download section and right-click each link, then select Save Link As. I'm saving mine to a PiicoDev folder I've made in My Documents. Open Thonny, go to the Run menu, and select Interpreter. Make sure that we're on BBC Micro:Bit. and make sure that MicroPython is installed. You can see the file panes with View, Files, and you can also show the plotter, which we'll need later, with Plotter. If you prefer working on python.MicroBit.org, check out our guide for that too.

The workflow for each PiicoDev project is pretty similar. In the File pane, navigate to where you downloaded your files earlier. Click main.py and Shift-click to select All Files, and then Upload to Micro:Bit. And immediately, we're ready to go.

If you press Ctrl-D to reboot the Micro:Bit. we can immediately see that we have some temperature streaming up the shell, and being plotted in the plotter on the right. If I cover the sensor with my thumb, I should be ableTo warm it, and we can see the temperature rises from about, I don't know, 23-degrees up to nearly 28-degrees. And then when I take my thumb away, it falls back down.

Press Ctrl-C to stop the script, and we'll open the file that's being stored on the Micro:Bit. So under the Micro:bit menu, we'll open main.py. This is our example. The other two files are just drivers for this sensor.

Open main.py, and we'll have a look at what's going on. We first import the device driver, and then import the sleep function. This allows us to create a delay.

We initialize the temperature sensor using the initialization function. So here, we're just assigning tempSensor as the sensor. And then in an infinite loop, we call tempSensor.readTempC for Celsius, tempF for Fahrenheit, and tempK for degrees Kelvin, if you prefer. And we assign each of those to a variable tempC, tempF, tempK.

Finally, in the print statement, we convert tempC, which is a floating point number, into a string and concatenate that with degreesC. And that's how, in the plot, we can print the temperature followed by degreesC. And then there's a one-second delay.

Now, if instead of printing tempC, we print tempF, and we can change that unit to degreesF, then we can save the script with Ctrl-S, reboot with Ctrl-D, and now the shell is printing degrees Fahrenheit. Nice.

I'll stop the script and use Ctrl-Z to undo that change. I'm more used to working in degrees Celsius.

Now let's do some decision making. I can put in an if statement and say, if tempC is greater than, I don't know, what are we working with? Maybe 26-degrees? If tempC is greater than 26, then we can print, it's too hot. Ctrl-S to save.Ctrl-D to run. And you can see we're printing 22.5 degrees. And as I warm that sensor, it crosses 26-degrees and prints, it's too hot.

And then if we let that cool down again, then we're, we dip below 26-degrees and the print statement goes away. Nice.

We've been directly editing the file that's stored on the Micro:Bit. Remember, we opened it from down in the Micro:Bit menu. You can tell because the main.py file name is wrapped in these square brackets.

If we open the original file that we downloaded, it's still the default. It's just like we downloaded.

To save our changes from the Micro:Bit to our computer, we can right-click main.py on our Micro:Bit and download to our computer. And that's going to overwrite the file already on our computer. Now, when I open that one up, it's populated with the changes we just made.

There you have it, reading temperature and doing a little bit of decision making using a PiicoDev Precision Temperature Sensor and a Micro:Bit V2.

If you make anything cool from this starter project, we'd love for you to share it on the Core Electronics forums. And that's also the best place to go if you have any 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.