Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Magicbit
Published

Simple Radar System From Magicbit (ESP32)

This tutorial shows how to make a simple radar system using HC-SR04 sensor and Microbit dev board with processing and Arduino IDE's.

BeginnerProtip2,380
Simple Radar System From Magicbit (ESP32)

Things used in this project

Hardware components

SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Magicbit
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Processing
The Processing Foundation Processing

Story

Read more

Schematics

circuit

Code

Arduino code

Arduino
#include <NewPing.h>
#define TRIGGER_PIN  21
#define ECHO_PIN     22
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#include <ESP32Servo.h>//include servo library
int distance;
Servo RadarServo; 
void setup() {
 
  Serial.begin(115200);
  RadarServo.attach(26); // Defines on which pin is the servo motor attached
  delay(3000);
}
void loop() {
  // rotates the servo motor from 15 to 165 degrees
  for(int i=0;i<=180;i++){  
  RadarServo.write(i);
    delay(50);
  distance = sonar.ping_cm();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree

 for(int j=0;j<100;j+=25){
if(i==180 && j>0){
  break;
}
  Serial.print(i); // Sends the current degree into the Serial Port
  Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(j); // Sends the current degree into the Serial Port
  Serial.print("*");
  Serial.print(1); // Sends the distance value into the Serial Port
  Serial.print("/"); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(distance); // Sends the distance value into the Serial Port
  Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  }
  
  }


  
  // Repeats the previous lines from 165 to 15 degrees
  for(int i=180;i>=0;i--){  
  RadarServo.write(i);
  delay(50);
  distance = sonar.ping_cm();
  for(int j=75;j>=0;j-=25){
if(i==180 &&  (j==75 ||j==50 ||j==25)){
  continue;
}
 Serial.print(i); // Sends the current degree into the Serial Port
  Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(j); // Sends the current degree into the Serial Port
  Serial.print("*");
  Serial.print(-1); // Sends the distance value into the Serial Port
  Serial.print("/"); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(distance); // Sends the distance value into the Serial Port
  Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  }
  }
}

processing code

Java
import processing.serial.*; // imports library for serial communication
PShape s;
int[] vertex={0,25,25,45,45,60,120,136,136,155,155};
int[] lengths={30,27,23,23,27,30,30,27,23,23,27};
Serial myPort; // defines Object Serial
int[] Angles = new int[724];
int[] Distances = new int[724];
int[][] arcCordinats=new int[724][724];
int[][] disCordinats=new int[724][724];
int[] Center={960, 950};
float Angle=0;
float IAngle=0;
float DAngle=0;
int Istart=0;
int Dstart=0;
float increment;
float Iincrement;
float Dincrement;
int[] Color=new int[201];
int[] reverseColor=new int[41];
String angleD="";
String angleF="";
String distance="";
String data="";
int S=0;
float preAngle=0;
int index1=0;
int index2=0;
int index3=0;
int D;
String d;
void setup() {
   s = createShape();
  s.beginShape();

  s.fill(255,150,0);
 // stroke(255,150,0);
  for(int i=0;i<=180;i+=180 ){
  for(int j=0;j<11;j++){
  s.vertex(lengths[j]*cos((vertex[j]+i)*PI/180),lengths[j]*sin((vertex[j]+i)*PI/180));

  }
  }
  s.endShape(CLOSE);
 // size(1980, 1080, P2D);
 fullScreen();
  for (int i=0; i<724; i++) {
    arcCordinats[i][0]=int(900*cos(i*PI/180.0/4))+Center[0];
    arcCordinats[i][1]=-int(900*sin(i*PI/180.0/4))+Center[1];
    disCordinats[i][0]=int(900*cos(i*PI/180.0/4))+Center[0];
    disCordinats[i][1]=-int(900*sin(i*PI/180.0/4))+Center[1];
  }
   //smooth();
   
  myPort = new Serial(this,"COM8", 115200); // starts the serial communication
  myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
delay(3000);
}
void draw() {
  
  background(0,200);


  

lines(IAngle, DAngle);

  Interface();
   logo(50,990); 
   
  
}

void lines(float IAngle, float DAngle) {

  if (Istart>0) {
    Inc(IAngle);
    Dec(DAngle);
  } else {
    Dec(DAngle);
    Inc(IAngle);
  }
}

void Dec(float DAngle) {
  if (increment==-1 || Dstart>0) {
    for (int i=Dstart; i<=200; i++) {
      strokeWeight(4);
      stroke(0, 200-i, 0);
      line(Center[0], Center[1], disCordinats[int(DAngle*4)+i][0], disCordinats[int(DAngle*4)+i][1]);
      
      //if((disCordinats[int(DAngle*4)+i][0]!=arcCordinats[int(DAngle*4)+i][0]) && (disCordinats[int(DAngle*4)+i][1]!=arcCordinats[int(DAngle*4)+i][1])){
        strokeWeight(4);
       stroke(200-i, 0, 0);
      line(disCordinats[int(DAngle*4)+i][0], disCordinats[int(DAngle*4)+i][1], arcCordinats[int(DAngle*4)+i][0], arcCordinats[int(DAngle*4)+i][1]);
      
     // }
      if (DAngle*4+i>=720) {
        break;
      }
    }
  }
}
void Inc(float IAngle) {

  if (increment==1 || Istart>0) {
    for (int i=Istart; i<=200; i++) {
      strokeWeight(4);
      stroke(0, 200-i, 0);
      line(Center[0], Center[1], disCordinats[int(IAngle*4)-i][0], disCordinats[int(IAngle*4)-i][1]);
      
      
    //  if((disCordinats[int(IAngle*4)-i][0]!=arcCordinats[int(IAngle*4)-i][0]) && (disCordinats[int(IAngle*4)-i][1]!=arcCordinats[int(IAngle*4)-i][1])){
         strokeWeight(4);
     stroke(200-i, 0, 0);
     line(disCordinats[int(IAngle*4)-i][0], disCordinats[int(IAngle*4)-i][1], arcCordinats[int(IAngle*4)-i][0], arcCordinats[int(IAngle*4)-i][1]);
     
   //  }
      if (IAngle*4-i<=0) {
        break;
      }
    }
  }
}



