SensorsIntermediate 3 hours
Ultrasonic Radar System
Mount an ultrasonic sensor on a servo and sweep 0–180°. Plot distance readings on the serial monitor or a Processing dashboard.
Required components
- Arduino UNO
- HC-SR04 Ultrasonic Sensor
- SG90 Servo Motor
- Breadboard
- Jumper Wires
Circuit diagram
Servo signal on D9. Ultrasonic Trig on D10, Echo on D11. Both modules share the Arduino 5V and GND rails.
Interactive 3D circuit viewer coming in the Virtual Lab.
Step-by-step guide
- 1
Mount HC-SR04 on top of the SG90 servo arm.
- 2
Connect servo signal to pin 9, VCC to 5V, GND to GND.
- 3
Wire HC-SR04: VCC→5V, GND→GND, Trig→Pin 10, Echo→Pin 11.
- 4
Upload the sketch and open Serial Monitor at 9600 baud.
- 5
Optional: visualize using Processing IDE radar sketch.
Code
#include <Servo.h>
Servo s;
const int trig = 10, echo = 11;
long ping() {
digitalWrite(trig, LOW); delayMicroseconds(2);
digitalWrite(trig, HIGH); delayMicroseconds(10);
digitalWrite(trig, LOW);
return pulseIn(echo, HIGH) * 0.034 / 2;
}
void setup() {
s.attach(9);
pinMode(trig, OUTPUT); pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop() {
for (int a = 0; a <= 180; a++) {
s.write(a); delay(30);
Serial.print(a); Serial.print(","); Serial.println(ping());
}
}Need help?
Stuck on a step?
Ask the Botix AI Tutor — it knows this project end-to-end.