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.

Parts in this circuit
| Part | Type |
|---|---|
| Gate arm | Servo |
| Car sensor | Ultrasonic sensor |
| Closed lamp | LED |
| Open lamp | LED |
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.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Gate arm - signal | Pin 11 (GPIO17) |
| 2 | Red | Gate arm - 5 V | Pin 2 (5V) |
| 3 | Black | Gate arm - GND | Pin 6 (GND) |
| 4 | Red | Car sensor - VCC | Pin 4 (5V) |
| 5 | Coloured | Car sensor - TRIG | Pin 16 (GPIO23) |
| 6 | Coloured | Car sensor - ECHO | Pin 18 (GPIO24) |
| 7 | Black | Car sensor - GND | Pin 9 (GND) |
| 8 | Coloured | Closed lamp - anode + | Pin 16 (GPIO23) |
| 9 | Black | Closed lamp - cathode - | Pin 14 (GND) |
| 10 | Coloured | Open lamp - anode + | Pin 15 (GPIO22) |
| 11 | Black | Open 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
- car_near() returns True when the object is closer than 40 cm.
- The inner while loop keeps the gate open for as long as the car is there.
- After the car has gone, a 2 second delay gives a safety margin before closing.
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.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 5 mm LED, green | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 330 ohm resistor, 1/4 W One per LED | 2 | Robu.in Amazon.in ThinkRobotics |
| 7 | HC-SR04 ultrasonic sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | 1 k and 2 k resistors (ECHO voltage divider) | 2 | Robu.in Amazon.in ThinkRobotics |
| 9 | SG90 micro servo | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
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.