raspberry pi 3 LDR- We will learn how to use an LDR “light-dependent resistor to control a light-emitting diode” in this project. Depending on the light intensity in the house, the led will be on and off.
The Amazon Purchase links are given below:
Arduino Uno: https://amzn.to/39aq6ZT
Mega 2560: https://amzn.to/2SszMsI
lm7805 Voltage Regulator: https://amzn.to/2ERYoTJ
330-ohm resistors pack: https://amzn.to/2Qj1Igg
female DC power jack socket: https://amzn.to/377Pg9M
470uf capacitors: https://amzn.to/2MrN3xR
5×7 cm Vero board: https://amzn.to/37b7KWO
female headers: https://amzn.to/350w6RE
connection wires: https://amzn.to/2MvOJXd
Super Starter kit for Beginners: https://amzn.to/398dliF
Jumper Wires: https://amzn.to/2SrnBwo
Bread Board: https://amzn.to/2MxV5FM
12v Adaptor: https://amzn.to/2MuOlZk
PCB plate: https://amzn.to/2MuwNMB
Variable Supply: https://amzn.to/39d0KdP
Digital Multimeter: https://amzn.to/34WbVoa
Vero Board / stripboard: https://amzn.to/39nL9Zg
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2PVwexF
Solder wire: https://amzn.to/2QlOvTS
Wire Stripper: https://amzn.to/353tYJa
PCB small portable drill machine: https://amzn.to/2MvQqnx
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 Circuit Diagram:
In my previous article, this is the circuit I described. We regulated this led with a push button in the previous tutorial. I changed a circuit this time and added an LDR. So this time we’re going to use an LDR to control this led. The LDR is connected to 3.3v and the LDR’s other arm is connected to the 10uf condenser’s positive side. While the condenser’s other leg is attached to the gnd … now take a wire from the middle and attach it to the raspberry pi pin number 7. Our circuit diagram is done. So pin number 11 is connected to the LED and the RC circuit is connected to pin number 7.
As you can see, all the components are interfaced according to the circuit diagram, as you can see that a 330-ohm resistor links the LED in series. The cathode side of the led is associated with pin number 6 which is the ground pin of the raspberry pi…While the other end of the resistor is attached to the pin number 11 … The LDR is connected in series to the 10uf capacitor on the right side…The first leg of the LDR is connected to the pin number 1 which is the 3.3v pin …. the other leg of the LDR is connected to the positive leg of the condenser … and the middle wire is connected to the pin number 7 of the raspberry pi .. and the ground leg of the condenser is connected to the ground …
Now let’s raspberry pi power up. We’re prepared for programming now… as you can see that there’s no keyboard and there’s no link between the mouse and the laptop or LCD. Because we’re going to use the ssh network to write and execute programs, as I described in my previous tutorials.
raspberry pi 3 ldr 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()
raspberry pi 3 ldr Program Explanation:
Open the putty software.
Place your raspberry pi’s IP address so press open.
Enter pi as your name for your account.
And when you sign in, raspberry as your username and click enter.
Using clear command to clear the window … using ls to list all files and folders. Type cd youtube to open the youtube directory ….Now use ls to list all the files…LED1.py is the last tutorial program I made…Let’s now make another file with the LDR name and the.py extension…Only type sudo nano ldr.py and press in.
The editor is opened as you can see..First we import rpi.gpio .. time .. and then use the set mode to specify the numbering scheme to be used.…I’ve already explained these three instructions in my 2nd tutorial, so I recommend that you watch the linked tutorial 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.… Use this variable to store the LDR value…Let’s set pins now … LDR = 7…ldr is linked to pin number 7…led=11….LED is connected to pin number 11….Now let’s use gpio.setup to configure the led as output, as you know led is an output tool. Now let’s use gpio.output to turn off the led...We keep the led off by default … let’s now make a user-defined function with the name rc_time.…rc_time is a function specified by the user and takes one argument as the LDR input connected to pin number 7…Let’s make another name count variable and set it to 0 … first set pin number 7 which is ldr as output.…Now use the gpio.output method to turn off the 7 pin which is LDR…Now use the delay time.sleep function… now switch pin number 7 which is ldr back to output…
Now let’s make an infinite while looping …. by simply typing while true … now let’s print the LDR value for this we use the print function … only store the LDR value in variable value … value = rc time function and use the print function to print this value…Now let’s use if conditions to toggle on and turn off the led value 10000 after checking this project in my room ..And then use the exception and eventually I explained the instructions in my previous tutorials…So now our software is ready… press o on the keyboard to save… and then press x to exit the editor… now use the ls command to list all the files, as you can see the development of the ldr.py folder… just type python ldr.py and press enter…