# Smart Parking System Using Micropython ESP32
# Created by Alif Iman
# 5/12/2023
# Import necessary modules
from machine import SoftI2C, Pin, PWM
from utime import sleep, sleep_ms
from ssd1306 import SSD1306_I2C # Use correct SSD1306 import
# Pin declaration for LED
green_led = Pin(25, Pin.OUT)
red_led = Pin(32, Pin.OUT)
# Initialize the PIR sensor on pin 34 and PWM for servo motor on pin 23
PIR_sensor = Pin(34, Pin.IN)
bar = PWM(Pin(23, Pin.OUT)) # PWM servo toll bar
bar.freq(50)
# Declare pin for OLED display
i2c_oled = SoftI2C(scl=Pin(2), sda=Pin(4))
# Initialize the OLED display (using SSD1306 library)
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, i2c_oled)
# Function to move the servo motor to a specific angle
def move_servo(angle):
# Map the angle to the duty cycle for the servo motor
duty_cycle = int((angle / 180) * 65535) # Adjusted for full 0-65535 range
bar.duty_u16(duty_cycle)
# Main loop
while True:
# Check if motion is detected by the PIR sensor
if PIR_sensor.value() == 1:
print("HAVE CAR!")
red_led.on()
sleep(2)
red_led.off()
sleep(1)
# Display message on the OLED screen
oled.fill(0) # Clear the screen (black background)
oled.text('Have car', 0, 0, 1) # Display text in white
oled.show()
sleep(2)
elif PIR_sensor.value() == 0:
print("YEAYYY...YOU ARE READY TO GO!")
green_led.on()
sleep(2)
green_led.off()
sleep(1)
# Display message on the OLED screen
oled.fill(0) # Clear the screen (black background)
oled.text('HONDA CITY', 0, 0, 1)
oled.text('PEARL WHITE', 0, 10, 1)
oled.text('HAVE A NICE DAY!!!', 0, 40, 1)
oled.show()
# Move the servo motor to 180 degrees (open toll bar)
move_servo(180)
sleep(1)
# Wait for 10 seconds
sleep(10)
# Move the servo motor back to 0 degrees (close toll bar)
move_servo(0)
sleep(1)
print("NO CAR.")
# Wait for a short time to reduce CPU usage
sleep_ms(10)