Radar

Example

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:

  1. Servo Sweep: The servo motor rotates from 0° to 180° and back, carrying the ultrasonic sensor
  2. Distance Measurement: At each angle, the ultrasonic sensor measures the distance to any objects
  3. Data Transmission: The Arduino sends angle and distance data via serial communication
  4. 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:

  1. Upload the Arduino code to your Arduino Mega
  2. Connect components according to the wiring diagram
  3. Open Serial Monitor (9600 baud rate) to see raw data
  4. 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 * 2 value 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

  1. Upload the Arduino radar code I provided earlier to your Arduino Mega
  2. 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)

  1. Download Processing IDE from processing.org
  2. Create a new sketch and paste the Processing radar code
  3. 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)
  4. Modify the connection line if needed:

    java
    myPort = new Serial(this, Serial.list()[0], 9600);  // Change [0] to correct index
  5. Run the sketch and you should see the radar display!

Option B: Python with Pygame

  1. Install required libraries:

    bash
    pip install pygame pyserial
  2. Find your serial port:
    • Windows: Usually COM3, COM4, etc. (check Device Manager)
    • Linux: Usually /dev/ttyUSB0 or /dev/ttyACM0
    • Mac: Usually /dev/cu.usbmodem... or /dev/cu.usbserial...
  3. Update the serial port in the Python code:

    python
    radar = RadarDisplay(serial_port='YOUR_PORT_HERE', baud_rate=9600)
  4. 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

  1. Go to the Processing website:
  2. 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)
  3. Install Processing:
    • Windows: Run the .exe file and follow the installation wizard
    • Mac: Open the .dmg file and drag Processing to your Applications folder
    • Linux: Extract the .tar.gz file to your preferred location
  4. 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

  1. Make sure your Arduino is connected to your computer via USB
  2. 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,45 then 2,47 then 4,52 etc.
    • If you don't see data, re-upload the Arduino radar code

Step 3: Create the Processing Sketch

  1. Open Processing (if not already open)
  2. Clear the default code:
    • You'll see some sample code in the editor
    • Select all text (Ctrl+A on Windows/Linux, Cmd+A on Mac)
    • Delete it all
  3. Copy the radar code:
    • Go back to the Processing radar code I provided earlier
    • Select ALL the code (Ctrl+A or Cmd+A)
    • Copy it (Ctrl+C or Cmd+C)
  4. Paste into Processing:
    • Click in the Processing editor window
    • Paste the code (Ctrl+V or Cmd+V)
    • The editor should now be filled with the radar visualization code

Step 4: Find Your Serial Port (IMPORTANT!)

  1. Run the sketch to check available ports:
    • Click the Play button (triangle icon) in the Processing toolbar
    • OR press Ctrl+R (Windows/Linux) or Cmd+R (Mac)
  2. 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: COM7

      OR on Mac/Linux:

      0: /dev/cu.usbmodem14101
      1: /dev/cu.Bluetooth-Incoming-Port
      2: /dev/ttyACM0
  3. Identify your Arduino port:
    • Windows: Usually COM3, COM4, COM5, etc.
    • Mac: Usually starts with /dev/cu.usbmodem or /dev/cu.usbserial
    • Linux: Usually /dev/ttyACM0 or /dev/ttyUSB0
    • Note the number (0, 1, 2, etc.) next to your Arduino's port

Step 5: Configure the Serial Port

  1. Stop the current sketch (click the Stop button - square icon)
  2. Find the serial connection line in the code:
    • Look for this line (around line 40):

      java
      myPort = new Serial(this, Serial.list()[0], 9600);
  3. 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:

      java
      myPort = new Serial(this, Serial.list()[1], 9600);

Step 6: Run the Radar Display

  1. Save your sketch (Ctrl+S or Cmd+S)
    • Give it a name like "ArduinoRadar"
    • Choose a location to save it
  2. Run the sketch again:
    • Click the Play button or press Ctrl+R/Cmd+R
  3. 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:

  1. Make sure Arduino is plugged in via USB
  2. Close Arduino IDE Serial Monitor if it's open
  3. Try a different port number in the code:

    java
    myPort = new Serial(this, Serial.list()[1], 9600);  // Try [1] instead of [0]

Problem: No data appearing on radar

Solution:

  1. Open Arduino Serial Monitor to verify data is being sent
  2. Check that baud rates match (9600 in both Arduino and Processing)
  3. Make sure Arduino radar code is actually running

Problem: "Port in use" error

Solution:

  1. Close Arduino Serial Monitor
  2. Close any other programs that might be using the serial port
  3. Unplug and replug the Arduino USB cable
  4. Try running the Processing sketch again

Problem: Console shows "Array out of bounds"

Solution:

  1. This means the port number is wrong
  2. Check the available ports list again
  3. Use a port number that actually exists (0, 1, 2, etc.)

Problem: Radar window opens but is black/blank

Solution:

  1. Check the console for error messages
  2. Make sure the Arduino is sending data in the correct format: angle,distance
  3. 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!