Mirko Pavleski
Published © GPL3+

360° Arduino Radar with 2xHC-SR04 Sensors

In this case, it is interesting that are used two sensors to scan the entire 360-degree space.

BeginnerFull instructions provided15,465
360° Arduino Radar with 2xHC-SR04 Sensors

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Software apps and online services

Arduino IDE
Arduino IDE
Processing
The Processing Foundation Processing

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematic diagram

Code

Arduino code

C/C++
#include <HCSR04.h>
#include <Servo.h>

UltraSonicDistanceSensor distanceSensor(6, 7);             //Create the 1st sensor object
UltraSonicDistanceSensor distanceSensor2(5, 4);             //Create the 2nd sensor object
Servo servoMotor;            //Create the Servo object

int delayTime = 5;            //Delay value to wait for the servo to reach the 1 angle difference
long d;                 //Distance from 1st sensor calculated
long d2;                //Distance from 2nd sensor calculated

void setup() {
  Serial.begin(9600);           //Initialize the Serial communication at 9600 bauds

  servoMotor.attach(2);         //Attach the servo to the Digital PWM pin 2
  servoMotor.write(180);        //Rotate the servo to the 180?
  delay(1000);              //Wait for the servo to reach 180?
  servoMotor.write(0);          //Rotate the servo to the 0?
  delay(1000);              //Wait for the servo to reach 0?

}

void loop() {
  for (int i = 1; i < 180; i++) {   //Move the Servo 180 degrees forward
    readSensors();            //Read the sensors
    Serial.print(i);          //Print the angle
    Serial.print(",");          //Print a ","
    Serial.print(d);          //Print the 1st distance
    Serial.print(",");          //Print a ","
    Serial.println(d2);         //Print the 2nd distance with end line
    servoMotor.write(i);        //Set the sensor at the angle
    delay(delayTime);         //Wait for the servo to reach i?
  }
  for (int i = 180; i > 1; i--) {   //Move the Servo 180 degrees backward
    readSensors();            //Read the sensors
    Serial.print(i);          //Print the angle
    Serial.print(",");          //Print a ","
    Serial.print(d);          //Print the 1st distance
    Serial.print(",");          //Print a ","
    Serial.println(d2);         //Print the 2nd distance with end line
    servoMotor.write(i);        //Set the sensor at the angle
    delay(delayTime);         //Wait for the servo to reach i?
  }
}

void readSensors() {
  d = distanceSensor.measureDistanceCm();
  d2 = distanceSensor2.measureDistanceCm();
}

Processing Code

C/C++
import processing.serial.*;
import static javax.swing.JOptionPane.*;

Serial myPort;        // The serial port
String serialin;
int data[] = new int[360];
PFont f;

final boolean debug = true;

void setup() {
  String COMx, COMlist = "";
  size(1280, 720);
  f = createFont("Verdana", 32, true); // Arial, 16 point, anti-aliasing on
  textFont(f, 20);
  frameRate(60);
  for (int i = 0; i < 360; i++) {
    data[i] = 0;
  }
  try {
    if (debug) printArray(Serial.list());
    int i = Serial.list().length;
    if (i != 0) {
      if (i >= 2) {
        // need to check which port the inst uses -
        // for now we'll just let the user decide
        for (int j = 0; j < i; ) {
          COMlist += char(j+'a') + " = " + Serial.list()[j];
          if (++j < i) COMlist += ",  ";
        }
        COMx = showInputDialog("Which COM port is correct? (a,b,..):\n"+COMlist);
        if (COMx == null) exit();
        if (COMx.isEmpty()) exit();
        i = int(COMx.toLowerCase().charAt(0) - 'a') + 1;
      }
      String portName = Serial.list()[i-1];
      if (debug) println(portName);
      myPort = new Serial(this, portName, 9600); // change baud rate to your liking
      myPort.bufferUntil('\n'); // buffer until CR/LF appears, but not required..
    } else {
      showMessageDialog(frame, "Device is not connected to the PC");
      exit();
    }
  }
  catch (Exception e)
  { //Print the type of error
    showMessageDialog(frame, "COM port is not available (may\nbe in use by another program)");
    println("Error:", e);
    exit();
  }
}


void draw() {
  background(26, 26, 36, 200);
  textSize(18);
  stroke(255, 255, 255, 150);
  fill(255, 50, 200, 200);
  text("Arduino RADAR 2D Visualization", 20, 710);
  text(hour(), 1050, 710);
  text(":", 1075, 710);
  text(minute(), 1085, 710);
  text(":", 1110, 710);
  text(second(), 1120, 710);
  fill(36, 255, 100, 200);
  strokeWeight(3);
  circle(640, 360, 600);
  circle(640, 360, 500);
  circle(640, 360, 400);
  circle(640, 360, 300);
  circle(640, 360, 200);
  circle(640, 360, 100);

  for (int i = 0; i < 360; i++) {
    fill(255, 10, 255, 200);
    stroke(50, 10, 255, 150);
    point(float(640) +  (map_values(data[i]))*cos(radians(i)), float(360) + (map_values(data[i]))*sin(radians(i)));
  }

  while (myPort.available() > 0) {
    serialin = myPort.readStringUntil(10);
    try {
      String serialdata[] = splitTokens(serialin, ",");
      if (serialdata[0] != null) {
        serialdata[0] = trim(serialdata[0]);
        serialdata[1] = trim(serialdata[1]);
        serialdata[2] = trim(serialdata[2]);

        int i = int(serialdata[0]);
        data[179-i] = int(serialdata[1]);
        data[(179-i)+180] = int(serialdata[2]);
      }
    } 
    catch (java.lang.RuntimeException e) {
    }
  }
}

float map_values(float x) {
  float in_min = 0, in_max = 200, out_min = 0, out_max = 700;
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Credits

Mirko Pavleski

Mirko Pavleski

148 projects • 1276 followers

Comments