# IoT Programming with Python and Raspberry PI
# Example - Temperature sensor
Interfacing of DS18B20 with Raspberry pi
Connection of DS18B20 with Raspberry pi
You can see there are three terminal
- Data (One wire protocol)
R1 is 4.7k ohm resistance for pulling up the voltage level
- Vcc should be connected to any of the 5v or 3.3v pins of Raspberry pi (PIN : 01, 02, 04, 17).
- Gnd should be connected to any of the Gnd pins of Raspberry pi (PIN : 06, 09, 14, 20, 25).
- DATA should be connected to (PIN : 07)
Enabling the one-wire interface from the RPi side
Now write a module in python to read the temperature
import glob import time RATE = 30 sensor_dirs = glob.glob("/sys/bus/w1/devices/28*") if len(sensor_dirs) != 0: while True: time.sleep(RATE) for directories in sensor_dirs: temperature_file = open(directories + "/w1_slave") # Reading the files text = temperature_file.read() temperature_file.close() # Split the text with new lines (\n) and select the second line. second_line = text.split("\n") # Split the line into words, and select the 10th word temperature_data = second_line.split(" ") # We will read after ignoring first two character. temperature = float(temperature_data[2:]) # Now normalise the temperature by dividing 1000. temperature = temperature / 1000 print 'Address : '+str(directories.split('/')[-1])+', Temperature : '+str(temperature)
Above python module will print the temperature vs address for infinite time. RATE parameter is defined to change or adjust the frequency of temperature query from the sensor.
GPIO pin diagram