Hi! I am very happy with my version of Arduino Radar with Mathematica. I used the HC-SR04 Ultrasonic Distance Sensor and a small Servo Motor “Miuzei Micro Servo 9g MS18” with Arduino Uno Rev. 3. A short clip for the radar working:
Here are close photos to the radar and circuit diagram:
Arduino Code
Arduino Code
Below, the Arduino Code:
#define echoPin 11 // Echo Pin
#define trigPin 12 // Trigger Pin
#include <Servo.h>
Servo myservo;
int pos = 0; // Position du servo-moteur.
long duration; // Durée d'un aller-retour en micro-secondes.
int distance; // Distance sonde-objet en cm.
int minimum = 100;// Distance minimale de détection en cm.
void setup() {
Serial.begin (115200);
myservo.attach(9);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
echoAffiche();
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
echoAffiche();
}
}
void echoAffiche() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
/*
Calcul de la distance en cm.
La vitesse du son est de 0,0343 cm par micro-secondes.
*/
distance = (duration*.0343)/2;
Serial.write(pos);
Serial.write(distance);
delay(50);
}
#define trigPin 12 // Trigger Pin
#include <Servo.h>
Servo myservo;
int pos = 0; // Position du servo-moteur.
long duration; // Durée d'un aller-retour en micro-secondes.
int distance; // Distance sonde-objet en cm.
int minimum = 100;// Distance minimale de détection en cm.
void setup() {
Serial.begin (115200);
myservo.attach(9);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
echoAffiche();
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
echoAffiche();
}
}
void echoAffiche() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
/*
Calcul de la distance en cm.
La vitesse du son est de 0,0343 cm par micro-secondes.
*/
distance = (duration*.0343)/2;
Serial.write(pos);
Serial.write(distance);
delay(50);
}
This is a screen shot of the Radar:
Wolfram Language code
Wolfram Language code
You must Evaluate Initialization Cells and Resize the image of the Radar.
If your Arduino and Mathematica do not connect, try Evaluate Initialization Cells again!
The Com port number may be a problem, if no connection, change COM5 to COM3.
This is a nice project to do, have fun!
If your Arduino and Mathematica do not connect, try Evaluate Initialization Cells again!
The Com port number may be a problem, if no connection, change COM5 to COM3.
This is a nice project to do, have fun!
serial=DeviceOpen["Serial",{"COM5","BaudRate"115200}];
ReadDataValue[]:=DeviceReadBuffer[serial]
RunScheduledTask[Data=ReadDataValue[],0.15];
angle:=Data[[1]];distance:=Data[[2]];distanceMax:=80;f[x_]:=-;
2
distanceMax
2
x
rayons=GraphicsDarker[Green],Thickness[0.002],Line{0,0},CosdistanceMax,SindistanceMax,{0,0},CosdistanceMax,SindistanceMax,{0,0},CosdistanceMax,SindistanceMax,{0,0},CosdistanceMax,SindistanceMax;
π
6
π
6
5π
6
5π
6
π
3
π
3
2π
3
2π
3
limite=Plotf[x],,,3,{x,-distanceMax,distanceMax},PlotRange{{-distanceMax,distanceMax},{-5,distanceMax}},AspectRatioAutomatic,FillingBottom,FillingStyleBlack,AxesStyleWhite,TicksStyleThick,ColorFunction{Green,Green,Green,Green},LabelStyleDirective[White,Large,Bold],BackgroundBlack,Epilog{Style[Text["Angle(°):",{-distanceMax+12,5}],FontColorWhite,Bold,24],Style[Text[Dynamic[angle],{-distanceMax+25,5}],FontColorWhite,Bold,24],Style[Text["Distance (cm):",{distanceMax-28,5}],FontColorWhite,Bold,24],Style[Text[Dynamic[distance],{distanceMax-10,5}],FontColorWhite,Bold,24]};
f[x]
4
f[x]
2
f[x]
4
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Ultrasonic Radar with Arduino and Wolfram Language
by Marcel Pelletier
Wolfram Community, STAFF PICKS, February 10, 2024
https://community.wolfram.com/groups/-/m/t/3119087
by Marcel Pelletier
Wolfram Community, STAFF PICKS, February 10, 2024
https://community.wolfram.com/groups/-/m/t/3119087