5101+ reviews
Order by 16:00 for same day shipping
14 days return
GB
EN
Individual
Business
In this project you will learn how to use an LDR (Light Dependent Resistor) to measure light intensity. The LDR 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 toGND (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 theThonny Python IDE and see the following code in:
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 greenRun button (▶) at the top the Thonny interface.
The terminal displays the light intensity in real time.