I decided to add my temperature sensor to my new Pi2. This is fairly straightforward - you need the following
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).
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.
sudo nano /boot/config.txt
# Added to get temperature sensor working dtoverlay=w1-gpio
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.