Arduino-Controlled Line-Following Robot with Ultrasonic Obstacle Detection
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a robot with two gearmotors, using an Arduino UNO as the central processing unit. It includes an HC-SR04 Ultrasonic Sensor for distance measurement, two IR sensors for line tracking, an L298N DC motor driver for motor control, a rocker switch for power management, and a 18650 Li-Ion battery as the power source.
Component List
HC-SR04 Ultrasonic Sensor
- Description: A sensor that measures distance using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
L298N DC Motor Driver
- Description: A module for controlling up to two DC motors with direction and speed control.
- Pins: OUT1-OUT4, 12V, GND, 5V, 5V-ENA-JMP-I/O, +5V-J1/J2, ENA/B, IN1-IN4
Rocker Switch
- Description: A switch to toggle the power supply to the circuit.
- Pins: Output, Input
Hobby Gearmotor with 48:1 Gearbox (x2)
- Description: A DC motor with a gearbox for increased torque.
- Pins: Pin 1, Pin 2
IR Sensor (x2)
- Description: A sensor that detects infrared light, used for line tracking.
- Pins: Out, GND, VCC
18650 Li-Ion Battery
- Description: A rechargeable battery providing power to the circuit.
- Pins: Positive, Negative
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V power supply from Arduino UNO.
- TRIG: Connected to pin D8 on Arduino UNO.
- ECHO: Connected to pin D9 on Arduino UNO.
- GND: Connected to the common ground.
Arduino UNO
- 5V: Provides power to HC-SR04 Ultrasonic Sensor, IR sensors, and L298N DC motor driver.
- GND: Common ground for the entire circuit.
- D8: Connected to TRIG pin of HC-SR04 Ultrasonic Sensor.
- D9: Connected to ECHO pin of HC-SR04 Ultrasonic Sensor.
- D13, D12, D11, D10: Control pins for L298N DC motor driver.
- D6, D5: PWM control pins for L298N DC motor driver ENA and ENB.
- D4, D2: Input pins for IR sensors.
L298N DC Motor Driver
- OUT1-OUT4: Connected to the pins of the two Hobby Gearmotors.
- 12V: Connected to the output of the rocker switch.
- GND: Connected to the common ground.
- 5V: Connected to 5V power supply from Arduino UNO.
- ENA, ENB: PWM control from Arduino UNO pins D5 and D6.
- IN1-IN4: Control signals from Arduino UNO pins D13, D12, D11, D10.
Rocker Switch
- Output: Connected to the 12V input of the L298N DC motor driver.
- Input: Connected to the Positive pin of the 18650 Li-Ion battery.
Hobby Gearmotor with 48:1 Gearbox
- Pin 1 and Pin 2: Connected to OUT1-OUT4 of the L298N DC motor driver.
IR Sensor
- Out: Connected to D4 and D2 on Arduino UNO for line tracking.
- GND: Connected to the common ground.
- VCC: Connected to 5V power supply from Arduino UNO.
18650 Li-Ion Battery
- Positive: Connected to the input of the rocker switch.
- Negative: Connected to the common ground.
Documented Code
// Definición pines EnA y EnB para el control de la velocidad
int VelocidadMotor1 = 6;
int VelocidadMotor2 = 5;
// Definición de los pines de control de giro de los motores In1, In2, In3 e In4
int Motor1A = 13;
int Motor1B = 12;
int Motor2C = 11;
int Motor2D = 10;
// Sensores infrarrojo - izquierdo y derecho
int infraPin = 2;
int infraPin1 = 4;
// Variables para la captura de los valores: 0 - fondo claro y 1 - línea negra
int valorInfra = 0;
int valorInfra1 = 0;
// Ultrasonicos
#define PIN_TRIG 9
#define PIN_ECHO 8
long duration, distancia;
// Configuración inicial
void setup() {
Serial.begin(9600);
delay(1000);
//configuración del ultrasonico
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// Establecemos modo de los pines de los sensores infrarrojo
pinMode(infraPin, INPUT);
pinMode(infraPin1, INPUT);
// Establecemos modo de los pines del control de motores
pinMode(Motor1A,OUTPUT);
pinMode(Motor1B,OUTPUT);
pinMode(Motor2C,OUTPUT);
pinMode(Motor2D,OUTPUT);
pinMode(VelocidadMotor1, OUTPUT);
pinMode(VelocidadMotor2, OUTPUT);
// Configuramos los dos motores a velocidad 150/255
analogWrite(VelocidadMotor1, 150);
analogWrite(VelocidadMotor2, 180);
// Configuramos sentido de giro
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
// Ejecución contínua
void loop() {
// Ultrasonico: Primero, generar un pulso corto de 2-5 microsegundos.
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(5);
digitalWrite(PIN_TRIG, HIGH);
// Después de ajustar un nivel de señal alto, esperamos unos 10 microsegundos. En este punto el sensor enviará señales con una frecuencia de 40 kHz.
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Tiempo de retardo de la señal acústica en el sonar.
duration = pulseIn(PIN_ECHO, HIGH);
// Ahora es el momento de convertir el tiempo a distancia
distancia = (duration / 2) / 29.1;
Serial.print("Distancia al objeto: ");
Serial.print(distancia);
Serial.println(" см.");
// Leemos el valor de los infrarrojo: 0 - fondo claro y 1 - línea negra
valorInfra = digitalRead(infraPin);
valorInfra1 = digitalRead(infraPin1);
Serial.println(valorInfra);
Serial.println(valorInfra1);
if (distancia > 10){ //mientras no detecte nada a cierta distancia no se detiene
// Cuatro escenarios: De frente
if(valorInfra == 0 && valorInfra1 == 0){
Serial.println("Ninguno en linea");
// Modificamos sentido de giro de los motores
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor2D, HIGH);
delay(20);
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D,LOW);
delay(20);
}
// El robot encuentra línea negra con el infrarrojo derecho y hay que corregir girando a la derecha
if(valorInfra == 0 && valorInfra1 == 1){
Serial.println("Derecho en linea");
// Modificamos sentido de giro de los motores
digitalWrite(Motor1A, LOW);
digitalWrite(Motor2D,LOW);
delay(25);
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor2D,LOW);
delay(20);
}
// El robot encuentra línea negra con el infrarrojo izquierdo y hay que corregir girando a la izquierda
if(valorInfra == 1 && valorInfra1 == 0){
Serial.println("Izquierdo en linea");
// Modificamos sentido de giro de los motores
digitalWrite(Motor1A,LOW);
digitalWrite(Motor2D, LOW);
delay(25);
digitalWrite(Motor1A,LOW);
digitalWrite(Motor2D