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

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

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

Circuit Documentation

Summary

This circuit is designed to control a pair of DC motors using an Arduino UNO microcontroller in conjunction with an L298N DC motor driver. The system also includes two infrared sensors for detecting line tracking, which is commonly used in robotics for following paths. The Arduino UNO is powered by a 9V battery and controls the motor driver, which in turn controls the speed and direction of the DC motors. The infrared sensors are used to provide input to the Arduino, which then makes decisions to adjust the motors accordingly.

Component List

Arduino UNO

9V Battery

DC Motor (x2)

L298N DC Motor Driver

IR Sensor (x2)

Wiring Details

Arduino UNO

9V Battery

DC Motors

L298N DC Motor Driver

IR Sensors

Documented Code

// Definición de los pines para el control de la velocidad
int VelocidadMotor1 = 6;
int VelocidadMotor2 = 5;

// Definición de los pines de control de giro de los motores
int Motor1A = 13;
int Motor1B = 12;
int Motor2C = 11;
int Motor2D = 10;

// Sensores infrarrojo - izquierdo y derecho
int infraPin = 2;
int infraPinL = 4;

// Variables para la captura de los valores: 0 - fondo claro y 1 - línea negra
int valorInfra = 0;
int valorInfral = 0;

// Configuración inicial
void setup() {
  Serial.begin(9600);
  delay(1000);

  // Establecemos modo de los pines de los sensores infrarrojo
  pinMode(infraPin, INPUT);
  pinMode(infraPinL, INPUT);

  // Establecemos modo de los pines del control de motores
  pinMode(Motor1A, OUTPUT);
  pinMode(Motor1B, OUTPUT);
  pinMode(Motor2C, OUTPUT);
  pinMode(Motor2D, OUTPUT);

  // Configuramos los dos motores a velocidad 150/255
  analogWrite(VelocidadMotor1, 150);
  analogWrite(VelocidadMotor2, 150);

  // Configuramos sentido de giro inicial (motores apagados en este caso)
  digitalWrite(Motor1A, LOW);
  digitalWrite(Motor1B, LOW);
  digitalWrite(Motor2C, LOW);
  digitalWrite(Motor2D, LOW);
}

// Ejecución continua
void loop() {
  // Leemos el valor de los sensores infrarrojos: 0 - fondo claro y 1 - línea negra
  valorInfra = digitalRead(infraPin);
  valorInfral = digitalRead(infraPinL);

  Serial.print("Valor Infra: ");
  Serial.print(valorInfra);
  Serial.print(" Valor Infral: ");
  Serial.println(valorInfral);

  // Cuatro escenarios:

  // Ninguno en línea
  if (valorInfra == 0 && valorInfral == 0) {
    Serial.println("Ninguno en línea");
    // Ambos motores hacia adelante
    digitalWrite(Motor1A, HIGH);
    digitalWrite(Motor1B, LOW);
    digitalWrite(Motor2C, HIGH);
    digitalWrite(Motor2D, LOW);
  }

  // Derecho en línea
  else if (valorInfra == 0 && valorInfral == 1) {
    Serial.println("Derecho en línea");
    // Girar a la derecha
    digitalWrite(Motor1A, LOW);
    digitalWrite(Motor1B, LOW);
    digitalWrite(Motor2C, HIGH);
    digitalWrite(Motor2D, LOW);
  }

  // Izquierdo en línea
  else if (valorInfra == 1 && valorInfral == 0) {
    Serial.println("Izquierdo en línea");
    // Girar a la izquierda
    digitalWrite(Motor1A, HIGH);
    digitalWrite(Motor1B, LOW);
    digitalWrite(Motor2C, LOW);
    digitalWrite(Motor2D, LOW);
  }

  // Ambos sensores en línea (final del circuito)
  else if (valorInfra == 1 && valorInfral == 1) {
    Serial.println("Ambos en línea");
    // Parar los motores
    digitalWrite(Motor1A, LOW);
    digitalWrite(Motor1B, LOW);
    digitalWrite(Motor2C, LOW);
    digitalWrite(Motor2D, LOW);
  }

  delay(100); // Añadido para evitar lecturas demasiado rápidas
}

This code is designed to be uploaded to an Arduino UNO microcontroller. It sets up the pins for motor control and reads from two infrared sensors to determine the path for a line-following robot. Depending on the sensor readings, it adjusts the motor speeds and directions to follow the line.