Ultrasound Radar – Getting it working

Selamat malam folks.
I found my ‘radar’ hardware system and it still worked. Here’s my schematic, Arduino sketch and Processing sketch. NOTE: In the Processing sketch on line 33, you need to manually specify the port that your computer has put the arduino on. Mine is currently on port 43 so that’s what the code will say.

I’m sure this will work for you.

Many many thank you’s to mechatronics for putting this quality project up on the internet in the first place. I had to edit his code for a few reasons. I’m sure he’s OK with that.

ultrasound radar MPA pinout jpg
FULL size image HERE
 

 Arduino code:

//
// Source: http://howtomechatronics.com/projects/arduino-radar/
//
// Includes the Servo library
#include <Servo.h>.
// Defines Tirg and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
// Variables for the duration and the distance
long duration;
int distance;

Servo myServo; // Creates a servo object for controlling the servo motor
int pos = 0;    // variable to store the servo position
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
myServo.attach(9); // Defines on which pin is the servo motor attached

}
void loop() {
// rotates the servo motor from 0 to 180 degrees
for(pos = 0; pos <= 180; pos += 1){
myServo.write(pos);
delay(15);
distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree

Serial.print(pos); // 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(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 180 to 0 degrees
for(pos = 180; pos>=0; pos-=1){
myServo.write(pos);
delay(15);
distance = calculateDistance();
Serial.print(pos);
Serial.print(“,”);
Serial.print(distance);
Serial.print(“.”);
}
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}

 

 

 

Processing Sketch: (you need to download the ‘processing’ program. It’s very much like the arduino programming environment, and you can tell they’re heavily related. Processing is GREAT for programming graphical things!) Get “Processing” HERE

//
//   source: http://howtomechatronics.com/projects/arduino-radar/
//  9th Nov 2015. Michael Allan:
//  I’ve modified it to fit better on a normal screen
//  The value of distance seems to work OK having measured it (+- a few cm)
//  but the red trace is inaccurate and needs code adjustment.
//  Also the angle thing updates too much – too blurry.
//  in fact if the blue effect for the distance and angle and our of range
//  in range can be switched off that would be much better.
//  MAybe update the angle in 5% changes?
//  Also the data aquisition shows very poor stability. The radar/sonar
//  slows down really badly at times – especially when there isn’t any
//  red (detected) part. That also needs improving.
//
import processing.serial.*; // imports library for serial communication
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;
Serial myPort; // defines Object Serial
// defubes variables
String angle=””;
String distance=””;
String data=””;
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {

size (800, 450);  // 800 pixels wide by 768 pixels high
//smooth();
myPort = new Serial(this,”COM43″, 9600); // starts the serial communication
myPort.bufferUntil(‘.’); // reads the data from the serial port up to the character ‘.’. So actually it reads this: angle,distance.
//orcFont = loadFont(“OCRAExtended-30.vlw”);
}
void draw() {

fill(98,245,31);    // numbers sets the colour that an object is to be filled witn.
//     textFont(orcFont);
// simulating motion blur and slow fade of the moving line
noStroke();
fill(0,4);
rect(0, 0, width, 800);      // ORI =       rect(0, 0, width, 800);

fill(98,245,31); // green color
// calls the functions for drawing the radar
drawRadar();
drawLine();
drawObject();
drawText();
}
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”
angle= 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
distance= data.substring(index1+1, data.length()); // read the data from position “index1” to the end of the data pr thats the value of the distance

// converts the String variables into Integer
iAngle = int(angle);
iDistance = int(distance);
}
void drawRadar() {
pushMatrix();
translate(380,380); // moves the starting coordinats to new location
noFill();
strokeWeight(2);
stroke(98,245,31);     //    Sets the color used to draw lines and borders around shapes.
// draws the arc lines
arc(0,0,600,600,PI,TWO_PI);  // ORI = arc(0,0,1800,1800,PI,TWO_PI);
arc(0,0,450,450,PI,TWO_PI);
arc(0,0,300,300,PI,TWO_PI);
arc(0,0,150,150,PI,TWO_PI);
// draws the angle lines
line(-320,0,320,0);
line(0,0,-320*cos(radians(30)),-320*sin(radians(30)));
line(0,0,-320*cos(radians(60)),-320*sin(radians(60)));
line(0,0,-320*cos(radians(90)),-320*sin(radians(90)));
line(0,0,-320*cos(radians(120)),-320*sin(radians(120)));
line(0,0,-320*cos(radians(150)),-320*sin(radians(150)));
line(-320*cos(radians(30)),0,320,0);
popMatrix();
}
void drawObject() {
pushMatrix();
translate(380,380); // moves the starting coordinats to new location
strokeWeight(9);
stroke(255,10,10); // red color
pixsDistance = iDistance*22.5; // covers the distance from the sensor from cm to pixels
// limiting the range to 40 cms
if(iDistance<40){
// draws the object according to the angle and the distance
line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),316*cos(radians(iAngle)),-316*sin(radians(iAngle)));
}
popMatrix();
}
void drawLine() {
pushMatrix();
strokeWeight(2);
stroke(30,250,60);
translate(380,380); // moves the starting coordinats to new location
line(0,0,310*cos(radians(iAngle)),-310*sin(radians(iAngle))); // draws the line according to the angle
popMatrix();
}
void drawText() { // draws the texts on the screen

pushMatrix();
if(iDistance>40) {
noObject = “Out of Range”;
}
else {
noObject = “In Range    “;
}
fill(0,0,0);
noStroke();
rect(0, 1010, width, 1080);
fill(98,245,31);
textSize(15);
text(“10cm”,445,400);
text(“20cm”,520,400);
text(“30cm”,600,400);
text(“40cm”,670,400);
textSize(16);
text(“Object: ” + noObject, 80, 430);
text(“Angle: ” + iAngle +” °”, 350, 430);
text(“Distance: “, 540, 430);
if(iDistance<40) {
text(”        ” + iDistance +” cm”, 590, 430);
}
textSize(25);
fill(98,245,60);
translate(375+324*cos(radians(30)),375-344*sin(radians(30)));
rotate(-radians(-60));
text(“30°”,0,0);
resetMatrix();
translate(370+325*cos(radians(60)),370-325*sin(radians(60)));
rotate(-radians(-30));
text(“60°”,0,0);
resetMatrix();
translate(364+330*cos(radians(90)),376-320*sin(radians(90)));
rotate(radians(0));
text(“90°”,0,0);
resetMatrix();
translate(323+252*cos(radians(120)),323-252*sin(radians(120)));
rotate(radians(-30));
text(“120°”,0,0);
resetMatrix();
translate(340+294*cos(radians(150)),340-210*sin(radians(150)));
rotate(radians(-60));
text(“150°”,0,0);
popMatrix();
}

 

 

Advertisements

One thought on “Ultrasound Radar – Getting it working

  1. I think I know why my radar system was ‘jittering’
    I think the “Baud” rate is too low.
    See the line that says
    Serial.begin(9600);
    I think it would be better if it says:
    Serial.begin(115200);
    but I don’t have my equipment right not to test it.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s