How to make a simple radar system with Arduino UNO R4 WIFI board

How to make a simple radar system with Arduino UNO R4 WIFI board

Hello and welcome back. In this project, we will learn how to make a simple radar system with Arduino UNO R4 WIFI board. This is just the basic structure, you can expand and modify it as you like. I used a waterproof ultrasonic sensor to measure distance, which is ideal for getting the best accuracy reading. However, you can also use the HC-SR04 ultrasonic sensor if you prefer. Also, I used the SG90 servo motor for rotate the sensor left and right. Then, we can scan a wider area and detect objects in multiple directions. You can also replace this motor if you want to get smooth movements. For simulating and visualizing the radar system, I used the Processing IDE. This Java-based simulation IDE allows us to create graphical interfaces. We can represent the real-time radar data using this IDE. If you’d like to learn more about this IDE, please follow this link.

Additionally, I integrated the Arduino Cloud to monitor the radar data remotely. Therefore, we can monitor these data from anywhere in the world. If you’re a beginner at these topics, I recommend checking out our basic tutorials and projects to understand these components and IDEs better. However, finally, you can make a successful radar system at the end of this guide.

  • What is the Arduino UNO R4 WIFi board and how to use it step by step – Click on me
  • What is the Arduino cloud – Click on me

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, prepare the reader system structure parts. For this, use the rigiform or form board, but you can use any other suitable material. Please use the following sizes for this.

Step 3

Thirdly, create the curved shape using a 4.5 x 10 piece. Then, prepare the other piece for mounting the curved shape. Use the following images as a reference.

Step 4

Next, drill a hole in the curved shape piece to install the ultrasonic sensor. Afterward, install this piece onto the other curved shape piece that we created earlier.

Step 5

Now, install the Arduino board and servo motor onto the base piece. For this, I used pieces of double-sided tape.

Step 6

Afterward, calibrate your servo motor using a PWM servo motor driver and set the servo to 90 degrees. Then, install the ultrasonic sensor into the hole of the curved shape piece.

Step 7

Next, install this complete assembly onto the servo motor horn. Then, connect the ultrasonic sensor to the sensor module.

Step 8

Now, connect the ultrasonic sensor module and the servo motor to the Arduino UNO R4 Wi-Fi board, using the circuit diagram below.

How to make a simple radar system with Arduino UNO R4 WIFI board

Step 9

Next, connect the Arduino board to the computer. Afterward, set up the Arduino Cloud by following the steps below.

How to make a simple radar system with Arduino UNO R4 WIFI board
  • First, go to the Arduino Cloud website and log in to your account, or sign up if you don’t have one. Then, click the ‘Get Started’ button.
  • Next, click the ‘Add Device’ button and select your Arduino UNO R4 Wi-Fi board. If it’s your first time, you’ll need to install the Arduino Agent program.
  • Now, click the ‘Things’ tab and create a new thing. With the free plan, you can create up to 2 things. Then, add two variables. Please use the following details to create the variables.
  • Name – range / variable type — int / Variable permission — Read only
  • Name – status / variable type — Boolean / Variable permission — Read only
  • Afterward, associate your device with the Arduino Cloud and enter your network details.
  • Next, click the ‘Sketch’ button and remove the automatically generated code, then copy and paste the following program. Finally, click the ‘Upload’ button.
  • Code and circuit diagram — Download
#include <Servo.h>//include library
Servo myServo;//create object your own name
int dis;
#include "thingProperties.h"

