Either the surfaces are insufficiently sonoreflective, or the sensor is malfunctioning, miswired, or underpowered.
What did I write int Post #16?
Read post #16.
Might I suggest disconnecting everything from the Arduino, except D11, D12, GND, and 5V going to the HC SR04 (bypassing the breadboard, if possible), and then using some hard, flat surface as a target to test your pulseIn()
return values — ideally, using a simplified sketch that only reads the sensor (and outputs to the Serial Monitor), doing nothing else?
Thing is i have my project dissertation next Friday, and next week is an exams week lol. I m afraid if i mess up too much with the circuit i might have a worse result..
One thing i noticed though! is when the robot is only connected to the LAPTOP (no connection to the Lithium battery), the motors don't turn the wheels. but the captor seems to work a lot better. it seems almost perfect, it detects 95% the times i put my hand on it..
The circuit is already a total mess and will never be reliable.
Loose wiring is certainly one of the problems.
Seems like you are powering both the Arduino and the motors from the same 6-V battery pack, is that correct? And the 6-V battery pack is connected directly to the Arduino 5-V pin, correct? These two mistakes could explain your problems.
Indeed i am using 3.7V Li-Ion X2. But they are connected to the breadboard +/-. Arduino's 5V is also connected to the +- of Breadboard.
Really?
Microcontrollers are meant to communicate over short distances on a circuit board. You have about five meters of wire. This will hinder "good" connections.
Yes i get it but what am i supposed to get rid off? just the 2 drivers have 14 connections each..
The Arduino 5V pin should generally not be used to connect an external power supply, as this can damage the board; use the VIN pin or the barrel jack connector instead. Also, the Arduino power supply should not also power the motors; you need two independent power supplies. The grounds should be interconnected, but not the voltages.
Number of wires is not the issue (it must communicate), but length of the conductors. Changing all for shorter wires will give you the chance to verify they are all connected to the right place, rather than glancing at it and concluding, "looks good" (when it is not).
See Post #25.
also sir, the accolade is locked in the code
if (distance < THRESHOLD_BUZZER) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
Solid advice, i wish i did that to be honest..
One key thing i just noticed today, when deleting the VERY LAST delay(50); in the loop, and connecting the CAR only to my laptop (without the batteries) the captor works almost fine!
The from the 3v7×2 should only be connected to the servo
pin and to the VCC2 pins on the two L293 D drivers, and nothing else. The Arduino should be powered via a separate source (7–12V), connected to the barrel jack or to VIN. I think that will fix your issues.
Why are you continuing with random guesswork? See Post #25, #15, #7... and I have already used and proven the "old code" from Post #1 worked (removing delay()
and servo()
statements) in Post #12. Why ignore advice and randomly guess? Be systematic. Read, act, observe and report on every suggestion.
Hello again.
Post 7 & 15 were very good feedback i didn't ignore them. Here's the improved code on their basis :
#include <Servo.h>
// Broches pour les drivers de moteur L293D (côté gauche et côté droit)
const int IN1_leftRear = 2; // Driver gauche IN1 (moteur arrière gauche)
const int IN2_leftRear = 3; // Driver gauche IN2 (moteur arrière gauche)
const int IN3_leftFront = 4; // Driver gauche IN3 (moteur avant gauche)
const int IN4_leftFront = 5; // Driver gauche IN4 (moteur avant gauche)
const int IN1_rightRear = 6; // Driver droit IN3 (moteur arrière droit)
const int IN2_rightRear = 7; // Driver droit IN4 (moteur arrière droit)
const int IN1_rightFront = 8; // Driver droit IN1 (moteur avant droit)
const int IN2_rightFront = 9; // Driver droit IN2 (moteur avant droit)
const int trigPin = 11; // Broche TRIG du capteur ultrason HC-SR04
const int echoPin = 12; // Broche ECHO du capteur ultrason HC-SR04
const int buzzerPin = 10; // Buzzer (signal)
const int servoPin = 13; // Servomoteur (signal)
// Angles du servomoteur (inversé : 0° = droite, 90° = centre, 180° = gauche)
const int SERVO_LEFT = 180;
const int SERVO_CENTER = 90;
const int SERVO_RIGHT = 0;
// Seuils de distance (en centimètres)
const int THRESHOLD_STOP = 30; // arrêter et éviter si obstacle < 25 cm
const int THRESHOLD_BUZZER = 20; // activer buzzer si obstacle < 20 cm
Servo servo; // objet Servo pour le capteur ultrason
// Fonction pour mesurer la distance en cm avec le capteur ultrasonique
int measureDistance() {
// Envoyer une impulsion ultrasonore
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Lire la durée de l'écho (pulseIn renvoie le temps en microsecondes)
unsigned long duration = pulseIn(echoPin, HIGH, 30000UL); // timeout après 30 ms (~5 m)
if (duration == 0) {
// Aucun écho reçu (obstacle hors de portée)
return 300; // valeur élevée par défaut si pas d'obstacle détecté
}
// Calculer la distance en cm (≈58 µs aller-retour par cm)
int distance = duration / 58;
return distance;
}
// Fonctions de contrôle des moteurs
void stopMotors() {
// Arrêter tous les moteurs (mettre toutes les entrées LOW)
digitalWrite(IN1_leftRear, LOW);
digitalWrite(IN2_leftRear, LOW);
digitalWrite(IN3_leftFront, LOW);
digitalWrite(IN4_leftFront, LOW);
digitalWrite(IN1_rightRear, LOW);
digitalWrite(IN2_rightRear, LOW);
digitalWrite(IN1_rightFront, LOW);
digitalWrite(IN2_rightFront, LOW);
}
void moveForward() {
// Avancer : moteurs gauche en avant (IN1 HIGH, IN2 LOW) et moteurs droit en avant
digitalWrite(IN1_leftRear, HIGH);
digitalWrite(IN2_leftRear, LOW);
digitalWrite(IN3_leftFront, HIGH);
digitalWrite(IN4_leftFront, LOW);
digitalWrite(IN1_rightRear, HIGH);
digitalWrite(IN2_rightRear, LOW);
digitalWrite(IN1_rightFront, HIGH);
digitalWrite(IN2_rightFront, LOW);
}
void turnLeft() {
// Tourner à gauche (pivot sur place) : gauche en arrière, droite en avant
digitalWrite(IN1_leftRear, LOW);
digitalWrite(IN2_leftRear, HIGH);
digitalWrite(IN3_leftFront, LOW);
digitalWrite(IN4_leftFront, HIGH);
digitalWrite(IN1_rightRear, HIGH);
digitalWrite(IN2_rightRear, LOW);
digitalWrite(IN1_rightFront, HIGH);
digitalWrite(IN2_rightFront, LOW);
delay(500); // pivoter pendant 0,5 s (ajuster si besoin)
stopMotors(); // marquer un arrêt après le virage
}
void turnRight() {
// Tourner à droite (pivot sur place) : gauche en avant, droite en arrière
digitalWrite(IN1_leftRear, HIGH);
digitalWrite(IN2_leftRear, LOW);
digitalWrite(IN3_leftFront, HIGH);
digitalWrite(IN4_leftFront, LOW);
digitalWrite(IN1_rightRear, LOW);
digitalWrite(IN2_rightRear, HIGH);
digitalWrite(IN1_rightFront, LOW);
digitalWrite(IN2_rightFront, HIGH);
delay(500); // pivoter pendant 0,5 s
stopMotors(); // marquer un arrêt après le virage
}
void setup() {
Serial.begin(9600);
// Configurer les broches des moteurs en sortie
pinMode(IN1_leftRear, OUTPUT);
pinMode(IN2_leftRear, OUTPUT);
pinMode(IN3_leftFront, OUTPUT);
pinMode(IN4_leftFront, OUTPUT);
pinMode(IN1_rightRear, OUTPUT);
pinMode(IN2_rightRear, OUTPUT);
pinMode(IN1_rightFront, OUTPUT);
pinMode(IN2_rightFront, OUTPUT);
stopMotors(); // s'assurer que les moteurs sont arrêtés au démarrage
// Configurer les broches du capteur ultrason
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Configurer la broche du buzzer
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
// Initialiser le servomoteur (orientation centrale)
servo.attach(servoPin);
servo.write(SERVO_CENTER);
delay(350); // délai pour que le servo atteigne le centre
}
void loop() {
// Mesurer la distance devant le robot
int distance = measureDistance();
Serial.println(distance);
if (distance < THRESHOLD_STOP) {
// **Obstacle proche détecté (< 25 cm)**
stopMotors(); // arrêt immédiat
// Activer le buzzer si obstacle très proche (< 20 cm)
if (distance < THRESHOLD_BUZZER) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
// Scanner à gauche puis à droite pour évaluer les distances
int distanceLeft, distanceRight;
servo.write(SERVO_LEFT);
delay(180); // attendre que le servo atteigne la position gauche
distanceLeft = measureDistance();
servo.write(SERVO_RIGHT);
delay(180); // attendre que le servo atteigne la position droite
distanceRight = measureDistance();
// Revenir au centre (face avant)
servo.write(SERVO_CENTER);
delay(180);
// Choisir la direction la plus dégagée et tourner le véhicule
if (distanceLeft > distanceRight) {
turnLeft();
} else {
turnRight();
}
// Désactiver le buzzer après le virage (direction changée)
digitalWrite(buzzerPin, LOW);
// (La boucle loop continue, le robot avancera à nouveau si la voie est libre)
}
else {
// **Aucun obstacle proche** : avancer tout droit
moveForward();
digitalWrite(buzzerPin, LOW); // s'assurer que le buzzer est éteint
}
// petite pause pour éviter des mesures trop fréquentes
}
I m looking into your servo() suggestion still sir
POST #25 i cannot apply it, it's risky to redo it all over again sir when i don't have much time before my dissertation!
Thing is now, captor works VERY FINE with this code, but only when connected to my LAPTOP. Not so much when it's the battery that's powering it..