- LearnElectronics
Smart Lighting System - Using LED's, LDR and Raspberry Pi
Updated: Jun 21, 2020
List of:
Software -
1. SSH Terminal

2. Python IDLE

Hardware-
1. USB Cable for Raspberry Pi

2. LED's (We used 4)

3. LCD Backpack

4. LDR Sensor

5. Raspberry Pi (We are using Raspberry Pi 3B+)

6. Connecting Wires (Male-Female)

7. Breadboard

8. LCD - 16*2 Display

9. SD Card (For Raspberry Pi)

10. Capacitor

Circuit:
Schematic -

Hardware Circuit -

Code:
import RPi.GPIO as GPIO
from ldr_pr import LDR
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()
mylcd.lcd_clear()
mylcd.lcd_display_string('Smart Lighting',1,2)
mylcd.lcd_display_string('Mode: ON',2,4)
while True:
try:
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
p=0
for i in range(0,5):
p=p+LDR(11,13)
p_avg = p/5
p_diff = 500 - p_avg
if p_diff >= 0:
GPIO.output(18, GPIO.HIGH)
mylcd.lcd_clear()
mylcd.lcd_display_string('LDR value:',1)
mylcd.lcd_display_string(str(round(p_avg)),1,10)
mylcd.lcd_display_string('LED ON',2,4)
else:
GPIO.output(18, GPIO.LOW)
mylcd.lcd_clear()
mylcd.lcd_display_string('LDR value: ',1)
mylcd.lcd_display_string(str(round(p_avg)),1,10)
mylcd.lcd_display_string('LED OFF',2,4)
except KeyboardInterrupt:
mylcd.lcd_clear()
mylcd.lcd_display_string('Smart Lighting',1,2)
mylcd.lcd_display_string('Mode: OFF',2,4)
break
GPIO.cleanup()
Download the codes -
Note: All these files should be converted to the python file format (i.e. .py format) before uploading the code to the Raspberry Pi board.
Working:
In the program, we have set the LDR value threshold - 500. So, whenever the value goes above 500, the Raspberry Pi would turn all the led's connected ON and if the value is below 500 then the led's are OFF.
Output 1 - Starting the Project

Output 2 - LDR value below the threshold

Output 3 - LDR value above the threshold

Check the practical implementation of the above-explained project and see how it works before you try it yourself.
See Also - Interfacing I2C - LCD with Raspberry Pi