void setup() {
  pinMode(3, OUTPUT);//define arduino pin
  pinMode(2, INPUT);//define arduino pin
  myServo.attach(9);//servo connect pin
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  // Your code here 
  for (int x = 0; x <= 180; x++) { //servo turn left
    myServo.write(x);//rotete servo
    range=distance();  
    Serial.print(x);//print servo angle
    Serial.print(",");
    Serial.print(range);//print ultrasonic readings
    Serial.print(".");
    if(range < 30){
      status = true;      
      }else{
        status = false;
    }
    ArduinoCloud.update();
    delay(20);
  }
  for (int y = 179; y > 0; y--) {//servo turn right
    myServo.write(y);//rotete servo
    range=distance(); 
    Serial.print(y);////print servo angle
    Serial.print(",");
    Serial.print(range);//print ultrasonic readings
    Serial.print(".");
    if(range < 30){
      status = true;      
      }else{
        status = false;
    }
    ArduinoCloud.update();
    delay(20);
  }

range = distance();
Serial.println(range);
delay(10);

  
}


//ultrasonic sensor code
int distance() {
  digitalWrite(3, LOW);
  delayMicroseconds(4);
  digitalWrite(3, HIGH);
  delayMicroseconds(10);
  digitalWrite(3, LOW);


  int t = pulseIn(2, HIGH);
  int cm = t / 29 / 2; //time convert distance
  return cm;//return value
}
  • Now, click the ‘Dashboard’ tab and create a dashboard for this radar system. For this, I used one gauge widget and one status widget. Also, link the ‘int’ variable for the gauge widget and the ‘boolean’ variable for the status widget.
  • Finally, arrange these widgets as you like. You can customize both the desktop and mobile dashboards in this step.

Step 10

Now, download and install the processing IDE. Then, copy and paste the following code into this IDE.

import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

Serial myport;  // create serial object
PFont f;
int Angle, Distance;
String angle = "";
String distance = "";
String data;
int index1 = 0;
int index2 = 0;
float pixsDistance;
float sweepAngle = 0; // Angle for radar sweep effect
float sweepSpeed = 0.5; // Speed of the sweep

// Additional variables for sensor data
String status = "Idle";  // Status of radar (Idle, Scanning)
String detectionStatus = "No Object"; // "Object Detected" or "No Object"
String dateTime = "";  // To hold current date/time
int targetCount = 0;  // Count of detected objects

void setup() {
  size(1300, 700);  // screen size
  smooth();
  f = createFont("David Bold", 20);  // font name and size
  textFont(f);

  String portName = "*******";  // set COM port
  myport = new Serial(this, portName, 9600);
  myport.bufferUntil('.');  // Expect data to end with a period (.)
}

void draw() {
  noStroke();
  fill(0, 10);  // Light background fill
  rect(0, 0, width, height);  // Clear the screen each frame
  fill(98, 245, 31);
  
  // Draw radar area
  greenmesh();
  radararea();
  words();
  
  // Draw the green line and red line
  greenLine();
  redline();
  
  // Add a sweeping effect to simulate radar scanning
  sweepEffect();
  
  // Display additional info
  displayAdditionalInfo();
}

// Get Arduino board serial values
void serialEvent(Serial myport) {
  data = myport.readStringUntil('.');
  if (data != null) {
    data = data.substring(0, data.length() - 1);  // Remove the period
    index1 = data.indexOf(",");
    angle = data.substring(0, index1);
    distance = data.substring(index1 + 1, data.length());

    // Converts the String variables into Integer
    Angle = int(angle);
    Distance = int(distance);
    
    // Update status
    status = "Scanning";
    
    // Update detection status based on distance
    if (Distance < 50) {
      detectionStatus = "Object Detected";
      targetCount++;
    } else {
      detectionStatus = "No Object";
    }
    
    // Update the current date and time
    dateTime = year() + "/" + nf(month(), 2) + "/" + nf(day(), 2) + " " + nf(hour(), 2) + ":" + nf(minute(), 2) + ":" + nf(second(), 2);
  }
}

