Kulshrest Tiwari
Published © GPL3+

Automatic Parking Gate

Pico based automatic gate

BeginnerFull instructions provided1 hour96
Automatic Parking Gate

Things used in this project

Hardware components

SB Components Pico Breadboard Learning Kit
×1
SB Components Raspberry Pi Pico H
×1
SB Components 0.91” OLED Breakout
×1
SB Components Ultrasonic Distance Sensor (HC-SR04)
×1
SB Components SB Serial Servo SB-SS023 Powerful Multi-purpose Digital Servo Motor
×1

Software apps and online services

Thonny

Story

Read more

Code

ssd1306.py

Python
oled display liberary code
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces

from micropython import const
import framebuf


# register definitions
SET_CONTRAST        = const(0x81)
SET_ENTIRE_ON       = const(0xa4)
SET_NORM_INV        = const(0xa6)
SET_DISP            = const(0xae)
SET_MEM_ADDR        = const(0x20)
SET_COL_ADDR        = const(0x21)
SET_PAGE_ADDR       = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP       = const(0xa0)
SET_MUX_RATIO       = const(0xa8)
SET_COM_OUT_DIR     = const(0xc0)
SET_DISP_OFFSET     = const(0xd3)
SET_COM_PIN_CFG     = const(0xda)
SET_DISP_CLK_DIV    = const(0xd5)
SET_PRECHARGE       = const(0xd9)
SET_VCOM_DESEL      = const(0xdb)
SET_CHARGE_PUMP     = const(0x8d)


class SSD1306:
    def __init__(self, width, height, external_vcc, color=framebuf.MONO_VLSB):
        self.width = width
        self.height = height
        self.external_vcc = external_vcc
        self.pages = self.height // 8
        self.buffer = bytearray(self.pages * self.width)
        fb = framebuf.FrameBuffer(self.buffer, self.width, self.height, color)
        self.framebuf = fb
        # Provide methods for accessing FrameBuffer graphics primitives. This is a
        # workround because inheritance from a native class is currently unsupported.
        # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
        self.fill = fb.fill
        self.pixel = fb.pixel
        self.hline = fb.hline
        self.vline = fb.vline
        self.line = fb.line
        self.rect = fb.rect
        self.fill_rect = fb.fill_rect
        self.text = fb.text
        self.scroll = fb.scroll
        self.blit = fb.blit
        self.init_display()

    def init_display(self):
        for cmd in (
            SET_DISP | 0x00, # off
            # address setting
            SET_MEM_ADDR, 0x00, # horizontal
            # resolution and layout
            SET_DISP_START_LINE | 0x00,
            SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
            SET_MUX_RATIO, self.height - 1,
            SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
            SET_DISP_OFFSET, 0x00,
            SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
            # timing and driving scheme
            SET_DISP_CLK_DIV, 0x80,
            SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
            SET_VCOM_DESEL, 0x30, # 0.83*Vcc
            # display
            SET_CONTRAST, 0xff, # maximum
            SET_ENTIRE_ON, # output follows RAM contents
            SET_NORM_INV, # not inverted
            # charge pump
            SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
            SET_DISP | 0x01): # on
            self.write_cmd(cmd)
        self.fill(0)
        self.show()

    def poweroff(self):
        self.write_cmd(SET_DISP | 0x00)

    def poweron(self):
        self.write_cmd(SET_DISP | 0x01)

    def contrast(self, contrast):
        self.write_cmd(SET_CONTRAST)
        self.write_cmd(contrast)

    def invert(self, invert):
        self.write_cmd(SET_NORM_INV | (invert & 1))

    def show(self):
        x0 = 0
        x1 = self.width - 1
        if self.width == 64:
            # displays with width of 64 pixels are shifted by 32
            x0 += 32
            x1 += 32
        self.write_cmd(SET_COL_ADDR)
        self.write_cmd(x0)
        self.write_cmd(x1)
        self.write_cmd(SET_PAGE_ADDR)
        self.write_cmd(0)
        self.write_cmd(self.pages - 1)
        self.write_data(self.buffer)


class SSD1306_I2C(SSD1306):
    def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False, color=framebuf.MONO_VLSB):
        self.i2c = i2c
        self.addr = addr
        self.temp = bytearray(2)
        super().__init__(width, height, external_vcc, color)

    def write_cmd(self, cmd):
        self.temp[0] = 0x80 # Co=1, D/C#=0
        self.temp[1] = cmd
        self.i2c.writeto(self.addr, self.temp)

    def write_data(self, buf):
        self.i2c.writeto(self.addr, b'\x40' + buf)


