import time
time.sleep(0.1) # Wait for USB to become ready
from machine import Pin, time_pulse_us, PWM
import utime
import sys
time.sleep(0.1)
# LED Init
#Test for the code can run initially
#Pico LED
led = Pin("LED", Pin.OUT)
led.value(1)
time.sleep(0.5)
led.value(0)
# External LED on GP28 to know the code can run
#Samuel u can remove this untill
led = Pin(28, Pin.OUT)
led.value(True)
time.sleep(1)
led.value(False)
time.sleep(1)
#here in the final program
print("System Ready")
# Define car in each slot
areaa = ["DEA69", "DAD6969", "DBE6969", "DDB6969"]
areab = ['JUG6969', 'USM1069', 'DAV1869']
# Ultrasonic pins
# Define GPIO pins for the ultrasonic sensor (adjust these if you used different pins)
TRIGGER_PIN = 3
ECHO_PIN = 2
trigger = Pin(TRIGGER_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
# PIR Sensor on GP15
# Define GPIO pins for the pir (adjust these if you used different pins)
pir = Pin(15, Pin.IN)
# Servo motor
# Define GPIO pins for the servo motor (adjust these if you used different pins)
servo_pin = Pin(19)
pwm = PWM(servo_pin)
# Set Duty Cycle for Different Angles
max_duty = 7864 # 180degrees=Nou Used by us
min_duty = 1802 # Open=0degrees---use this for open
close_duty = 5000 # close=90degrees---use this for close
pwm.freq(50) # Set PWM frequency-pwm.freq(frequency value)
# Start with gate CLOSED
pwm.duty_u16(close_duty)
time.sleep(2)
# Function to detect PIR car-passed event
def get_car_passed():
"""
Reads PIR sensor.
PIR HIGH (1) means motion detected.
PIR LOW (0) means no motion.
"""
return pir.value() == 0
# Ultrasonic distance detection
def get_car_sensor():
def get_distance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(10)
trigger.low()
try:
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return -1
distance = (duration * 0.0343) / 2
return distance
except OSError as e:
print("Sensor error:", e)
return -1
while True:
dist = get_distance()
if dist >= 0:
if dist < 20:
return True
else:
return False
utime.sleep(0.5)
# MAIN LOOP
while True:
cs = get_car_sensor()
if cs == True:
car_plate = input("Enter plate number: ")
print("Your car plate number is " + car_plate)
if car_plate == "stop":
car_sensor = False
if car_plate in areaa:
lista = True
listb = False
elif car_plate in areab:
listb = True
lista = False
else:
lista = False
listb = False
if lista == True:
print("Access Granted, Welcome to Parking Area A")
pwm.duty_u16(min_duty) # gate open
time.sleep(1)
elif listb == True:
print("Access Granted, Welcome to Parking Area B")
pwm.duty_u16(min_duty) # gate open
time.sleep(1)
else:
print("Access Denied Unidentified Car Plate Number")
pwm.duty_u16(close_duty) # keep gate closed
time.sleep(2)
continue # restart loop (no gate movement)
# Now detect PIR for car passing
print("Waiting for PIR sensor to detect car passing...")
while True:
car_passed = get_car_passed()
if car_passed:
print("Car has passed the gate.")
break
time.sleep(0.1)
continue # restart loop ( car still at the gate)
# After PIR detects passing → close gate
pwm.duty_u16(close_duty)
time.sleep(2)
else:
# No car detected by ultrasonic → keep gate closed
pwm.duty_u16(close_duty)
time.sleep(2)
time.sleep(2)