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

Arduino-Controlled Line-Following Robot with Dual IR Sensors and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Line-Following Robot with Dual IR Sensors and L298N Motor Driver

Circuit Documentation

Summary

The circuit in question is designed to control a pair of DC motors using an Arduino UNO microcontroller and an L298N DC motor driver. The system also includes two infrared sensors for detecting track lines, which are used to guide the motors' actions. A 9V battery provides the power source for the motor driver and indirectly for the Arduino through voltage regulation.

Component List

Arduino UNO

9V Battery

DC Motor (x2)

L298N DC Motor Driver

IR Sensor (x2)

Wiring Details

Arduino UNO

9V Battery

DC Motor #1

DC Motor #2

L298N DC Motor Driver

IR Sensor #1

IR Sensor #2

Documented Code

// Definition of pins for motor speed control
// Activates ENA (enable a) and ENB (enable b)
int velmotor1 = 6;
int velmotor2 = 5;

// Definition of motor control pins In1, In2, In3, In4
int motor1A = 13;
int motor1B = 12;
int motor2C = 11;
int motor2D = 10;

// Infrared sensors - left and right
// Will detect the track to activate functions
int infrapinr = 2;
int infrapinl = 4;

// Variable to capture the values - 0 for light ground and 1 for dark ground
int valorinfrar = 0;
int valorinfral = 0;

// Initial configuration
void setup() {
  Serial.begin(9600);
  delay(1000);
  // Set the mode of the infrared sensor pins
  pinMode(infrapinr, INPUT);
  pinMode(infrapinl, INPUT);
  
  // Set the mode of the motor control pins
  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2C, OUTPUT);
  pinMode(motor2D, OUTPUT);
  pinMode(velmotor1, OUTPUT);
  pinMode(velmotor2, OUTPUT);
  
  // Set the motor speed between 150/255
  analogWrite(velmotor1, 150);
  analogWrite(velmotor2, 150);
  
  // Configure the direction of rotation
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, LOW);
  digitalWrite(motor2C, LOW);
  digitalWrite(motor2D, LOW);
}

void loop() {
  // Read the values of the infrared sensors - 0 for light ground and 1 for dark ground
  valorinfrar = digitalRead(infrapinr);
  valorinfral = digitalRead(infrapinl);
  Serial.println(valorinfrar);
  Serial.println(valorinfral);

  // Four scenarios
  // Straight ahead
  if (valorinfrar == 0 & valorinfral == 0) {
    Serial.println("none on line");
    digitalWrite(motor1A, HIGH);
    digitalWrite(motor2D, HIGH);
    delay(20);

    digitalWrite(motor1A, LOW);
    digitalWrite(motor2D, LOW);
    delay(20);
  }
  // Line found on the right side
  if (valorinfrar == 1 & valorinfral == 0) {
    Serial.println("right on line");
    digitalWrite(motor1A, LOW);
    digitalWrite(motor2D, LOW);
    delay(25);

    digitalWrite(motor1A, HIGH);
    digitalWrite(motor2D, LOW);
    delay(20);
  }
  // Line found on the left side
  if (valorinfrar == 0 & valorinfral == 1) {
    Serial.println("left on line");
    digitalWrite(motor1A, LOW);
    digitalWrite(motor2D, LOW);
    delay(25);

    digitalWrite(motor1A, LOW);
    digitalWrite(motor2D, HIGH);
    delay(20);
  }
  // Line found on both sides
  if (valorinfrar == 1 & valorinfral == 1) {
    Serial.println("both on line");
    // Stop the motors
    digitalWrite(motor1A, LOW);
    digitalWrite(motor1B, LOW);
    digitalWrite(motor2C, LOW);
    digitalWrite(motor2D, LOW);
  }
}

This code is responsible for reading the infrared sensors and controlling the motors based on the sensor readings. It sets up the motor control pins and the infrared sensor pins, defines the initial motor speed, and then enters a loop where it reads the sensor values and controls the motors accordingly. The comments within the code provide additional context for each section and operation.