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.
Wiring
- 1Servo signal (orange/yellow) → pin 11.
- 2Servo power (red) → a regulated 5V supply. Use a regulated supply when possible — do not connect an unknown-voltage battery directly to the servo.
- 3Connect servo GND and Arduino GND together. One shared ground is essential.
- 4HC-SR04 VCC → 5V, GND → GND.
- 5HC-SR04 Trig → pin 10, Echo → pin 9.
Step-by-Step Instructions
- 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
Power the servo properly
Use a regulated 5V supply for the servo when possible. An SG90 can pull 600–700 mA when it stalls, which is more than the Arduino's 5V pin can supply safely. Give the servo its own regulated 5V source and connect servo GND and Arduino GND together. Do not connect an unknown-voltage battery directly to the servo.
- 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
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
Read the output
Open the Serial Monitor and watch pairs like 90,42 — angle 90 degrees, 42 centimetres away.
Arduino Code
#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 / 2Turns 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
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.
Up next
🌡️ Mini Weather Station