-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS22_Light_Dependent_Resistor.py
More file actions
50 lines (41 loc) · 1.52 KB
/
S22_Light_Dependent_Resistor.py
File metadata and controls
50 lines (41 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File: main.py
# Author: Armstrong Subero
# Platform: Raspberry Pi Pico (RP2040) with MicroPython
# Program: S22_Light_Dependent_Resistor
# Interpreter: MicroPython (latest version)
# Program Version: 1.0
#
# Program Description: This program reads the value from a Light Dependent Resistor (LDR)
# connected to an analog pin on the Raspberry Pi Pico. It turns an LED
# on or off based on the light intensity detected by the LDR.
#
# Hardware Description:
# LDR connected to ADC0 (GPIO 26) in a voltage divider configuration with a resistor.
# LED connected to GPIO 16 via a 1K resistor.
#
# Created: August 28th, 2024, 12:22 PM
# Last Updated: August 28th, 2024, 12:22 PM
from machine import ADC, Pin
import utime
# Initialize the LDR on ADC0 (GPIO 26)
ldr = ADC(Pin(26))
# Initialize the LED on GPIO 16
led = Pin(16, Pin.OUT)
# Set a threshold value for light intensity (adjust this value based on your setup)
threshold = 20000
# Function to read the LDR value and control the LED
def control_led():
while True:
# Read the LDR value (0-65535)
light_value = ldr.read_u16()
# Print the light value for debugging
print("Light Value:", light_value)
# Turn the LED on if the light intensity is below the threshold
if light_value < threshold:
led.on()
else:
led.off()
# Sleep for a short time before reading again
utime.sleep(0.5)
# Run the LED control function
control_led()