# Project objective: Control the brightness of an LED based on two conditions: light level, motion detected.
#
# Hardware and connections used:
# LED to GPIO Pin 15
# 220 ohm resistor for the LED
# PIR Motion sensor to GPIO Pin 16
# LDR Sensor to GPIO Pin 28
# modules
from machine import Pin, PWM, ADC # PWM class is needed to create a PWM object
from time import sleep
# analogue to digital conversion on pin 28 for LDR
LDR = ADC(28)
# creating a PWM object, specifying GPIO Pin 15 as OUT
pwm = PWM(Pin(15))
# creating a PIR object, setting it as IN
pir = Pin(16, Pin.IN)
# setting initial frequency of PWM output at 1000 Hz
pwm.freq(1000)
# continuously read data from the PIR sensor
# print a status whether a motion or detected or not and set LED PWM based on LDR reading
while True:
if pir.value() == 1:
print(f"Motion")
value = LDR.read_u16()
pwm.duty_u16(value)
else:
print(f"No motion")
pwm.duty_u16(0)
# PIR sensor would check for movement every second
sleep(1)