Arduino Radar Project Setup
Components Needed:
- Arduino Mega 2560
- HC-SR04 Ultrasonic Sensor
- SG90 Servo Motor (or similar)
- Jumper wires
- Breadboard
- USB cable
Wiring Connections:
HC-SR04 Ultrasonic Sensor:
- VCC → 5V on Arduino
- GND → GND on Arduino
- Trig → Digital Pin 9
- Echo → Digital Pin 10
SG90 Servo Motor:
- Red (VCC) → 5V on Arduino
- Brown/Black (GND) → GND on Arduino
- Orange/Yellow (Signal) → Digital Pin 6
How It Works:
- Servo Sweep: The servo motor rotates from 0° to 180° and back, carrying the ultrasonic sensor
- Distance Measurement: At each angle, the ultrasonic sensor measures the distance to any objects
- Data Transmission: The Arduino sends angle and distance data via serial communication
- Visualization: The data can be displayed on the radar screen visualization
Key Features:
- 180° scanning range with 2-degree increments
- Distance range up to 200cm (adjustable in code)
- Real-time data transmission via Serial Monitor
- Smooth servo movement with proper delays
- Error handling for out-of-range measurements
Usage Instructions:
- Upload the Arduino code to your Arduino Mega
- Connect components according to the wiring diagram
- Open Serial Monitor (9600 baud rate) to see raw data
- Use the radar visualization by opening the HTML file in a web browser for a graphical display
Customization Options:
- Adjust scanning speed: Change the delay values in the main loop
- Modify angle increment: Change the
direction * 2value for finer/coarser steps - Set distance limits: Modify the maximum distance in
measureDistance() - Add object detection: Implement logic to detect and track specific objects
The project outputs data in CSV format (angle,distance) which can be easily processed by other applications or the provided visualization interface. The radar will continuously sweep and display objects in real-time, creating a classic radar sweep effect!
#include <Servo.h>
// Create servo object
Servo radarServo;
// Define pins
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 6;
// Variables
int angle = 0;
int direction = 1; // 1 for forward, -1 for backward
long duration;
int distance;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Attach servo
radarServo.attach(servoPin);
// Move servo to starting position
radarServo.write(0);
delay(1000);
Serial.println("Radar System Started");
}
void loop() {
// Move servo to current angle
radarServo.write(angle);
delay(30); // Small delay for servo to reach position
// Measure distance
distance = measureDistance();
// Send data to serial in format: angle,distance
Serial.print(angle);
Serial.print(",");
Serial.println(distance);
// Update angle for next iteration
angle += direction * 2; // Move 2 degrees at a time
// Change direction when reaching limits
if (angle >= 180) {
direction = -1;
angle = 180;
} else if (angle <= 0) {
direction = 1;
angle = 0;
}
delay(50); // Delay between measurements
}
int measureDistance() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send 10 microsecond pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin
duration = pulseIn(echoPin, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
// Limit maximum distance reading
if (distance > 200) {
distance = 200;
}
return distance;
}https://claude.ai/public/artifacts/18e65028-67d3-48ac-90d5-0d29d26b006b
Setup Instructions
Step 1: Upload Arduino Code
- Upload the Arduino radar code I provided earlier to your Arduino Mega
- Open the Serial Monitor to verify it's sending data in format:
angle,distance
Step 2: Choose Your Visualization Method
Option A: Processing IDE (Easiest)
- Download Processing IDE from processing.org
- Create a new sketch and paste the Processing radar code
- Find your serial port:
- Run the sketch once to see available ports in the console
- Note the port number (usually 0 for the first Arduino)
Modify the connection line if needed:
javamyPort = new Serial(this, Serial.list()[0], 9600); // Change [0] to correct index- Run the sketch and you should see the radar display!
Option B: Python with Pygame
Install required libraries:
bashpip install pygame pyserial- Find your serial port:
- Windows: Usually
COM3,COM4, etc. (check Device Manager) - Linux: Usually
/dev/ttyUSB0or/dev/ttyACM0 - Mac: Usually
/dev/cu.usbmodem...or/dev/cu.usbserial...
- Windows: Usually
Update the serial port in the Python code:
pythonradar = RadarDisplay(serial_port='YOUR_PORT_HERE', baud_rate=9600)- Run the Python script
Step 3: Troubleshooting
If no data appears:
- Check serial port connection
- Verify Arduino is sending data (use Serial Monitor)
- Make sure baud rates match (9600)
- Try different serial port numbers/names
Common Serial Ports:
- Windows:
COM1,COM3,COM4, etc. - Linux:
/dev/ttyACM0,/dev/ttyUSB0 - Mac:
/dev/cu.usbmodem14101,/dev/cu.usbserial-1410
Features You'll Get:
✅ Real-time radar sweep just like the simulation
✅ Object detection shown as red dots
✅ Fading trail effects for smooth visualization
✅ Live data display showing current angle/distance
✅ Grid overlay with distance markers
✅ Object persistence to track detected items
The Processing version is generally easier to set up and more beginner-friendly, while the Python version offers more customization options. Both will give you the exact radar visualization effect from the simulation!
Pro tip: Start with Processing first since it's designed specifically for Arduino projects and handles serial communication very smoothly.
Detailed Step-by-Step Instructions for Processing IDE Radar Display
Step 1: Download and Install Processing IDE
- Go to the Processing website:
- Open your web browser
- Navigate to https://processing.org/download
- Download Processing:
- Click the "Download Processing" button
- Choose your operating system (Windows, Mac, or Linux)
- The download will start automatically (file will be around 100-200MB)
- Install Processing:
- Windows: Run the
.exefile and follow the installation wizard - Mac: Open the
.dmgfile and drag Processing to your Applications folder - Linux: Extract the
.tar.gzfile to your preferred location
- Windows: Run the
- Launch Processing:
- Open Processing from your Start Menu (Windows), Applications (Mac), or extracted folder (Linux)
- You should see a white text editor window with a toolbar at the top
Step 2: Prepare Your Arduino
- Make sure your Arduino is connected to your computer via USB
- Verify the Arduino code is uploaded and working:
- Open Arduino IDE
- Go to Tools → Serial Monitor
- Set baud rate to 9600
- You should see data like:
0,45then2,47then4,52etc. - If you don't see data, re-upload the Arduino radar code
Step 3: Create the Processing Sketch
- Open Processing (if not already open)
- Clear the default code:
- You'll see some sample code in the editor
- Select all text (
Ctrl+Aon Windows/Linux,Cmd+Aon Mac) - Delete it all
- Copy the radar code:
- Go back to the Processing radar code I provided earlier
- Select ALL the code (
Ctrl+AorCmd+A) - Copy it (
Ctrl+CorCmd+C)
- Paste into Processing:
- Click in the Processing editor window
- Paste the code (
Ctrl+VorCmd+V) - The editor should now be filled with the radar visualization code
Step 4: Find Your Serial Port (IMPORTANT!)
- Run the sketch to check available ports:
- Click the Play button (triangle icon) in the Processing toolbar
- OR press
Ctrl+R(Windows/Linux) orCmd+R(Mac)
- Check the console output:
- Look at the black console area at the bottom of the Processing window
You should see something like:
Available serial ports: 0: COM3 1: COM4 2: COM7OR on Mac/Linux:
0: /dev/cu.usbmodem14101 1: /dev/cu.Bluetooth-Incoming-Port 2: /dev/ttyACM0
- Identify your Arduino port:
- Windows: Usually
COM3,COM4,COM5, etc. - Mac: Usually starts with
/dev/cu.usbmodemor/dev/cu.usbserial - Linux: Usually
/dev/ttyACM0or/dev/ttyUSB0 - Note the number (0, 1, 2, etc.) next to your Arduino's port
- Windows: Usually
Step 5: Configure the Serial Port
- Stop the current sketch (click the Stop button - square icon)
- Find the serial connection line in the code:
Look for this line (around line 40):
javamyPort = new Serial(this, Serial.list()[0], 9600);
- Change the port number if needed:
- If your Arduino was listed as "0:" in step 4, keep
[0] - If your Arduino was listed as "1:", change to
[1] - If your Arduino was listed as "2:", change to
[2] Example: If your Arduino is on port 1, change the line to:
javamyPort = new Serial(this, Serial.list()[1], 9600);
- If your Arduino was listed as "0:" in step 4, keep
Step 6: Run the Radar Display
- Save your sketch (
Ctrl+SorCmd+S)- Give it a name like "ArduinoRadar"
- Choose a location to save it
- Run the sketch again:
- Click the Play button or press
Ctrl+R/Cmd+R
- Click the Play button or press
- Check for success:
- A new window should open showing the radar display
- You should see:
- Green radar grid with concentric circles
- Angle lines at 30-degree intervals
- A green sweep line moving back and forth
- Red dots appearing when objects are detected
- An info panel showing current readings
Troubleshooting
Problem: "Error connecting to serial port"
Solution:
- Make sure Arduino is plugged in via USB
- Close Arduino IDE Serial Monitor if it's open
Try a different port number in the code:
javamyPort = new Serial(this, Serial.list()[1], 9600); // Try [1] instead of [0]
Problem: No data appearing on radar
Solution:
- Open Arduino Serial Monitor to verify data is being sent
- Check that baud rates match (9600 in both Arduino and Processing)
- Make sure Arduino radar code is actually running
Problem: "Port in use" error
Solution:
- Close Arduino Serial Monitor
- Close any other programs that might be using the serial port
- Unplug and replug the Arduino USB cable
- Try running the Processing sketch again
Problem: Console shows "Array out of bounds"
Solution:
- This means the port number is wrong
- Check the available ports list again
- Use a port number that actually exists (0, 1, 2, etc.)
Problem: Radar window opens but is black/blank
Solution:
- Check the console for error messages
- Make sure the Arduino is sending data in the correct format:
angle,distance - Try unplugging and replugging the Arduino
Expected Result
When everything works correctly, you should see:
- A dark green radar screen with grid lines
- A bright green sweep line rotating from 0° to 180° and back
- Red dots appearing where objects are detected
- Real-time data in the info panel showing current angle and distance
- Console output showing:
"Angle: 45°, Distance: 73cm"etc.
Keyboard Controls
Once running, you can use these keys:
- 'C' - Clear all detected objects from the display
- 'R' - Reconnect to the serial port
- Close the window - Stop the radar display
The radar will continuously scan and display objects in real-time, just like a real radar system!