Here's a Python program for an LDR-based street light controller using an Arduino UNO:
Program Code:
# Import necessary libraries import time import RPi.GPIO as GPIO # Set pin numbering mode GPIO.setmode(GPIO.BOARD) # Define pin for LDR input LDR_PIN = 7 # Define pin for street light output LIGHT_PIN = 11 # Set up LDR pin as input and street light pin as output GPIO.setup(LDR_PIN, GPIO.IN) GPIO.setup(LIGHT_PIN, GPIO.OUT) # Main loop while True: # Read LDR value ldr_value = GPIO.input(LDR_PIN) # If ambient light level is low, turn on street light if ldr_value == 0: GPIO.output(LIGHT_PIN, GPIO.HIGH) # Otherwise, turn off street light else: GPIO.output(LIGHT_PIN, GPIO.LOW) # Wait for a short amount of time to prevent constantly reading LDR value time.sleep(0.1)
In this program, we first import the necessary libraries (time and RPi.GPIO). We then set the pin numbering mode to BOARD. The LDR input pin and the street light output pin are defined as LDR_PIN and LIGHT_PIN, respectively. We then set up the LDR pin as input and the street light pin as output.
The main loop continuously reads the LDR value using the GPIO.input()
function. If the ambient light level is low (the LDR value is 0), the street light is turned on using the GPIO.output()
function with GPIO.HIGH
as the second parameter. If the ambient light level is high (the LDR value is 1), the street light is turned off using the GPIO.output()
function with GPIO.LOW
as the second parameter.
We also add a short delay using the time.sleep()
function to prevent the program from constantly reading the LDR value.
To connect the components, follow these steps:
- Connect one leg of the LDR to the Arduino UNO's 5V pin.
- Connect the other leg of the LDR to a 10k ohm resistor.
- Connect the other end of the 10k ohm resistor to the Arduino UNO's analog input pin A0.
- Connect one end of a jumper wire to the same row as the LDR's other leg and the 10k ohm resistor.
- Connect the other end of the jumper wire to a ground (GND) pin on the Arduino UNO.
- Connect the positive wire (red) of the LED to a digital output pin on the Arduino UNO (in this example, pin 13).
- Connect the negative wire (black) of the LED to a resistor.
- Connect the other end of the resistor to the ground (GND) pin on the Arduino UNO.
No comments:
Post a Comment