class SSD1306_SPI(SSD1306):
    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False, color=framebuf.MONO_VLSB):
        self.rate = 10 * 1024 * 1024
        dc.init(dc.OUT, value=0)
        res.init(res.OUT, value=0)
        cs.init(cs.OUT, value=1)
        self.spi = spi
        self.dc = dc
        self.res = res
        self.cs = cs
        import time
        self.res(1)
        time.sleep_ms(1)
        self.res(0)
        time.sleep_ms(10)
        self.res(1)
        super().__init__(width, height, external_vcc, color)

    def write_cmd(self, cmd):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))
        self.cs(1)

    def write_data(self, buf):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(buf)
        self.cs(1)

main.py

Python
application code
# Import necessary libraries
from machine import Pin, I2C, PWM  # For GPIO pin control, I2C communication, and PWM for motor
from time import sleep            # For adding delays
import time                       # For precise timing calculations
import ssd1306                    # For controlling the OLED display

# Define GPIO pins for ultrasonic sensor, LEDs, and stepper motor
trigger = Pin(26, Pin.OUT)        # Trigger pin for ultrasonic sensor
echos = Pin(27, Pin.IN)           # Echo pin for ultrasonic sensor
led1 = Pin(16, Pin.OUT)           # First LED
led2 = Pin(18, Pin.OUT)           # Second LED
led3 = Pin(19, Pin.OUT)           # Third LED
led4 = Pin(15, Pin.OUT)           # Fourth LED

# Function to control the stepper motor based on distance
def stepper(detail):
    motor = PWM(Pin(1))           # Initialize motor control pin with PWM
    motor.freq(50)                # Set frequency to 50Hz
    def motion(angle):            # Function to move motor to a specific angle
        duty = int(1000 + (angle / 180) * 8000)  # Calculate duty cycle for the angle
        motor.duty_u16(duty)      # Set the motor duty cycle
    if detail < 20:               # If object is very close (< 20 cm)
        motion(60)                # Open the gate to 60 degrees
    else:                         # If object is farther
        motion(135)               # Open the gate to 135 degrees

# Function to display the distance on the OLED screen
def action(detail):
    tet = f"{detail:.2f} cm"      # Format the distance to two decimal places
    i2c = I2C(0, scl=Pin(21), sda=Pin(20))  # Initialize I2C communication for OLED
    oled = ssd1306.SSD1306_I2C(128, 32, i2c)  # Create an OLED object with resolution 128x32
    oled.fill(0)                  # Clear the OLED screen
    oled.text(tet, 40, 10)        # Display the distance in the center
    oled.show()                   # Show the updated display

# Function to control LEDs based on distance
def led_present(distance):
    if distance < 20:             # Very close (< 20 cm)
        led4.on()                 # Turn on all LEDs
        led2.on()
        led3.on()
        led1.on()
    elif 20 < distance < 40:      # Close (20-40 cm)
        led4.off()                # Turn off LED 4, keep others on
        led3.on()
        led1.on()
        led2.on()
    elif 40 < distance < 70:      # Medium (40-70 cm)
        led4.off()                # Turn off LEDs 3 and 4, keep others on
        led1.on()
        led2.on()
        led3.off()
    elif 70 < distance < 100:     # Far (70-100 cm)
        led4.off()                # Turn off LEDs 2, 3, and 4, keep LED 1 on
        led1.on()
        led2.off()
        led3.off()
    else:                         # Very far (> 100 cm)
        led1.off()                # Turn off all LEDs
        led2.off()
        led3.off()
        led4.off()

# Function to calculate distance using the ultrasonic sensor
def duri(echos):
    trigger.value(0)              # Ensure the trigger is low
    sleep(0.002)                  # Wait for 2 ms
    trigger.value(1)              # Send a 10 µs pulse to trigger
    sleep(0.00001)                # Keep trigger high for 10 µs
    trigger.value(0)              # Set trigger low again
    while echos.value() == 0:     # Wait for the echo to go high
        pass
    start = time.ticks_us()       # Record the start time
    while echos.value() == 1:     # Wait for the echo to go low
        pass
    end = time.ticks_us()         # Record the end time
    duration = time.ticks_diff(end, start)  # Calculate the duration of the echo
    distance = (duration * 0.0343) / 2  # Convert duration to distance in cm
    return distance

# Main loop to run the program
while True:
    a = duri(echos)               # Measure the distance
    print(a)                      # Print the distance for debugging
    led_present(a)                # Control LEDs based on distance
    action(a)                     # Update the OLED display
    stepper(a)                    # Move the stepper motor based on distance
    sleep(1)                      # Wait for 1 second before repeating

Credits

Kulshrest Tiwari
5 projects • 2 followers
embedded sofware engineer
Contact

Comments

Please log in or sign up to comment.