from machine import Pin,ADC
from machine import Pin
from time import sleep, time # Import the 'time' module for timestamps
LED = Pin(14, Pin.OUT)
PIR_sensor = Pin(13, Pin.IN, Pin.PULL_UP)
LED.low()
sleep(3)
# Function to log motion events to the "log.txt" file
def log_motion_event():
timestamp = time()
with open("log.txt", "a") as log_file:
log_file.write("Motion Detected at: {}\n".format(timestamp))
while True:
print(PIR_sensor.value())
if PIR_sensor.value() == 0:
print("Motion Detected! -> LED is now ON")
log_motion_event() # Log the motion event with timestamp
LED.high()
sleep(5)
else:
print("No motion detected -> LED is OFF")
LED.low()
sleep(1)