Programming Raspberry Pi to detect light intensity using
(LDR) photocell sensor
AIM: Programming Raspberry Pi to detect light intensity using (LDR) photocell sensor.
OBJECTIVES:
a. To understand the basics of GPIO Pins of Raspberry Pi.
b. To study the basics of (LDR) Photocell and ADC.
c. Write a python program to display light intensity reading on LCD.
CIRCUIT/ BLOCK DIAGRAM:
1|Page
GPIO Pin Connection:
Pins on Kit Pins on Raspberry Pi
GPIO Pin Pin Number
SDA GPIO 2 3
SCL GPIO 5 5
LCD_RS GPIO 21 40
LCD_EN GPIO 20 38
D4 GPIO 14 8
D5 GPIO 15 10
D6 GPIO 18 12
D7 DPIO 23 16
+ 3.3V - 1
+ 5V - 2
GND - 6
Sensor connected to ADC (ADS1115)
Sensor ADC Pin Pin Name
LDR A0 Analog I/p 0
Pot A1 Analog I/p 1
2|Page
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.
b. Write a program for (LDR) Photocell sensor and ADS1115 interfacing with
raspberry pi and save it as “photocell.py” or “ldr.py” file name.
Program1: #Photocell sensorandADS1115 interfacing with Raspberry Pi
#import numpy
#importtheADS1x15module.
import time
import Adafruit_ADS1x15
import RPi.GPIO as GPIO
#Below are the LCD GPIO PIN Numbers
LCD_RS=21
LCD_E =20
3|Page
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.001
E_DELAY=0.001
def update(ch):
adc.start_adc(ch,gain=GAIN)
value =adc.get_last_result()
time.sleep(0.5)
return value
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) #clearLCD
def lcd_string(msg):
msg=msg.ljust(LCD_WIDTH,' ')
for i in range(LCD_WIDTH):
4|Page
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)
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)
#Create an ADS1115ADC (16-bit) instance.
adc=Adafruit_ADS1x15.ADS1115()
GAIN=1
val= [ ]
#Start continuous ADC conversions on channel 0 using the previous gain value.
adc.start_adc(0, gain=GAIN)
print('ReadingADS1x15channel0')
lcd_init()
5|Page
lcd_byte(LCD_LINE_1,LCD_CMD)
lcd_string(":*: FCT Pune :*:")
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(":*:LDR Sensor:*:")
time.sleep(2)
whileTrue:
ch0=update(0)
ch1=update(1)
print("CH0 "+str(ch0) + "\tCH1 "+str(ch1))
n0= (3.28/26288.0) * ch0
n1=(3.28/26288.0) *ch1
ldr='{0:.2f}'.format(n0)+" V"
mV1='{0:.2f}'.format(n1)+ "V"
print("LDR"+ldr+"\tPOT"+mV1)
lcd_byte(0X01,LCD_CMD) #clearLCD
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("LDR:"+ldr)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string("VDC : " + mV1)
time.sleep(2)
Steps to execute Program:
1. Make connections as given above.
2. Open ‘Photocell.py’ file in python3 IDE
3. Select Run from top menu and click on Run Module.
6|Page