Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and IR Line Tracking

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and IR Line Tracking

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

HC-SR04 Ultrasonic Sensor

Gearmotor DC Wheels (Left and Right)

L298N DC Motor Driver

Rocker Switch (SPST)

IR Sensors

7.4V Battery

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Gearmotor DC Wheels (Left)

Gearmotor DC Wheels (Right)

L298N DC Motor Driver

Rocker Switch (SPST)

IR Sensors

7.4V Battery

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);