16 Apr 2016

Temperature Sensor Readings using Raspberry Pi 3

In this post we will get a temperature sensor working with the Raspberry Pi 3, time to start messing about with wires, resistors and sensors!

In this post we will be covering; wiring up a temperature sensor, getting readings from the sensor on the Raspberry Pi 3 terminal and finally using a python script to show the readings in a friendly format for easy reading.


For this guide we are going to need the items below


  • 4.7Ω Resistor
  • Breadboard



  • Jumper Wires

We will need a selection of both male to male, and female to male jumper wires. In this guide there are 3 female to male wires used and two male to male wires used. Your set-up maybe different.

  • DS18B20 Temperature sensor



These are cheap water proof temperature sensors that are easy to get working with the Raspberry Pi 3, you can purchase these from Amazon or Ebay

Make sure you get the one that looks like the image above, the same sensor is available that is not water proof and looks different. The none water proof version is not much use if you are going to be using this for an Aquaponics set-up!

My DS18B20 sensors do not have bare wires on the end, most come with bare wires.
On this sensor the red wire is for power, the yellow wire for data and the black wire is ground.

Lets get started!


This guide will show diagrams and also photographs of the wiring step by step, below is the starting point.

We have the unplugged Raspberry Pi 3 next to a breadboard with the required 4.7 ohm resistor already in place. The DS18B20 sensor connection is also in shot but with no connections made yet.

On this breadboard the resistor is connected to B1 and B5. It does not matter which way round the resistor is placed and you can use which ever holes in the breadboard you wish. I will use my breadboards hole references throughout this guide.

Note: The DS18B20 in the wiring diagrams has 4 wires, this is not the case with my sensor, it only has 3 wires. Ignore the orange coloured wire coming off the sensor in the diagrams.





Below is a diagram showing all the GPIO (General Purpose Input Output) pins on the Raspberry Pi 3, you can use this for reference if required. Do not worry about the different colours for now.

All we are interested in for this guide is GPIO 4, which is pin 7 or physically 4 pins down and a 3.3 volts power pin and a ground pin. It should be apparent which pins to use by referring to the wiring diagrams, photographs or image below.

GPIO Raspberry Pi 3 Pin guide


Connect Power


Ensure your Raspberry Pi 3 is unplugged and connect one jumper wire from a 3.3 volt pin on the Raspberry Pi 3 to hole A1 in the breadboard. Connect another jumper wire from hole E1 on the bread board to the power wire (red) on the Temperature sensor.


I have used white wires for my power wiring, in the wiring diagrams power wires are the red lines.



Connect Ground


Next we will connect the wire for ground, You can use any ground pin off the Raspberry Pi 3 you like. The ground from the Raspberry Pi 3 is just connected directly to the sensor in this example.

I have used a blue wire for my ground, in the wiring diagram it is the black line.



Connect Data


Now we will connect the data wires, this will be two wires. One wire from GPIO 4 to C5 on the bread board. The second wire from E5 on the breadboard to the yellow data wire on the sensor.


I have used orange wires for the data wiring, in the wiring diagram it is the yellow lines.


Here is a close up. 


If your sensor has bare wires then it will need to be connected like below, no different just that the sensors wires will need to be in the breadboard instead of directly connecting wires to the sensor.




That is it wiring all done, time to test it.

If you have connected the sensor backwards when wiring up, the end of the sensor will heat up and eventually this will break the sensor. If your not sure with your wiring after you turn the Raspberry Pi 3 on, feel of the sensor to see if it is heating up - it will be obvious if this happens, it gets very hot!


Set-up and testing on the Raspberry Pi 3


Log in to the Raspberry Pi 3, the first thing we need to do is add a line of text to a file. Type in to the terminal
sudo nano /boot/config.txt


This will open the file we need to add a line to. The line we need to amend to the end of this file is below

dtoverlay=w1-gpio

Copy and then paste (Shift + Insert) this on to the end of the file, press Ctrl+x followed by y and enter to save the changes.


Reboot the Raspberry Pi 3, log back in and type in to the terminal
sudo modprobe w1-gpio



Follow this with
sudo modprobe w1-therm


Next we need to navigate to the devices directory, in the terminal type
cd /sys/bus/w1/devices


If we list the directory contents here, assuming the wiring is correct - we should see our Temperature sensor show up as a directory something like 28-0115905491ff

Each sensor has its own unique number so yours will not be the same as mine.
ls


Navigate in to the sensors directory, in the terminal type (replace **** with your sensor number)
cd ****


Now to get a reading from the sensor, type in to the terminal
cat w1_slave





You will be returned the above. Where in my screenshot it says t=20562, this is the temperature reading. This is 20.562 degrees. 

If your sensor is not connected properly you may get returned :crc=d5 NO,check your wiring and try taking a reading again. 

Great it is working! But not really easy to read.

Next we will use the programming language python to make these temperature readings easier. 


Python script

Below is the python script we are going to use to make the sensor readings much easier to read.

#!/usr/bin/env python

import os, glob, time, sys

#example python script from www.aquaponicpi.co.uk for use with DS18B20 temperature sensor

#initiate the temperature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#Define the degree symbol
degree = unichr(176)

#where to get the reading
device_folder = glob.glob('/sys/bus/w1/devices/REPLACETHIS')
device_file = [device_folder[0] + '/w1_slave']

#get raw reading from sensor
def read_temp_raw(): 
    f_1 = open(device_file[0], 'r')
    lines_1 = f_1.readlines()
    f_1.close()
    return lines_1

#check reading was good and strip out temperature
def read_temp(): 
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES' or lines[2].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t='), lines[3].find('t=')
    temp = float(lines[1][equals_pos[0]+2:])/1000
    return temp

#get the temp
temp = read_temp_raw() 

#chop it all up
outputmain= temp[1][-6:-4]
outputdecimel= temp[1][-3]

#put it all together with degree symbol
finaloutput= outputmain+ "."+outputdecimel+degree.encode("utf-8")+"C"

#print the final output to the terminal
print finaloutput

Copy all of the above to your clipboard, we will be pasting this in to a python script.

With the above copied to the clipboard, navigate to where you want the script to be, for me I am creating this in /home/pi
cd /home/pi

In the terminal type
sudo nano temperature.py

This will create a new file named temperature.py and open it in the nano text editor. This new file will be blank.

Paste in the code copied to the clipboard from above, using the key combination Shift+Insert.

With the code pasted in, use the arrow keys and navigate to where it says REPLACETHIS, change this to your sensor number. Be careful not to delete the ' at the end.


In the screenshot below you can see I have replaced that text with my sensor number.


Press Ctrl+x followed by y and then enter to save the file.

Test the Python script!


To test your new python script in the terminal type
sudo python temperature.py

You will be returned a nice clean response showing a reading taken from the DS18B20 as shown in the screenshot below.


Much easier to read, we can add a description to this as well if we like by modifying the python script a little.

Hopefully that has all worked exactly the same for you!? Any questions or suggestions? Let me know in the comments below.

Thanks for reading all of that! In the next post we will go about getting our temperature reading to display on the Raspberry Pi 3`s web page so we can see what the temperature is from anywhere in the world along with some other goodies also!




Show support for aquaponicpi and like the Facebook page! 
Click on the icon below to be taken to aquaponicpi on Facebook!
follow me on facebook







No comments:

Post a Comment