print("Hello, ESP32!")
from machine import Pin, PWM, ADC
from time import sleep
from machine import Pin, PWM
import ultrasonic_sensor
from utime import sleep
# Pin declaration
TRIG = Pin(25)
ECHO = Pin(33)
SERVO_PIN = 13 # Assuming servo is connected to pin 13
MOTION_THRESHOLD = 10 # Adjust this threshold as needed
# Create object for sensor
sensor = ultrasonic_library.HCSR04(trigger_pin=TRIG, echo_pin=ECHO, echo_timeout_us=500*2*30)
# Initialize servo
servo = PWM(Pin(SERVO_PIN), freq=50)
servo.duty(0) # Start with the servo in locked position
# Function to check for motion
def detect_motion(threshold):
# Measure distance
distance_cm = sensor.distance_cm()
# Check if distance is below threshold
if distance_cm < threshold:
return True # Motion detected
else:
return False # No motion detected
# Main program
while True:
if detect_motion(MOTION_THRESHOLD):
# Motion detected, unlock the door
servo.duty(90) # Adjust the angle as needed
else:
# No motion detected, lock the door
servo.duty(0) # Lock position
# Add a small delay before next measurement
sleep(1)