Fareed Mohmed← Project library

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

Obstacle-avoiding robot car

An ultrasonic sensor looks ahead. The car drives while the way is clear, slows down when something is near, and backs away when it is too close.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Obstacle-avoiding robot car: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Drive motorL298N motor driver
Front sensorUltrasonic sensor

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)
1ColouredDrive motor - ENAPin 32 (GPIO12)
2ColouredDrive motor - IN1Pin 29 (GPIO5)
3ColouredDrive motor - IN2Pin 31 (GPIO6)
4RedDrive motor - 5VPin 2 (5V)
5BlackDrive motor - GNDPin 6 (GND)
6RedFront sensor - VCCPin 4 (5V)
7ColouredFront sensor - TRIGPin 16 (GPIO23)
8ColouredFront sensor - ECHOPin 18 (GPIO24)
9BlackFront sensor - GNDPin 9 (GND)

Python code (gpiozero)

from gpiozero import Motor, DistanceSensor
from time import sleep

motor = Motor(forward=5, backward=6, enable=12)
sensor = DistanceSensor(echo=24, trigger=23, max_distance=2)

while True:
    cm = sensor.distance * 100
    if cm > 40:
        motor.forward(0.9)                 # clear: full speed
        print("Clear,", round(cm), "cm: driving fast")
    elif cm > 15:
        motor.forward(0.4)                 # something ahead: slow down
        print("Object at", round(cm), "cm: slowing down")
    else:
        motor.stop()
        print("TOO CLOSE:", round(cm), "cm. Backing away")
        motor.backward(0.5)
        sleep(1)
        motor.stop()
    sleep(0.2)

How the code works

You will learn

  • Making decisions from a distance reading
  • Controlling motor speed and direction
  • Three zones: clear, slow, stop

Try this

  • Slide the object distance from 200 cm down to 5 cm and watch the motor and the console.
  • Change the slow-down distance to 60 cm.
  • Make the car turn instead of backing: add a second motor and run only one of them.

On a real Pi

  • The motor needs its own 6 to 12 V supply on the L298N. Join the grounds. Never power the motor from the Pi 5 V pin.
  • The HC-SR04 ECHO pin gives 5 V: use a 1 k and 2 k divider before the Pi.
  • Ultrasonic sensors miss soft cloth and thin chair legs. Add a second sensor or a bumper switch for safety.

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
4HC-SR04 ultrasonic sensor1Robu.in Amazon.in ThinkRobotics
51 k and 2 k resistors (ECHO voltage divider)2Robu.in Amazon.in ThinkRobotics
6L298N motor driver module1Robu.in Amazon.in ThinkRobotics
7TT gear motor with wheel, 6 V1Robu.in Amazon.in ThinkRobotics
84 x AA battery holder (6 V) or 9-12 V adapter
Motor supply, separate from the Pi
1Robu.in Amazon.in ThinkRobotics
9Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
10Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 32. Time-lapse cameraAll projects34. Radar sweep with servo and ultrasonic →

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.