Arduino Radar: Making Invisible Distances Visible With 169 Lines
A radar display makes invisible distances visible. The sensor sees one point at a time. The display shows the whole sweep at once. Building that transformation with a servo, an ultrasonic sensor, and a Python script is surprisingly straightforward.
The Arduino sweeps a servo from 15 degrees to 165 degrees and back. At each angle it triggers the HC-SR04, measures the echo duration, calculates distance, and sends the angle and distance over serial. The Python script reads the stream and plots each point on a polar radar display in real time.
The Sweep
The servo moves in one degree steps with a 30 millisecond stabilization delay at each position. The full sweep from left to right covers 150 degrees and takes about five seconds. The return sweep covers the same angles in reverse. The sensor keeps measuring in both directions.
The distance calculation is standard for the HC-SR04. A 10 microsecond trigger pulse, a timed echo pulse, and the formula duration times 0.034 divided by 2. The result in centimeters gets printed to serial alongside the angle as a comma separated pair.
The Display
The Python script converts each angle,distance pair from polar to Cartesian coordinates using sine and cosine. Pygame draws the result on a circular radar background with reference lines every 30 degrees. The current detection appears as a colored line from the center to the detected point.
A deque stores the last 50 measurements. The script draws the last five positions for each detection as connected lines. This creates a persistence effect that makes moving objects show as trails instead of blinking dots.
What I Would Change
The serial port is hardcoded in the Python script. Switching machines means editing the code. I would add auto-detection or a command line argument.
The maximum range of 80 centimeters is not shown on the display. A range ring at the edge would give context for distance readings. The reference lines show angle but not range.
The visualization draws a single continuous line through all detection points. When multiple objects are present at different angles, the line connects them and creates false shapes. Each object should produce its own trail.
It is 50 lines on the Arduino and 119 on the Python side. The hardware is two components and a servo. Together they turn serial numbers into a moving radar screen. That is a satisfying ratio of output to input.