Ask a parent, teacher, or trusted adult before building, powering a circuit, using tools or batteries, or buying parts. Purchases are optional and at your own cost. If anything gets hot, smells burnt, leaks, swells, sparks, or moves unexpectedly, switch off or disconnect power without touching the damaged or hot part, then tell an adult. Read safety rules

All projects
📡🟡 Intermediate 1.5 hrs

Ultrasonic Scanner

A sweeping ultrasonic scanner that measures distance while a servo turns the sensor left and right.

What You'll Build

You will build a miniature radar station. A servo slowly sweeps an ultrasonic sensor from 15° to 165° and back, and at every angle the sensor measures how far away the nearest object is. The angle and distance pair gets printed to the Serial Monitor so you can watch objects appear and disappear as the beam passes over them.

What You Need

  • ✓Arduino Uno
  • ✓HC-SR04 ultrasonic sensor
  • ✓SG90 servo
  • ✓Breadboard
  • ✓Jumper wires

How It Works

The HC-SR04 works like a bat. It sends out a burst of sound too high for humans to hear, then measures how long the echo takes to come back. Because sound travels about 0.034 cm per microsecond, distance equals time divided by two, multiplied by that speed. The servo simply rotates the sensor so you can scan a whole area instead of one direction.

Before You Wire It

Ask a parent, teacher, or trusted adult before building, powering a circuit, using tools or batteries, or buying parts. Purchases are optional and at your own cost. If anything gets hot, smells burnt, leaks, swells, sparks, or moves unexpectedly, switch off or disconnect power without touching the damaged or hot part, then tell an adult. Read safety rules

Wiring

  1. 1Servo signal (orange/yellow) → pin 11.
  2. 2Servo power (red) → a regulated 5V supply. Use a regulated supply when possible — do not connect an unknown-voltage battery directly to the servo.
  3. 3Connect servo GND and Arduino GND together. One shared ground is essential.
  4. 4HC-SR04 VCC → 5V, GND → GND.
  5. 5HC-SR04 Trig → pin 10, Echo → pin 9.

Step-by-Step Instructions

  1. 1

    Mount the sensor

    Tape or glue the ultrasonic sensor onto the servo horn so the two 'eyes' face forward. Keep the wires loose enough to turn.

  2. 2

    Power the servo properly

    Use a regulated 5V supply sized for your exact servo. Stall current varies by model and can exceed what the Arduino's 5V pin or a computer USB port should supply. Ask an adult to check the servo datasheet, give it a suitable regulated source, and connect servo GND and Arduino GND together. Never connect an unknown battery directly.

  3. 3

    Test distance alone

    Before adding movement, upload a short sketch that just prints the distance. Wave your hand and check the numbers make sense.

  4. 4

    Add the sweep

    Now let the servo step one degree at a time. A short delay after each step gives the servo time to actually arrive before you measure.

  5. 5

    Read the output

    Open the Serial Monitor and watch pairs like 90,42 — angle 90 degrees, 42 centimetres away.

Arduino Code

ultrasonic-scanner.ino
#include <Servo.h>

Servo radar;
const int TRIG = 10;
const int ECHO = 9;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  radar.attach(11);
  Serial.begin(9600);
}

int readDistance() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long duration = pulseIn(ECHO, HIGH);
  return duration * 0.034 / 2;
}

void loop() {
  for (int angle = 15; angle <= 165; angle++) {
    radar.write(angle);
    delay(30);
    Serial.print(angle);
    Serial.print(",");
    Serial.println(readDistance());
  }
  for (int angle = 165; angle > 15; angle--) {
    radar.write(angle);
    delay(30);
    Serial.print(angle);
    Serial.print(",");
    Serial.println(readDistance());
  }
}

Understanding the Code

#include <Servo.h>

Loads Arduino's servo library so you can command angles instead of writing tricky pulse code yourself.

delayMicroseconds(10)

The HC-SR04 needs a 10 microsecond pulse on Trig to fire its sound burst.

pulseIn(ECHO, HIGH)

Waits and measures how long the Echo pin stays HIGH — that is the round trip time of the sound.

duration * 0.034 / 2

Turns microseconds into centimetres, halved because the sound travelled there and back.

for (int angle = 15; ...)

A loop that counts the angle up one step at a time, sweeping the servo.

Challenges & Upgrades

Trigger a buzzer when something comes closer than 20 cm.
Draw the radar on your computer using Processing or p5.js.
Speed up the sweep and see how it affects accuracy.

Troubleshooting

😖 Distance reads 0 or huge numbers

Fix: Trig and Echo may be swapped, or the object is closer than 2 cm — the sensor's minimum range.

😖 Servo buzzes and resets the board

Fix: It is pulling too much current. Power it separately and connect the grounds.

Helpful Resources

Want extra help? Build a virtual circuit in Tinkercad or watch a video guide for this project. These are optional external resources.

Try it in Tinkercad

Open Tinkercad Circuits to recreate and test the circuit online before using real parts.

Open Tinkercad

Watch video guides

Browse video tutorials for this exact project and compare how each part is connected.

Find videos

Outside examples may use different pins or parts. Follow this page’s wiring and safety instructions if they do not match. Ask a trusted adult before following an outside guide or buying anything; external parts, advice, and costs are your responsibility.

Up next

🌡️ Mini Weather Station

Build This