import machine # Import the machine module for GPIO operations
import time # Import the time module for sleep functions
# Define GPIO pins
LDR_PIN = 17 # GPIO pin connected to the LDR sensor
LED_PIN = 18 # GPIO pin connected to the LED
# Set up GPIO pins
ldr = machine.Pin(LDR_PIN, machine.Pin.IN) # Configure LDR pin as input
led = machine.Pin(LED_PIN, machine.Pin.OUT) # Configure LED pin as output
# Main loop
while True:
# Read LDR sensor value
ldr_value = ldr.value()
# If it's dark (LDR value is 1), turn on the LED
# Otherwise, turn off the LED
if ldr_value == 1:
led.on() # Turn on the LED
print("The street light is ON")
else:
led.off() # Turn off the LED
print("The street light is OFF")
# Pause for a short duration to avoid rapid toggling
time.sleep(0.5)
'''An LDR, or Light Dependent Resistor, is a type of resistor whose resistance varies with the amount of light falling on it. It belongs to the category of resistors known as photoresistors. The working principle of an LDR is based on the variation of its resistance concerning the incident light.
Here's how an LDR works:
1. **Material Composition:**
- An LDR is typically made of semiconductor materials, often cadmium sulfide (CdS) or cadmium selenide (CdSe).
2. **Photocell Behavior:**
- When light falls on the LDR, photons interact with the semiconductor material and release electrons. The conductivity of the semiconductor material increases as more electrons are released.
3. **Resistance Variation:**
- The increase in conductivity results in a decrease in the resistance of the LDR. Conversely, when there is little or no light, the conductivity decreases, and the resistance increases.
4. **Inverse Relationship:**
- The relationship between light intensity and resistance is generally inverse. Higher light intensity corresponds to lower resistance, and lower light intensity corresponds to higher resistance.
5. **Electrical Circuit Integration:**
- LDRs are often used in electrical circuits as light sensors. They are connected in series or parallel with other electronic components to modulate the flow of current based on the ambient light conditions.
6. **Application in Circuits:**
- In applications such as streetlights or automatic lighting systems, LDRs are used to detect the ambient light level. When it gets dark (low light intensity), the resistance of the LDR increases, leading to changes in the circuit and triggering the activation of lights or other components.
7. **Response Time:**
- The response time of an LDR is relatively slow compared to other light sensors. Therefore, it may not be suitable for applications where rapid changes in light conditions need to be detected.
8. **Spectral Sensitivity:**
- The spectral sensitivity of an LDR depends on the material used. Different LDRs may exhibit variations in their response to different wavelengths of light.
In summary, LDRs are passive components that change resistance based on the incident light intensity. This property makes them useful for various applications, including light sensing in electronic circuits, exposure control in cameras, and ambient light detection in automatic lighting systems.'''