This project demonstrates a simple mini sonar system using an Arduino, an ultrasonic sensor, and a servo motor, with real-time visualization in Processing. It is designed for beginners to learn about sensor interfacing, serial communication, and graphical visualization.
- Arduino Uno (or compatible board)
- HC-SR04 Ultrasonic Sensor
- SG90 / MG90 Servo Motor
- Jumper wires
- Breadboard (optional)
Ultrasonic Sensor
- VCC → 5V
- GND → GND
- Trig → Pin 10
- Echo → Pin 11
Servo Motor
- Signal → Pin 12
- VCC → 5V
- GND → GND
Do not power the servo directly from Arduino if it draws too much current; use an external 5V source if needed.
The Arduino code rotates the servo motor back and forth between 15° and 165° while measuring distances with the ultrasonic sensor. For each step:
- The servo moves to a new angle.
- The ultrasonic sensor measures the distance to the nearest object.
- Arduino sends a line of data over the serial port in the format:
angle= current servo angle (degrees)distance= measured distance (cm).= delimiter to signal the end of the measurement
This data is read by Processing for visualization. You can find the code in ard_ide.ino.
The Processing sketch reads the serial data from Arduino and creates a radar-like interface. Key steps include:
- Serial reading: Processing reads each line from the serial port until it encounters the
.delimiter. It extracts the angle and distance values. - Mapping coordinates: The distance is mapped to pixels on the screen, and the angle determines the direction from the radar center.
- Radar beam: A moving line represents the current servo position.
- Echo points: Detected objects are drawn as points, which gradually fade to simulate sonar blips.
- UI: A bottom bar shows the current angle and distance, along with the project name.
The radar display updates continuously, giving a dynamic visualization of detected objects.
You can find the Processing code in sketch_251108b.pde.
- Arduino rotates the servo in small increments, scanning the environment.
- The ultrasonic sensor measures distances at each angle.
- Arduino sends the measurements to Processing via serial communication.
- Processing parses the data, calculates the position of each detected object, and draws it on a radar display.
- Previous points fade over time, creating a “blip” effect similar to a real sonar.
- Connect your Arduino to the computer.
- Upload
ard_ide.inoto your Arduino board. - Open Processing and run
sketch_251108b.pde. - Update the serial port in Processing to match your Arduino (for example,
"COM3"on Windows or"/dev/tty.usbmodemXXXX"on macOS). - Observe the radar display as the servo sweeps and the ultrasonic sensor detects objects.
- The visualization works best for objects within 0–40 cm.
- Avoid pointing the sensor at soft or absorbent surfaces, which may not reflect sound reliably.
- You can adjust the servo sweep angles or speed in the Arduino code to fit your setup.
This project is ideal for beginners learning about Arduino sensors, serial communication and graphical visualization using Processing.