I decided to add my temperature sensor to my new Pi2. This is fairly straightforward - you need the following
- 1 temperature sensor
Wiring it up
The DS18B20 sensor has three pins which are connected as follows. Note that the sensor has a domed side and a flat side - make sure you have it the right way round (i.e. flat side towards the pi in the breadboard setup shown below).
- Pin 1 connects to GND
- Pin 2 connects to GPIO4 on the Raspberry Pi
- Pin 3 connects to the 3.3V supply (3V3) from the Pi
This is connected to the Pi as follows. Do this with the Pi turned off!
If you are using one of the 'new' Pis (i.e. the Pi 2), you will need to add the following line to file /boot/config.txt. If you don't do this then the temperature sensor wont be detected by the Pi.
- Open the file for editing
sudo nano /boot/config.txt
- Add the following to the end of the file
# Added to get temperature sensor working dtoverlay=w1-gpio
- Use Ctrl-X to save the file
Log on to the Pi and enter the following commands
sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices ls -l
The final command above lists the content of directory /sys/bus/w1devices - you will see something similar to the following.
Note that there is a directory consisting of numbers - i.e. 28-00000521fad0 in the example above. This is the unique directory for the temperature sensor and is the serial number of the sensor you have used. Make a note of this as you will need tit when accessing the sensor.
Reading the temperature
Cd to the directory that you noted above and then run command cat w1_slave. This will interrogate the sensor and display the output - e.g.
Note that the temperature is displays in Celcius to 3 decimal places - e.g. in the example above t=23500, so temperature is 23.5 Celcius.