Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Programming Raspberry Pi to Temperature using DS18B20 Sensor

The document outlines a project to program a Raspberry Pi to detect temperature using the DS18B20 temperature sensor. It includes objectives such as understanding GPIO pins, studying the DS18B20 sensor, and writing a Python program to display temperature readings on an LCD. The document provides circuit connections, a detailed Python program, and steps to execute the program.

Uploaded by

indalkaranuja2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming Raspberry Pi to Temperature using DS18B20 Sensor

The document outlines a project to program a Raspberry Pi to detect temperature using the DS18B20 temperature sensor. It includes objectives such as understanding GPIO pins, studying the DS18B20 sensor, and writing a Python program to display temperature readings on an LCD. The document provides circuit connections, a detailed Python program, and steps to execute the program.

Uploaded by

indalkaranuja2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Raspberry Pi to detect temperatures using DS18B20

temperature sensor
AIM: Programming Raspberry Pi to detect temperature using DS18B20 temperature sensor.

OBJECTIVES:
a. To understand the basics of GPIO Pins of Raspberry Pi.
b. To study the basic temperature sensor DS18B20.
c. To write a python program to display temperature sensor reading on LCD display.

CIRCUIT/ BLOCK DIAGRAM:

DS18B20 temperature sensor interfacing with Raspberry Pi

1|Page
GPIO Pin Connection:
Pins on Kit Pins on Raspberry Pi
GPIO Pin Pin Number
DS 18B20 GPIO 4 7
LCD_RS GPIO 21 40
LCD_EN GPIO 20 38
D4 GPIO 14 8
D5 GPIO 15 10
D6 GPIO 18 12
D7 GPIO 23 16
+ 3.3V - 1
+ 5V - 2
GND - 6

Procedure:

Note: The Raspbian operating system comes with Python already installed in it.

1. Setting up the circuit:


a. Turn OFF the Raspberry Pi while building/ connecting the circuit board/components
according to diagram.
b. Then turn the Raspberry Pi and check the Raspbian OS loaded properly or not. If not
then check the circuit connection again.

2. Python Programming: Python via IDLE

a. Start Raspberry Pi in desktop mode, open the Applications Menu in the top left of
your screen, and navigate to Programming > Python 3 (IDLE) /. This will open the
Python-shell.

2|Page
b. Write a program for DS18B20 temperature sensor with raspberry pi and save it as
“DS18B20.py” file name.

Program: # DS18B20 temperature sensor with Raspberry PI


import os
import glob
import time
import RPi.GPIO as GPIO
#DS18B20 PIN is GPIO4
# Below are LCD GPIO PIN Numbers
LCD_RS = 21
LCD_E = 20
LCD_D4 =14
LCD_D5 =15
LCD_D6 =18
LCD_D7 =23
LCD_WIDTH =16
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0X80
LCD_LINE_2 = 0XC0
E_PULSE =0.0005
E_DELAY = 0.0005
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()

3|Page
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
lcd_byte(0X01,LCD_CMD) # clear LCD
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("Temp : " + '{0:.2f}'.format(temp_c) + " C")
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string("Temp : " + '{0:.2f}'.format(temp_f) + " F")
print("Temp : " + '{0:.2f}'.format(temp_c) + " C\t" + '{0:.2f}'.format(temp_f) + " F")
return temp_c
def lcd_init():
GPIO.setmode(GPIO.BCM)
GPIO.setup(LCD_E, GPIO.OUT)
GPIO.setup(LCD_RS,
GPIO.OUT)GPIO.setup(LCD_D4,
GPIO.OUT) GPIO.setup(LCD_D5,
GPIO.OUT) GPIO.setup(LCD_D6,
GPIO.OUT) GPIO.setup(LCD_D7,
GPIO.OUT)
lcd_byte(0X33,LCD_CMD)
lcd_byte(0X32,LCD_CMD)
lcd_byte(0X28,LCD_CMD)
lcd_byte(0X0C,LCD_CMD)
lcd_byte(0X06,LCD_CMD)
lcd_byte(0X01,LCD_CMD) # clear LCD
def lcd_string(msg):
msg = msg.ljust(LCD_WIDTH, ' ')
for i in range(LCD_WIDTH):
lcd_byte(ord(msg[i]),LCD_CHR)
def lcd_byte(bits, mode):
set4bitMSB(bits, mode)
set4bitMSB(bits << 4, mode)
def set4bitMSB(bits,mode):
GPIO.output(LCD_RS, mode)
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
4|Page
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
lcd_init()
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string(":*: FCT Pune :*:")
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(":DC18B20 Sensor:")
time.sleep(2)
while True:
temp = read_temp()
time.sleep(2)

Steps to execute Program

1. Make connections as given above


2. Open ‘DS18B20.py’ file in python 3 IDE
3. Select Run from top menu and click Run Module.

5|Page

You might also like