Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and IR Line Tracking
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a mobile robot equipped with DC motors, ultrasonic sensors, and infrared sensors. The Arduino UNO serves as the central microcontroller, interfacing with an HC-SR04 Ultrasonic Sensor for distance measurement, two IR sensors for line tracking, and an L298N DC motor driver to control the gearmotors attached to the robot's wheels. A rocker switch is used to control the power supply to the motor driver. The circuit is powered by a 7.4V battery.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- Used for controlling the sensors and motor driver
- Provides digital and analog I/O pins
HC-SR04 Ultrasonic Sensor
- Ultrasonic distance sensor
- Measures the distance to obstacles in front of the sensor
Gearmotor DC Wheels (Left and Right)
- DC motors with gear reduction
- Provide movement to the robot's wheels
L298N DC Motor Driver
- Dual H-bridge motor driver
- Controls the direction and speed of the DC motors
Rocker Switch (SPST)
- Single Pole Single Throw switch
- Used to control the power supply to the motor driver
IR Sensors
- Infrared sensors for line tracking
- Detects the presence of a line under the robot
7.4V Battery
- Power source for the circuit
- Supplies power to the motor driver and Arduino UNO
Wiring Details
Arduino UNO
5Vconnected to the VCC of HC-SR04 Ultrasonic Sensor and IR sensorsGNDconnected to the GND of HC-SR04 Ultrasonic Sensor, IR sensors, and L298N DC motor driverVinconnected to the 5V input of the L298N DC motor driverA0connected to the output of the right IR sensorA1connected to the output of the left IR sensorA2connected to the ECHO pin of the HC-SR04 Ultrasonic SensorA3connected to the TRIG pin of the HC-SR04 Ultrasonic SensorD5connected to the ENB pin of the L298N DC motor driverD6connected to the IN4 pin of the L298N DC motor driverD7connected to the IN3 pin of the L298N DC motor driverD8connected to the IN2 pin of the L298N DC motor driverD9connected to the IN1 pin of the L298N DC motor driverD10connected to the ENA pin of the L298N DC motor driver
HC-SR04 Ultrasonic Sensor
VCCconnected to the 5V of Arduino UNOGNDconnected to the GND of Arduino UNOECHOconnected to the A2 pin of Arduino UNOTRIGconnected to the A3 pin of Arduino UNO
Gearmotor DC Wheels (Left)
PIN1connected to the OUT4 pin of the L298N DC motor driverPIN2connected to the OUT3 pin of the L298N DC motor driver
Gearmotor DC Wheels (Right)
PIN1connected to the OUT2 pin of the L298N DC motor driverPIN2connected to the OUT1 pin of the L298N DC motor driver
L298N DC Motor Driver
GNDconnected to the GND of Arduino UNO and the negative terminal of the 7.4V battery5Vconnected to the Vin pin of Arduino UNO12Vconnected to the terminal 2 of the Rocker Switch (SPST)ENA,IN1,IN2,IN3,IN4,ENBconnected to the respective pins on the Arduino UNO as detailed aboveOUT1,OUT2,OUT3,OUT4connected to the respective pins on the Gearmotor DC Wheels
Rocker Switch (SPST)
- Terminal
1connected to the positive terminal of the 7.4V battery - Terminal
2connected to the 12V input of the L298N DC motor driver
IR Sensors
outconnected to the A0 and A1 pins of Arduino UNO for the right and left sensors, respectivelygndconnected to the GND of Arduino UNOvccconnected to the 5V of Arduino UNO
7.4V Battery
+connected to terminal 1 of the Rocker Switch (SPST)-connected to the GND of the L298N DC motor driver
Documented Code
// Declaration of pins for sensors, motors, and the ultrasonic sensor
const int IRDroit = A0;
const int IRGauche = A1;
const int brocheEcho = A2;
const int TrigUltra = A3;
const int enableMoteurGauche = 5;
const int moteurGauchePin1 = 6; // Forward direction
const int moteurGauchePin2 = 7; // Reverse direction
const int enableMoteurDroit = 10;
const int moteurDroitPin1 = 9; // Forward direction
const int moteurDroitPin2 = 8; // Reverse direction
void setup() {
pinMode(TrigUltra, OUTPUT);
pinMode(brocheEcho, INPUT);
pinMode(IRDroit, INPUT);
pinMode(IRGauche, INPUT);
pinMode(moteurGauchePin1, OUTPUT);
pinMode(moteurGauchePin2, OUTPUT);
pinMode(moteurDroitPin1, OUTPUT);
pinMode(moteurDroitPin2, OUTPUT);
pinMode(enableMoteurGauche, OUTPUT);
pinMode(enableMoteurDroit, OUTPUT);
Serial.begin(9600);
}
void loop() {
suivreLigneHauteVitesse();
long distance = lireDistanceUltrason();
if (distance < 20) {
approcherEtPousserBalle();
}
}
void suivreLigneHauteVitesse() {
bool irDroit = digitalRead(IRDroit);
bool irGauche = digitalRead(IRGauche);
if (irDroit == HIGH && irGauche == HIGH) {
avancer(255);
} else if (irDroit == HIGH && irGauche == LOW) {
tournerGauche(150);
} else if (irDroit == LOW && irGauche == HIGH) {
tournerDroite(150);
} else {
arreter();
}
}
// Function to approach and push the ball into the hole
void approcherEtPousserBalle() {
long distance = lireDistanceUltrason();
while (distance > 2) {
distance = lireDistanceUltrason();
int vitesse = map(distance, 2, 20, 100, 200); // Gradually slow down
avancer(vitesse);
delay(15);
}
// Check if the ball is in the hole
long distancePrecedente = distance;
distance = lireDistanceUltrason();
if (distance - distancePrecedente > 10) {
arreter();
} else {
avancer(100);
}
}
// Function to read the distance with the ultrasonic sensor
long lireDistanceUltrason() {
digitalWrite(TrigUltra, LOW);
delayMicroseconds(2);
digitalWrite(TrigUltra, HIGH);
delayMicroseconds(10);
digitalWrite(TrigUltra, LOW);
long duree = pulseIn(brocheEcho, HIGH);
long distance = duree * 0.0344 / 2;
return distance;
}
// Functions to control the motors
void avancer(int vitesse) {
analogWrite(enableMoteurGauche, vitesse);
analogWrite(enableMoteurDroit, vitesse);
digitalWrite(moteurGauchePin1, HIGH);
digitalWrite(moteurGauchePin2, LOW);
digitalWrite(moteurDroitPin1, HIGH);
digitalWrite(moteurDroitPin2, LOW);
}
void tournerGauche(int vitesse) {
analogWrite(enableMoteurGauche, vitesse);
analogWrite(enableMoteurDroit, vitesse);
digitalWrite(moteurGauchePin1, LOW);
digitalWrite(moteurGauchePin2, HIGH);
digitalWrite(moteurDroitPin1, HIGH);
digitalWrite(moteurDroitPin2, LOW);
}
void tournerDroite(int vitesse) {
analogWrite(enableMoteurGauche, vitesse);
analogWrite(enableMoteurDroit, vitesse);
digitalWrite(moteurGauchePin1, HIGH);
digitalWrite(moteurGauchePin2, LOW);
digitalWrite(moteurDroitPin1, LOW);
digitalWrite(moteurDroitPin2, HIGH);