# Import necessary modules
import machine
import time
# Define pins for PIR sensor, LED, and LDR specific to ESP32-C3-Mini
PIR_PIN = 7 # Adjust as per your ESP32-C3-Mini pin configuration
LED_PIN = 3 # Adjust as per your ESP32-C3-Mini pin configuration
LDR_PIN = 0 # Adjust as per your ESP32-C3-Mini pin configuration
# Initialize the PIR sensor, LED, and LDR
pir = machine.Pin(PIR_PIN, machine.Pin.IN)
led = machine.Pin(LED_PIN, machine.Pin.OUT)
ldr = machine.ADC(machine.Pin(LDR_PIN))
ldr.atten(machine.ADC.ATTN_11DB) # Configure ADC for 0-3.3V range
SIMULATE = "OFF"
# Define a threshold for darkness (adjust based on your LDR readings)
LDR_THRESHOLD = 150
# Delay before turning off the LED after no motion (in seconds)
DELAY_TIME = 1
# Function to check if it's dark
def is_dark():
print("LDR: " + str(ldr.read() > LDR_THRESHOLD))
return ldr.read() > LDR_THRESHOLD
# Main loop
try:
while True:
if pir.value() == 1: # Motion detected
print("PIR: true")
if is_dark():
led.value(1) # Turn on LED
SIMULATE = "ON"
time.sleep(3) # Small delay to prevent rapid toggling
else:
#print("-")
time.sleep(DELAY_TIME)
if pir.value() == 0: # Recheck motion to avoid premature LED off
led.value(0) # Turn off LED
SIMULATE = "OFF"
print("LDR: " + str(ldr.read()) + " LED should be: " + SIMULATE)
except KeyboardInterrupt:
# Cleanup on exit
led.value(0)
print("Exiting gracefully...")
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1