Raspberry Pi gpio interface with Temperature sensor ds18b20

In this post I will discuss how to interface the temperature sensor with the Raspberry Pi and read the values given by the Temperature sensor ds18b20 using the Python language. In the previous post I have discussed the Ultrasonic sensor and the how to make the hurdle avoiding car using the Raspberry PI and the Ultrasonic sensor.
After reading this post you will able to understand the interfacing of the Raspberry PI with the temperature sensor and write the code to read the analog values of the temperature sensor using the Python language in the Python IDLE.

As most of us know that the Raspberry Pi has a total of twenty six General Purpose Input / Output pins on it. You can either use these General Purpose Input / Output pins as by default they are to be served as the input or output or you can configure these pins to be served as serial communication bus such the Inter Integrated Circuit (I2C) or the Serial Peripheral Interface, it depends upon which sensor or module you are using in the project. If you are using the sensor or actuator that has the serial communication bus interface then you will need to configure the Raspberry Pi according to the serial communication required by your slave device. I have already discussed the enabling of these serial communication buses in my previous however in this tutorial you need not the knowledge of these interfaces as this post will just use GPIO pins of the Raspberry Pi as either simple input or the output. The temperature sensor will be connected to the one of the GPIO pin of the Raspberry Pi and then the code is written in the Python IDLE using the Python language. The temperature sensor that I am using is shown in the following figure.

This temperature sensor is different from the thermistor. It is a digital temperature sensor which uses the one-wire protocol for serial communication between the Temperature sensor and the Raspberry Pi. As clear form this discussion we need to enable the one-wire serial communication for letting the Raspberry PI to talk to the temperature in the Raspberry Pi.

Enabling the 1-wire Interface in the Raspberry Pi:

Let us now see how to enable the I-wire protocol or interface in the Raspberry Pi. Simply follow the following steps mentioned below to be able to let the Raspberry PI communicate via 1-wire protocol.

Step1:

Open the terminal window in the Raspberry Pi.

Step2:

Type the following command in the terminal window:

sudo raspi-config

Step3:

After you typed the above mentioned command in the terminal window a window will appear like the one shown in the following image.

Step4:

Select.

Step5:

Click yes.

Step6:

Click ok.

Step7:

Click yes.

After following the above mentioned steps you will be able to enable the one- wire protocol / interface on the Raspberry Pi.

Raspberry Pi GPIO connection with DS18B20:

The raspberry Pi GPIO is connected to the temperature sensor DS18B20 via one wire interface. As we have already enabled the one-wire interface on the Raspberry Pi GPIOM I will now discuss the connection of the Raspberry PI with the temperature sensor. As we can see in the picture below the Raspberry Pi supports the one-wire interface on only one pin that is the GPIO 7 pin. You can connect the temperature sensor only to this pin of the Raspberry Pi. This can be seen in the following picture.

Now connect the Raspberry Pi with the temperature sensor like the one shown in the following picture:

The data pin of the temperature sensor is connected to the GPIO 7 of the raspberry Pi. As can be seen in the above paragraph that the GPIO pin 7 can support the one-wire protocol and thus can be connected to the temperature sensor like the picture above.

Python code for DS18B20 and the Raspberry Pi:

Follow the following steps to be able to write the code in the Python IDLE.

Step1:

Open the Python IDLE as shown in the following image:

Step2:

You can copy and paste the following Python code in the Python IDLE:

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

importos

importglob

importtime

os.system('modprobe w1-gpio')

os.system('modprobe w1-therm')

base_dir='/sys/bus/w1/devices/'

device_folder=glob.glob(base_dir+'28*')[0]

device_file=device_folder+'/w1_slave'

defread_temp_raw():

f=open(device_file,'r')

lines=f.readlines()

f.close()

returnlines

defread_temp():

lines=read_temp_raw()

whilelines[0].strip()[-3:]!='YES':

time.sleep(0.2)

lines=read_temp_raw()

equals_pos=lines[1].find('t=')

ifequals_pos!=-1:

temp_string=lines[1][equals_pos+2:]

temp_c=float(temp_string)/1000.0

temp_f=temp_c*9.0/5.0+32.0

returntemp_c,temp_f

whileTrue:

print(read_temp())

time.sleep(1)

Step3:

Now save the file. Do not forget to enter the extension “.py” at the end of the name of your file.

Step4:

Now you can run this either directly from the Python IDLE or by typing the name in the terminal window of the Raspberry Pi.
Notice the libraries we have imported in the above code. These libraries are important are already installed in the Raspbian which is the recommenced operating system for the Raspberry Pi.
So this post explains you about the DS18B20 temperature sensor, then it discusses the One –wire interface and explains the method involved in enabling the one-wire interface in the Raspberry Pi then it discusses the connection of the Raspberry Pi with the temperature sensor.
That is all for now. In the next post I will come up with another application of the Raspberry Pi. Till then stay connected, keep reading and enjoy learning.

Raspberry Pi gpio interface with Temperature sensor ds18b20

Share this:

Related

Author: admin

I am Kashif Mirza, the founder of ProjectIOT123. I am an Embedded Engineer and working on Embedded Projects since 2003. I have worked on Arduino, Raspberry Pi, PIc Microcontroller, 8051 etc. and have designed both prototypes & industrial projects.