Wikijunior:Raspberry Pi/Raspberry Pi Temperature Sensor

Tutorial by Andrew Oakley Public Domain 2 July 2016 www.cotswoldjam.org

Getting started
This tutorial shows you how to make a temperature sensor which shines red when hot, and blue when cold.

At the end of each line, press Enter.

Your tutor should have prepared your Raspberry Pi for you and given you a bag of components. Alternatively, you can download the files from: http://cotswoldjam.org/tutorials

Temperature sensor
A DS18B20 waterproof temperature sensor. It has three wires at one end and a metal sensor on the other end. This version is waterproof and the sensor can be dipped in water. A smaller, cheaper, non-waterproof version is also available.

Breadboard
A mini breadboard. This has a top and bottom section. All the points along each column in each section are joined, so we can make electrical connections.

Resistors
Three resistors. Check the coloured stripes – the two identical ones are 220 Ohms (red red brown) and the odd one out (with a blue stripe) is 4700 Ohms. The more Ohms, the more resistance to electricity. Resistors can be placed either way around.

Light-Emitting Diodes (LEDs)
Red and blue LEDs. The short leg is the negative leg. There is also a flat side to the rim of the bulb; this will also indicate which side is negative. LEDs also need a small resistor to prevent them from burning out. We will use a 220 Ohm resistor for each LED.

Jumper wires
Jumper wires. You should have six male-to-female jumpers (sticky-outy at one end, inny at the other end) and one male-to-male jumper (outy at both ends). Your wires may be of many different colours.

The circuit


Place the components on the breadboard like this.

The LEDs have their negative connector (short leg, flat side) to the right, on the same column as the 220 Ohm resistors.

The temperature sensor has three connectors:

The 4700 Ohm resistor crosses the columns between the temperature sensor's data connector (centre), and the positive connector (right).

Next, connect three male-to-female jumpers between the temperature sensor and the Raspberry Pi's GPIO pins.


 * 1) Ground (negative) goes to pin 6 (GND).
 * 2) Data (middle connector) goes to pin 7 (GPIO4).
 * 3) 3.3V (positive) goes to pin 1.

Note that pin numbers ("board numbers") and GPIO numbers ("BCM numbers" – Broadcom, the processor maker) are not the same.

Check the diagram.

Now connect the jumpers between the LEDs and the Raspberry Pi.

A male-to-female jumper goes from the positive leg of the blue LED to pin 16 (GPIO23).

Another male-to-female jumper goes from the positive leg of the red LED to pin 18 (GPIO24).

A male-to-male jumper crosses between the resistors in the bottom section, and finally, a male-to-female jumper goes from one of the resistors in the bottom section to pin 14 (GND).

Configuring the Pi for 1-Wire Sensors
The temperature sensor uses a 1-Wire interface (positive and negative connectors, plus 1 data connector). We need to turn 1-Wire on.

From the Raspberry Pi desktop, click Menu – Preferences – Raspberry Pi Configuration.

From the Configuration program, click the Interfaces tab at the top. Then find the 1-Wire row and click Enabled. If it's already enabled, that's fine. If it wasn't already enabled, you will be asked to reboot the machine – make sure you reboot.

First program – taking the temperature
From the menu, select Programming - Python 3. Then use File, New Window to create a new program and type it in. Alternatively, you can load the ready-made program using File, Open, then scroll sideways and double-click the python folder, then double-click the temperature folder, then click  and Open.

When typing in the program, make sure you put spaces where they're needed at the start of lines. For example, two spaces before. Spaces must line up.

Run the program by selecting Run menu, Run Module. You should see some temperature readings! Try warming up the sensor by holding it tightly. Stop the program by holding down the Ctrl key and pressing C.

teaches the computer about new things, using small programs that other people have written. We use the word "library" to decribe this. is a library that makes the GPIO pins easy to use. is a library that makes the temperature sensor easy to use. We also import the  command from the   library.

repeats a section forever (keep going while… always!)

reads the temperature from the sensor, and places the value in Celsius into the variable named. Variables are like boxes which can hold numbers or words.

outputs information to the screen. In this case, a temperature reading.

waits for a number of seconds. We leave a gap of 0.1 of a second between readings. However, readings also take a moment, so the loop doesn't go too fast.

Second program – lighting the LEDs
Change the program, or load in the  file:

Run the program by selecting Run menu, Run Module. The red and blue LEDs will change their brightness depending on the temperature! Stop the program with CTRL+C and try changing the values of cold and hot. Body temperature is about 37°C (98.6°F), an ice cube is 0°C (32°F) and a typical room temperature is 22 degrees Celsius (71.6 degrees Fahrenheit).

tells the computer that we have an LED connected, and that we will change the brightness using Pulse Width Modulation – flashing it on and off very quickly!

We set the brightness to a level (hotness) depending on the temperature between cold and hot.

If the temperature is hot or above, only red lights up (hotness is set to 1). If it's cold or below, only blue (hotness is set to 0). If it's in between, the brightness of blue and red will be dim or bright depending on temperature – that's the maths that does (temp-cold) divided by (hot-cold). Try working out the maths with a pencil or a calculator!

So hotness is always a decimal number between 0 (cold) and 1 (hot). We use a neat maths trick for the blue LED to get the "reverse" of the brightness; 1 minus hotness. When hotness is 0.8, the blue LED's brightness is: 1 − 0.8 = 0.2

Advanced programmers
Have a look at the  library to find out how we get the readings from the temperature sensor. Find the file  and output the readings from the terminal using the   command.

Alter the code for  library to use Fahrenheit rather than Celsius.

Cjam-temperature-tutorial.pdf
The original PDF for this tutorial is on Wikimedia Commons: