Raspberry pi 3 LDR Sensor, circuit and python programming

(Last Updated On: July 30, 2019)

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.


The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:

Raspberry Pi 3 full package: Best offer: https://amzn.to/2PA0UWF
Power bank: https://amzn.to/2DQD2rJ
LDR: https://amzn.to/2r3hW0N
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
5v 2A adaptor: https://amzn.to/2GsNfcw
2n2222 NPN transistors: https://amzn.to/2EmXsIb
10k Resistor: https://amzn.to/2KTd6OW
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
female DC power jack socket: https://amzn.to/2KEo1gt
470 uf capacitors: https://amzn.to/2xDCOzf
5×7 cm Vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / stripboard: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wirecutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF

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, Click here”.

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 10 UF 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:

Program Explanation:

Watch 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:

Recommended For You

About the Author: 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...

1 Comment

Leave a Reply

Close