Fareed Mohmed← Project library

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

Radar sweep with servo and ultrasonic

A servo turns the ultrasonic sensor from left to right. The Pi prints a text radar line for each angle and beeps when something is close.

▶ Open in the simulator

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

Parts in this circuit

PartType
Scan servoServo
ScannerUltrasonic sensor
Warning buzzerBuzzer

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)
1ColouredScan servo - signalPin 11 (GPIO17)
2RedScan servo - 5 VPin 2 (5V)
3BlackScan servo - GNDPin 6 (GND)
4RedScanner - VCCPin 4 (5V)
5ColouredScanner - TRIGPin 16 (GPIO23)
6ColouredScanner - ECHOPin 18 (GPIO24)
7BlackScanner - GNDPin 9 (GND)
8ColouredWarning buzzer - +Pin 12 (GPIO18)
9BlackWarning buzzer - -Pin 14 (GND)

Python code (gpiozero)

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

servo = AngularServo(17, min_angle=-90, max_angle=90)
sensor = DistanceSensor(echo=24, trigger=23, max_distance=2)
buzzer = Buzzer(18)

def scan(angles):
    for angle in angles:
        servo.angle = angle
        sleep(0.4)                            # let the servo arrive
        cm = sensor.distance * 100
        print("%4d deg  %3d cm  %s" % (angle, cm, "#" * int(cm / 10)))
        if cm < 30:
            buzzer.beep(0.1, 0.1, n=2)

while True:
    scan(range(-90, 91, 30))                  # sweep left to right
    scan(range(90, -91, -30))                 # and back

How the code works

You will learn

  • Moving a sensor with a servo
  • Making a table of readings
  • Writing your own function with a list of angles

Try this

  • Change the Object distance slider while the sweep runs and watch the bars.
  • Use a step of 15 degrees for a finer scan.
  • Print only the angle with the closest object after each sweep.

On a real Pi

  • On a real robot each angle sees a different object. Here one distance value is used for all angles, so the bars change together.
  • A cheap servo shakes the sensor. Wait until it stops before you measure.
  • Keep the sensor wires loose enough to turn with the servo.

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
4Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
5HC-SR04 ultrasonic sensor1Robu.in Amazon.in ThinkRobotics
61 k and 2 k resistors (ECHO voltage divider)2Robu.in Amazon.in ThinkRobotics
7SG90 micro servo1Robu.in Amazon.in ThinkRobotics
8Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
9Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 33. Obstacle-avoiding robot carAll projects35. Pick-and-place robot arm (2 servos) →

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.