void serialEvent (Serial myPort) { // starts reading data from the Serial Port
  // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  data = myPort.readStringUntil('.');
  data = data.substring(0, data.length()-1);

  index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
  angleD= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
  index2 = data.indexOf("*"); // find the character ',' and puts it into the variable "index1"
  angleF= data.substring(index1+1, index2); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
  index3 = data.indexOf("/"); // find the character ',' and puts it into the variable "index1"
  distance= data.substring(index2+1, index3); 
  d= data.substring(index3+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance
  
  
Angle = int(angleD)+int(angleF)/100.0;
 // Angle=int(angleF);
  increment = int(distance);
D=int(d);
if (Angle>=0 && Angle<=180 && increment==1) {
    IAngle=Angle;
  //  Dincrement=0;
   // Iincrement=0;
    
  } else if (Angle>=0 && Angle<=180 && increment==-1) {
    DAngle=Angle;
   // Dincrement=0;
    //Iincrement=0;
   
  } 
 if (Angle==180.0 && increment==-1) {
    
    Iincrement=0.25;

  } else if (Angle==0.0 && increment==1) {
    Dincrement=-0.25;
  
  }
  
 
if(Iincrement==0.25){
  Istart++;
}
else if(Dincrement==-0.25){
  Dstart++;
}
 
if(Istart>=200){
    Istart=0;
     IAngle=0;
   Iincrement=0;}
if(Dstart>=200){

  Dstart=0;
  DAngle=0;
  Dincrement=0;
   
}
 IAngle=IAngle+Iincrement;
  DAngle=DAngle+Dincrement;
 // println(increment+","+Angle+","+Istart+","+IAngle);
 if(D==0 || D >50){
  disCordinats[int(Angle*4)][0]=arcCordinats[int(Angle*4)][0];
    disCordinats[int(Angle*4)][1]=arcCordinats[int(Angle*4)][1];
}
else{
      disCordinats[int(Angle*4)][0]=int(D*18*cos(int(Angle*4)*PI/180.0/4))+Center[0];
    disCordinats[int(Angle*4)][1]=-int(D*18*sin(int(Angle*4)*PI/180.0/4))+Center[1];
  
}
}




void Interface(){
  strokeWeight(100);
  for(int i=0;i<2000;i+=100){
    
 stroke(0,(2000-i)*0.05, 0,50);
// fill(0,(2000-i)*0.03, 0,70);
 arc(Center[0], Center[1],i,i,PI,2*PI,OPEN);
 }
  
  strokeWeight(4);
      stroke(0, 220, 0);
     line(0, Center[1], 1980,Center[1]);
       strokeWeight(2);
        for(int i=30; i<180;i+=30){
        line(Center[0], Center[1], Center[0]+900*cos(i*PI/180),Center[1]-900*sin(i*PI/180));}
        fill(0, 255, 0);
        text(30, Center[0]+910*cos(30*PI/180),Center[1]-900*sin(30*PI/180));
        text(60, Center[0]+905*cos(60*PI/180),Center[1]-915*sin(60*PI/180));
        text(90, Center[0]+900*cos(90*PI/180)-20,Center[1]-910*sin(90*PI/180));
        text(120, Center[0]+910*cos(120*PI/180)-50,Center[1]-920*sin(120*PI/180));
        text(150, Center[0]+900*cos(150*PI/180)-80,Center[1]-920*sin(150*PI/180));
        //line(Center[0], Center[1], 0,Center[1]-990*tan(PI/6));
        //line(Center[0], Center[1], Center[0]+900/tan(PI/3),0);
        //line(Center[0], Center[1], Center[0]-990/tan(PI/3),0);
        //line(Center[0], Center[1], 990,0);
        noFill();
          strokeWeight(8);
          strokeCap(SQUARE);
        arc(Center[0], Center[1],1800,1800,PI,2*PI,OPEN);
        strokeWeight(6);
        arc(Center[0], Center[1],1200,1200,PI,2*PI,OPEN);
        strokeWeight(4);
        arc(Center[0], Center[1],600,600,PI,2*PI,OPEN);
       rect(0,Center[1],1920,80);
       line(800,Center[1],800,Center[1]+80);
       line(1400,Center[1],1400,Center[1]+80);
       
       
textSize(40);
fill(0, 255, 0);
text("Angle:"+int(angleD), 810, 1000); 
if(D>50 || D==0){
  text("Angle: No object", 1410, 1000); 
}else{
text("Distance:"+D, 1410, 1000);}
noFill();

}


void logo(int pointX,int pointY){
 shape(s, pointX, pointY);
 stroke(0);
  fill(0);
 rect(pointX-6.5,pointY-10,15,12);
 rect(pointX-6.5,pointY+21,15,5);
circle(pointX-14,pointY,3);
circle(pointX+16,pointY,3);
circle(pointX+2,pointY-18,8);
fill(255);
noStroke();
 rect(pointX-12,pointY-20,5,5);
 textSize(40);
 fill(255);
text("Magicbit Radar", 100, 1000); 
noFill();

}

Credits

Magicbit

Magicbit

57 projects • 34 followers
Magicbit is an integrated development platform based on ESP32 for learning, prototyping, coding, electronics, robotics, IoT and more.

Comments