This tutorial of Robo India explains how to use Light Dependent Resistor(LDR) with Raspberry Pi
1. Introduction:
An LDR (Light Dependent Resistor) or a photo resistor is a photo conductive sensor. It is a variable resistor that changes it’s resistance in a proportion to the light exposed to it. Its resistance decreases with the intensity of light.
1.3 Hardware required
S.No. | Item | Quantity |
1 | Raspberry Pi | 1 |
2 | Pi Widge B+ | 1 |
2 | Breadboard | 1 |
3 | LDR | 1 |
4 | Capacitor 0.1UF | 1 |
5 | Jumper Male to male | 4 |
2. Building Circuit
Schematic of circuit
Layout of circuit
3. Programming:
Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the code to run this circuit on Raspberry Pi.
You may download this code (Python Code) from here.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) #define the pin that goes to the circuit pin_to_circuit = 22 def rc_time (pin_to_circuit): count = 0 #Output on the pin for GPIO.setup(pin_to_circuit, GPIO.OUT) GPIO.output(pin_to_circuit, GPIO.LOW) time.sleep(0.1) #Change the pin back to input GPIO.setup(pin_to_circuit, GPIO.IN) #Count until the pin goes high while (GPIO.input(pin_to_circuit) == GPIO.LOW): count += 1 return count #Catch when script is interupted, cleanup correctly try # Main loop while True: print ( rc_time(pin_to_circuit)) except KeyboardInterrupt: pass finally: GPIO.cleanup()
4. Output
Python Coding with Raspberry Pi connects your project to the real world. The easiest introduction to Python is through IDLE, a Python development environment.
Open IDLE (Python Development Environment) from the Desktop or applications menu:
Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.
Then write Python 3 file_name.py > Press Enter. (.py is file extension).
Your Python code is connected with your real world project. Values of LDR depend upon the light exposed on it. These values are displayed on the serial monitor.
5. Troubleshooting
Values are not changing: Control light exposing on LDR by covering it. LDR values change on the amount of light exposed on the LDR.
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team
Robo India
https://roboindia.com
the code runs normally, but there is a problem when it gets a big light like sunlight or flashlight
is there a way to solve this problem?