Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 36 of 47 · Robotics · Intermediate

Automatic gate with ultrasonic sensor

A car comes close and the gate arm lifts. It stays open while the car is there, waits two seconds, then closes. A red and a green lamp show the state.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Automatic gate with ultrasonic sensor: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Gate armServo
Car sensorUltrasonic sensor
Closed lampLED
Open lampLED

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredGate arm - signalPin 11 (GPIO17)
2RedGate arm - 5 VPin 2 (5V)
3BlackGate arm - GNDPin 6 (GND)
4RedCar sensor - VCCPin 4 (5V)
5ColouredCar sensor - TRIGPin 16 (GPIO23)
6ColouredCar sensor - ECHOPin 18 (GPIO24)
7BlackCar sensor - GNDPin 9 (GND)
8ColouredClosed lamp - anode +Pin 16 (GPIO23)
9BlackClosed lamp - cathode -Pin 14 (GND)
10ColouredOpen lamp - anode +Pin 15 (GPIO22)
11BlackOpen lamp - cathode -Pin 20 (GND)

Python code (gpiozero)

from gpiozero import AngularServo, DistanceSensor, LED
from time import sleep

gate = AngularServo(17, min_angle=-90, max_angle=90)    # 0 = closed, 90 = open
sensor = DistanceSensor(echo=24, trigger=23, max_distance=2)
red = LED(23)
green = LED(22)

def car_near():
    return sensor.distance * 100 < 40

def close_gate():
    gate.angle = 0
    red.on()
    green.off()
    print("Gate CLOSED")

def open_gate():
    gate.angle = 90
    red.off()
    green.on()
    print("Gate OPEN")

close_gate()
while True:
    if car_near():
        open_gate()
        while car_near():             # stay open while the car is in the way
            sleep(0.2)
        sleep(2)                      # extra time for the car to pass
        close_gate()
    sleep(0.2)

How the code works

You will learn

  • Functions that return True or False
  • Waiting for a condition to end
  • Safety logic: never close on a car

Try this

  • Slide the Car sensor distance below 40 cm, then back up again.
  • Slide it down and up again quickly: does the gate close on the car?
  • Add a buzzer that beeps while the gate is moving.

On a real Pi

  • A real gate needs a safety edge or a photo-beam that stops it if something is in the way. Do not rely on one ultrasonic sensor.
  • Use a proper gate motor with its own controller. The Pi only sends open and close signals.
  • Mains-powered gate motors must be wired by a qualified electrician.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
55 mm LED, green1Robu.in Amazon.in ThinkRobotics
6330 ohm resistor, 1/4 W
One per LED
2Robu.in Amazon.in ThinkRobotics
7HC-SR04 ultrasonic sensor1Robu.in Amazon.in ThinkRobotics
81 k and 2 k resistors (ECHO voltage divider)2Robu.in Amazon.in ThinkRobotics
9SG90 micro servo1Robu.in Amazon.in ThinkRobotics
10Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
11Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 35. Pick-and-place robot arm (2 servos)All projects37. Simon memory game →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.