5134+ reviews
Order by 16:00 for same day shipping
14 days return
EN
Individual
Business
In this project you will learn how to use a LDR (Light Dependent Resistor) to measure light intensity. The LDR (found in your GPIO Kit ) changes its resistance depending on the amount of light, allowing you to use this data to, for example, automatically switch an LED on or off based on the light level.
Connect the LDR (light sensor):
Connect the LED:
Connect to GND (pin 6) .
GPIO | Pin # | Function | Connection |
GPIO 17 | Pin 11 | Analog input | LDR |
GPIO 27 | Pin 13 | Digital output | LED |
3.3V | Pin 1 | Power supply 3.3V | LDR |
GND | Pin 6 | Earth (Ground) | LDR & LED |
Open the Thonny Python IDE and enter the following code:
from gpiozero import LED, MCP3008
from time import sleep
# LED en LDR koppelen
led = LED(27) # LED op GPIO 27
ldr = MCP3008(channel=0) # LDR op analoge invoer kanaal 0 (MCP3008)
while True:
light_level = ldr.value # Lees het lichtniveau (waarde tussen 0 en 1)
print(f"Lichtniveau: {light_level:.2f}")
if light_level < 0.5: # Drempelwaarde: weinig licht
led.on()
else:
led.off()
sleep(0.5)
Click File > Save As and name the file ldr_intro.py .
Click the green Run button (▶) at the top of the Thonny interface.
The terminal displays the light intensity in real time.
After this project you can continue with the next:
https:// electronicsforyou.com/project/gpio-project-6-rgb-led