Mesure de distance en temps réel par sonar Arduino et visualisation Processing
Composants et fournitures
| Arduino UNO | ×1 |
| Servomoteur SG90 | ×1 |
| Capteur ultrasonique HC-SR04 (générique) | ×1 |
| Fils de connexion (génériques) | ×1 |
| Plaque d'essai sans soudure (taille complète) | ×1 |
Outils et machines nécessaires
- Pistolet à colle chaude (générique)
- Fer à souder, kit SolderPro 150
Applications et services en ligne
- Arduino IDE
- Processing (The Processing Foundation)
À propos du projet
Bonjour à tous ! On m'a suggéré de réaliser une voiture à évitement d'obstacles, mais je trouvais cela trop commun et peu pratique. J'ai donc eu l'idée de créer un sonar en reprenant le concept du servomoteur et du capteur HC-SR04 de la voiture à évitement d'obstacles.
N'ayant pas d'écran TFT LCD pour afficher la sortie du sonar, j'ai pensé à utiliser l'IDE Processing qui permet d'afficher les résultats sur mon PC avec une belle résolution ! Étant novice avec Processing, j'ai d'abord pratiqué avec l'éditeur web p5, puis j'ai utilisé Processing IDE et certaines bibliothèques pour lire la sortie du sonar et l'afficher sur l'ordinateur.
J'ai fixé le capteur ultrasonique sur le servomoteur à l'aide d'un pistolet à colle chaude, de sorte qu'il tourne avec le servo et couvre une plage de 180 degrés. La détection d'objets et la distance sont affichées dans Processing IDE, avec un code supplémentaire qui affiche précisément la distance et l'angle !
Vous pouvez utiliser les images de référence ci-dessus pour comparer avec votre propre projet.
Regardez la vidéo jusqu'à la fin pour bien comprendre le fonctionnement de la sortie.
Code
Code sonar pour Arduino IDE (C/C++)
#include <Servo.h>
const int trigPin = 12;
const int echoPin = 11;
long duration;
int distance;
Servo s1;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
s1.attach(9);
}
void loop() {
for(int i = 0; i < 180; i++) {
s1.write(i);
delay(30);
distance = calDist();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
for(int i = 180; i > 0; i--) {
s1.write(i);
delay(30);
distance = calDist();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
}
int calDist() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
Code sonar pour Processing IDE (Java)
import processing.serial.*;
Serial myPort;
String ang = "";
String distance = "";
String data = "";
int angle, dist;
void setup() {
size(2000, 800);
myPort = new Serial(this, "COM3", 9600);
myPort.bufferUntil('.');
background(0);
}
void draw() {
fill(0, 5);
noStroke();
rect(0, 0, width, height * 0.93);
noStroke();
fill(0, 255);
rect(0, height * 0.93, width, height);
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent(Serial myPort) {
data = myPort.readStringUntil('.');
data = data.substring(0, data.length() - 1);
int index1 = data.indexOf(",");
ang = data.substring(0, index1);
distance = data.substring(index1 + 1, data.length());
angle = int(ang);
dist = int(distance);
System.out.println(angle);
}
void drawRadar() {
pushMatrix();
noFill();
strokeWeight(0.5);
stroke(10, 255, 10);
translate(width/2, height - height * 0.06);
line(-width/2, 0, width/2, 0);
arc(0, 0, width * 0.5, width * 0.5, PI, TWO_PI);
arc(0, 0, width * 0.25, width * 0.25, PI, TWO_PI);
arc(0, 0, width * 0.75, width * 0.75, PI, TWO_PI);
arc(0, 0, width * 0.95, width * 0.95, PI, TWO_PI);
// lignes de grille toutes les 30°
for (int a = 30; a <= 150; a += 30) {
line(0, 0, (-width/2) * cos(radians(a)), (-width/2) * sin(radians(a)));
}
stroke(175, 255, 175);
// lignes intermédiaires toutes les 15°
for (int a = 15; a <= 165; a += 30) {
line(0, 0, (-width/2) * cos(radians(a)), (-width/2) * sin(radians(a)));
}
popMatrix();
}
void drawLine() {
pushMatrix();
strokeWeight(9);
stroke(0, 255, 0);
translate(width/2, height - height * 0.06);
line(0, 0, (width/2) * cos(radians(angle)), (-width/2) * sin(radians(angle)));
popMatrix();
}
void drawObject() {
pushMatrix();
strokeWeight(9);
stroke(255, 0, 0);
translate(width/2, height - height * 0.06);
float pixelDist = (dist / 40.0) * (width / 2.0);
float pd = (width/2) - pixelDist;
float x = -pixelDist * cos(radians(angle));
float y = -pixelDist * sin(radians(angle));
if (dist <= 40) {
line(-x, y, -x + (pd * cos(radians(angle))), y - (pd * sin(radians(angle))));
}
popMatrix();
}
void drawText() {
pushMatrix();
fill(100, 200, 255);
textSize(25);
text("10cm", (width/2) + (width * 0.115), height * 0.93);
text("20cm", (width/2) + (width * 0.24), height * 0.93);
text("30cm", (width/2) + (width * 0.365), height * 0.93);
text("40cm", (width/2) + (width * 0.45), height * 0.93);
if (dist <= 40) {
text("Distance :" + dist, width * 0.7, height * 0.99);
}
translate(width/2, height - height * 0.06);
textSize(25);
text("30", (width/2) * cos(radians(30)), (-width/2) * sin(radians(30)));
text("60", (width/2) * cos(radians(60)), (-width/2) * sin(radians(60)));
text("90", (width/2) * cos(radians(90)), (-width/2) * sin(radians(90)));
text("120", (width/2) * cos(radians(120)), (-width/2) * sin(radians(120)));
text("150", (width/2) * cos(radians(150)), (-width/2) * sin(radians(150)));
popMatrix();
}
Schéma de câblage
Le schéma de connexion est disponible dans les fichiers du projet. Le capteur HC-SR04 se connecte aux broches 11 (echo) et 12 (trig), le servomoteur sur la broche 9.
Processus de fabrication
- Détecteur de langue TinyML avec Edge Impulse sur Arduino Nano 33 BLE Sense
- Construire un jeu gyroscopique temps réel avec Arduino Nano et capteur MPU-6050
- Dé numérique Arduino sur écran LCD 16x2 – Générateur aléatoire à bouton poussoir
- Construire un dispositif de lévitation ultrasonique avec Arduino – Guide étape par étape
- Construire un Voltmètre Intelligent avec Arduino et Smartphone – Projet DIY Facile
- Enregistreur de Température et Humidité Arduino avec DHT11 et Shield Ethernet
- Bocal Intelligent IoT avec ESP8266, Arduino et Capteur Ultrasonique – Surveillance Temps Réel
- Compteur de stationnement intelligent : Suivi véhicule en temps réel avec Arduino, Processing et PHP
- Construire une Boussole Numérique Temps Réel avec Arduino et Processing