Raspberry Pi gpio interface with Temperature sensor ds18b20

Raspberry Pi gpio interface with Temperature sensor ds18b20 3

[otw_is sidebar=otw-sidebar-1]Raspberry Pi gpio interface with Temperature sensor ds18b20In this post I will discuss how to interface the temperature sensor with the Raspberry Pi (Raspberry Pi gpio interface with Temperature sensor ds18b20)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.

[otw_is sidebar=otw-sidebar-3]

Raspberry Pi gpio interface with Temperature sensor ds18b20 and the Python language:

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.

Raspberry Pi gpio interface with Temperature sensor ds18b20This 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.

[otw_is sidebar=otw-sidebar-3]

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.

Enabling the 1-wire Interface in the Raspberry Pi:

Step4:

 

Select.

 

Enabling the 1-wire Interface in the Raspberry Pi:Step5:

Click yes.

Enabling the 1-wire Interface in the Raspberry Pi:

Step6:

Click ok.

Enabling the 1-wire Interface in the Raspberry Pi:

Step7:

 

 

 

Click yes.

Enabling the 1-wire Interface in the Raspberry Pi:

 

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:

[otw_is sidebar=otw-sidebar-3]

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.

Raspberry Pi connection with DS18B20,Raspberry Pi GPIO

Step1:

Open the Python IDLE as shown in the following image:

Python code for DS18B20 and the Raspberry Pi:Step2:

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

import os
import glob
import time
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'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_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
return temp_c, temp_f

while True:
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.

[otw_is sidebar=otw-sidebar-2]

 

Raspberry Pi gpio interface with Temperature sensor ds18b20

3 Comments

  1. Wow, marvelous weblog structure! How long have you ever
    been blogging for? you made running a blog glance easy.
    The full glance of your site is excellent, let alone the content!

    You can see similar here sklep online

  2. Hey! Do you know if they make any plugins to assist with Search
    Engine Optimization? I’m trying to get my website to rank for some targeted keywords but
    I’m not seeing very good results. If you know of any please share.
    Thank you! You can read similar blog here: Link Building

Leave a Reply

Your email address will not be published. Required fields are marked *