// Half circle and lines (arc, angles)
void radararea() {
  pushMatrix();
  translate(625, 680);  // Move to center of the screen
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);

  // Draw arcs (distance rings)
  arc(0, 0, 1150, 1150, PI, TWO_PI);
  arc(0, 0, 850, 850, PI, TWO_PI);
  arc(0, 0, 550, 550, PI, TWO_PI);
  arc(0, 0, 250, 250, PI, TWO_PI);

  // Draw angle lines (sector lines)
  line(-450, 0, 450, 0);  // 180°
  for (int i = 30; i <= 150; i += 30) {
    line(0, 0, -600 * cos(radians(i)), -600 * sin(radians(i)));  // Draw each angle line
  }
  popMatrix();
}

// Green grid mesh
void greenmesh() {
  stroke(98, 245, 31);
  strokeWeight(0.1);
  for (int x = 0; x <= 700; x += 5) {
    line(0, x, width, x);
  }
  for (int y = 0; y <= 1300; y += 5) {
    line(y, 0, y, height);
  }
}

// Display text on screen
void words() {
  fill(98, 245, 31);
  text("180'", 10, 670);
  fill(98, 245, 31);
  text("0'", 1210, 670);
  fill(98, 245, 31);
  text("30'", 1160, 380);
  fill(98, 245, 31);
  text("60'", 940, 160);
  fill(98, 245, 31);
  text("90'", 615, 70);
  fill(98, 245, 31);
  text("120'", 310, 150);
  fill(98, 245, 31);
  text("150'", 80, 370);
  fill(255);
  text("SriTu Hobby Radar system", 20, 30);
  fill(255);
  text("Angle -- " + Angle + " '", 20, 60);
  fill(255);
  text("Distance -- " + Distance + " cm", 20, 90);
}

// Green line (angle representation)
void greenLine() {
  pushMatrix();
  strokeWeight(7);
  stroke(30, 250, 60);  // green color
  translate(625, 680);
  line(0, 0, 600 * cos(radians(Angle)), -600 * sin(radians(Angle)));
  popMatrix();
}

// Red line (distance representation)
void redline() {
  pushMatrix();
  translate(625, 680);
  strokeWeight(7);

  // Dynamically adjust the color of the red line based on distance
  if (Distance < 20) {
    stroke(0, 255, 0);  // Green if within 20cm
  } else if (Distance < 50) {
    stroke(255, 10, 10);  // Yellow if within 50cm
  } else {
    stroke(255, 255, 0);  // Red for longer distances
  }

  pixsDistance = Distance * 22.5;  // Scale the distance
  
  // Draw the red line based on the scaled distance
  line(pixsDistance * cos(radians(Angle)), -pixsDistance * sin(radians(Angle)), 
       600 * cos(radians(Angle)), -600 * sin(radians(Angle)));
  popMatrix();
}

// Radar sweep effect
void sweepEffect() {
  pushMatrix();
  translate(625, 680);
  strokeWeight(2);
  stroke(255, 255, 255, 150);  // White color for the sweep effect
  
  // Calculate the current position of the sweeping line
  sweepAngle += sweepSpeed;
  if (sweepAngle > 180) {
    sweepAngle = 0;  // Reset the sweep angle after one full sweep
  }

  // Draw the sweeping line
  line(0, 0, 600 * cos(radians(sweepAngle)), -600 * sin(radians(sweepAngle)));
  popMatrix();
}

// Display additional data
void displayAdditionalInfo() {
  fill(255);
  text("Status: " + status, 950, 40);
  text("Detection: " + detectionStatus, 950, 70);
  text("Date/Time: " + dateTime, 950, 100);
  text("Target Count: " + targetCount, 950, 130);
}
  • Then, enter the COM port of your connected Arduino board.
 String portName = "***********";  // set COM port
  • Finally, click the ‘Run’ button. In this step, you need to connect the Arduino board to a power source. You can use USB power for this.

Step 11

“Now, download the Arduino Cloud app from the Play Store or App Store. Then, log into your account. You should now see the dashboard you created on the main dashboard. Next, test your radar system using the Arduino Cloud and Processing interface. The full video guide is below. We hope to see you in the next tutorial or project. Have a great day!

How to make a simple radar system with Arduino UNO R4 WIFI board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *