raspberry pi

Raspberry pi 3 LDR Sensor, circuit and python programming

Raspberry Pi 3 LDR Description:

 

raspberry pi 3 LDR- In this Tutorial, you will learn how to use an LDR “light-dependent resistor to control an LED” light-emitting diode”. The LED will be turned on and turned off depending on the light intensity in the room. This project can also be used for day and night detection.


Amazon Links:

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Raspberry pi 3 LDR Sensor Circuit Diagram:

raspberry pi 3 ldr

This schematic is designed in the Cadsoft eagle 9.1.0 version. If you want to learn how to make schematics and PCB then watch my tutorial.

This is the circuit that I explained in my previous tutorial. In the previous tutorial, we controlled this led using a push button. This time I modified a circuit and added an LDR. So this time we will control this led using an LDR. The LDR is connected with 3.3v and the other leg of the LDR is connected with the positive leg of the 10UF capacitor. while the other leg of the capacitor is connected with the ground… now take a wire from the middle and connect it with pin number 7 of the raspberry pi. our circuit diagram is completed. So LED is connected with pin number 11 and the RC circuit is connected with pin number 7.


raspberry pi 3 ldr

As you can see all the components are interfaced as per the circuit diagram, as you can see the LED is connected in series with a 330-ohm resistor..the cathode side of the led is connected with pin number 6 which is the ground pin of the raspberry pi..the anode side is connected with the resistor…while the other end of the resistor is connected with pin number 11…

On the right side, an LDR is connected in series with a 10uf capacitor…the first leg of the LDR is connected with pin number 1 which is the 3.3v pin….the another leg of the LDR is connected with the positive leg of the capacitor…and a wire from the middle is connected with the pin number 7 of the raspberry pi..and the ground leg of the capacitor is connected with the ground…So that’s it…


raspberry pi 3 ldr

Now let’s power up raspberry pi. now we are ready for the programming… as you can see no keyboard and mouse is connected and it has no physical connection with the laptop or an LCD. because as I explained in my previous tutorials, we will be using the ssh network using the putty software to write and execute programs. So I recommend you should watch my previous tutorial. The link is given below.  Which completely explains, how to set up your ssh network using wifi.


How to set up the SSH network using wifi:

For the Step by Step Program explanation watch video Tutorial given at the end of this Article.

Raspberry pi 3 LDR Sensor Programming:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
delayt = .1 
value = 0 # this variable will be used to store the ldr value
ldr = 7 #ldr is connected with pin number 7
led = 11 #led is connected with pin number 11
GPIO.setup(led, GPIO.OUT) # as led is an output device so that’s why we set it to output.
GPIO.output(led, False) # keep led off by default 
def rc_time (ldr):
    count = 0

    #Output on the pin for
    GPIO.setup(ldr, GPIO.OUT)
    GPIO.output(ldr, False)
    time.sleep(delayt)

    #Change the pin back to input
    GPIO.setup(ldr, GPIO.IN)

    #Count until the pin goes high
    while (GPIO.input(ldr) == 0):
        count += 1

    return count


#Catch when script is interrupted, cleanup correctly
try:
    # Main loop
    while True:
        print("Ldr Value:")
        value = rc_time(ldr)
        print(value)
        if ( value <= 10000 ):
                print("Lights are ON")
                GPIO.output(led, True)
        if (value > 10000):
                print("Lights are OFF")
                GPIO.output(led, False)
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

Program Explanation:

Watch the video for the Complete explanation. The video is available at the end. Below is the script used while recording the video.

Open the putty software.

enter the IP address of your raspberry pi and click open.

 Enter pi as your login name.

and raspberry as your password and click enter after you log in.



Use clear command to clear the screen…use the ls command to list all the files and folders..  Open the youtube folder by typing cd youtube….now use the ls command to list all the files …led1.py is the program that I created in my last tutorial…now let’s make another file with the name ldr and extension .py…simply type sudo nano ldr.py and press enter…as you can see an editor is opened…first we start by importing rpi.gpio ..time ..and then use the setmode function to specify the numbering scheme which will be used…I have already explained these three instruction in my 2nd tutorial, so I recommend you should watch that tutorial the link is given in the description…then define a variable delay time and set it to.


1….later if you want to change the delay you can simply change this value over here..now let’s define another variable with the name value and set it to zero…this variable will be used to store the LDR value..now let’s define pins… ldr = 7….ldr is connected with pin number 7…led = 11….led is connected with pin number 11….now let’s use the gpio.setup function to set the led as output, as you know led is an output device..now let’s use the gpio.output function to turn off the led..by default we keep the led in off state…

now let’s make a user-defined function with the name rc_time…rc_time is a user defined function and it takes one argument as the input which is the ldr as ldr is connected with pin number 7…let’s make another variable with name count and set it to 0…first set pin number 7 which is ldr as output, for this use gpio.setup function …now use the gpio.output function to turn off pin number 7 which is ldr…now use the time.sleep function for the delay….now let’s change pin number 7 which is ldr back to input….


Now let’s make an infinite while loop….by simply typing while true…now lets print the ldr value for this we use the print function…first store the ldr value in variable value…value = rc_time function and use the print function to print this value…now lets use if conditions to turn on and turn off the led the value 10000 is selected after testing this project in my room..and then use the exception and finally instruction which I have already explained in my previous tutorials…

So now our program is ready… press control o on the keyboard to save…and then press control x to exit the editor….now use the ls command to list all the files,as you can see the ldr.py file is created…let’s run this program by simply typing python ldr.py and press enter…

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

2 Comments

  1. PLEASE REPLY

    now let’s make a user-defined function with the name rc_time…rc_time is a user defined function and it takes one argument as the input which is the ldr as ldr is connected with pin number 7…let’s make another variable with name count and set it to 0…first set pin number 7 which is ldr as output, for this use gpio.setup function …now use the gpio.output function to turn off pin number 7 which is ldr…now use the time.sleep function for the delay….now let’s change pin number 7 which is ldr back to input…

    But my question is why you made this input then output and again input?

Leave a Reply

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

Back to top button