Introduction
This guide will help you read temperature data from your PiicoDev® Precision Temperature Sensor and a Raspberry Pi single-board computer
To follow along, it's best to have:
- A Raspberry Pi (model 3, 4, Zero or Zero W) This tutorial will use a Raspberry Pi 4, Model B
- A PiicoDev Precision Temperature Sensor TMP117
- A PiicoDev Adapter for Raspberry Pi
- A PiicoDev Cable (100mm or longer is best for Raspberry Pi)
In the video we use a PiicoDev Platform to keep everything safe and secure.
For this tutorial, we'll assume you're familiar using a Raspberry Pi like a desktop computer - connected to a keyboard, mouse, monitor and with an internet connection. If you need help getting started, have a look at Chapter 1 of the Raspberry Pi Workshop for Beginners.
If you prefer not to use the Expansion Board for Raspberry Pi, there are other connection options in our PiicoDev Connection Guide.
Connect the PiicoDev sensor to your Pi
Ensure the default address is set
For temperature sensors with an address switch labelled "ASW" (hardware versions v12 and above), make sure the first switch is ON and the rest are OFF.
For earlier revisions of the temperature sensor, the address was set by a solder jumper on the back. In this case, make sure that address 0x48 is selected.
Mount the PiicoDev® Adapter on your Raspberry Pi and plug connect your temperature sensor to the Adapter via the PiicoDev cable.
The adapter connects to the 40-pin GPIO header on the Raspberry Pi - ensure the header is mounted correctly, with the "Ethernet" label on the same side as the Pi's ethernet adapter.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Pictured above: A Raspberry Pi 4 Model B with USB Power and HDMI (two white leads). The PiicoDev Adapter is placed on the 40-pin header, and connects to the temperature sensor with a PiicoDev Cable.
Enable I2C
Power on your Raspberry Pi. Open the Raspberry Pi Configuration Menu, select the Interfaces tab and ensure I2C is enabled.
You only need to do this step for your first PiicoDev project - from here on you probably won't have to repeat this step when using PiicoDev hardware.
Install PiicoDev
Open Thonny (Pi Start Menu > Programming > Thonny IDE) and open the Manage Packages menu (Tools > Manage Packages)
Search for 'piicodev' and install or upgrade if necessary.
Download the Example Script
Download this example script: main.py (right-click, "save link as")
Save this file wherever you like to keep your programming projects. For this tutorial I'll save mine in /home/pi/PiicoDev
Run the Example
Double click main.py to open in Thonny, then click the green "Run Current Script" button.
Pictured above: The temperature data streams up the Shell and graphs in the Plotter (View > Show Plotter). The temperature sensor gets warmed up quickly, then cools slowly.
All going well, you ought to see temperature data streaming up the Shell window.
If you receive an error, it may be because the sensor is not connected properly - reconnect the sensor and try again.
The example also reads temperature in Farenheit and Kelvin - Modify the print statement to call the appropriate variable as you like.
eg.
print("It's " + str(tempF) + "°F")
Now we can easily read temperature in Celsius, Farenheit and Kelvin.
Tip: Open the Plotter (View > Plotter) to see a nice plot of your temperature data
Connecting Multiple Sensors
It's possible to connect up to four temperature sensors to the same PiicoDev bus and read them independently - they each require a unique address. In the connect section, we set our sensor to the default address (0x48): where only the first ASW switch is ON. To read from more sensors, they need to be configured with unique addresses. For example, the second sensor should have the ASW2 switch ON, and the rest off. The third should have the ASW3:ON and the rest off - and similarly a fourth sensor.
The following example will read two sensors independently. This example looks similar to the first example in this tutorial, except now the initialisation functions are passed the asw argument. This is a list of four numbers that represent the positions of the ASW switches. sensorA has the first switch set, so asw=[1,0,0,0]. Likewise, sensorB has the second switch set, so the second element is 1 and the rest are 0: asw=[0,1,0,0]. This logic can be expanded to a maximum of four sensors.
# PiicoDev TMP117 - reading multiple sensors # This program independently reads the temperature from two PiicoDev TMP117 precision temperature sensors # Each sensor should have its ASW switches configured to match the asw argument during initialisaiton, eg. # sensorA: asw=[1,0,0,0] so the first switch should be ON and the rest OFF # sensorB: asw=[0,1,0,0] so the second switch should be ON and the rest OFF # this logic expands to a maximum of four sensors from PiicoDev_TMP117 import PiicoDev_TMP117 from PiicoDev_Unified import sleep_ms # cross-platform compatible sleep function sensorA = PiicoDev_TMP117(asw=[1,0,0,0]) # initialise the sensor sensorB = PiicoDev_TMP117(asw=[0,1,0,0]) # initialise the sensor # Advanced users may prefer to set the address explicitly, instead: # sensorA = PiicoDev_TMP117(address=0x48) # initialise the sensor # sensorB = PiicoDev_TMP117(address=0x49) # initialise the sensor while True: # Read and print the temperature in various units tempA = sensorA.readTempC() # read from sensor A tempB = sensorB.readTempC() # read from sensor B # print both results print("A:" + str(tempA) + " " "B:" + str(tempB)) sleep_ms(1000) # delay 1 second
If you have any questions or uncertainty, start the discussion below. We're full-time makers and here to help!