from machine import Pin, PWM
from time import sleep
# Servo motor setup on GPIO 10
servo = PWM(Pin(10))
servo.freq(50) # Set frequency to 50Hz (important for servo control)
# PIR Sensor setup on GPIO 27
pir = Pin(27, Pin.IN)
# Function to set servo position (0° to 180°)
def set_servo_angle(angle):
min_duty = 2000 # Approx. 0° position
max_duty = 8000 # Approx. 180° position
duty = min_duty + (angle / 180) * (max_duty - min_duty)
servo.duty_u16(int(duty))
while True:
if pir.value() == 1: # Motion detected
print("Motion detected! Gate opening...")
# Move servo to 90° (Halfway open)
set_servo_angle(90)
sleep(1)
print("Welcome to Party")
sleep(5) # Keep gate open for 10 seconds
else: # No motion detected
print("No motion detected. Gate closing...")
print("Sorry, No permission")
sleep(5) # Delay before checking again
# Move servo back to 0° (Closed)
set_servo_angle(0)
sleep(1)
sleep(1) # Check motion every second