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 obstacle avoidance and line-following capabilities. It uses an Arduino UNO as the central microcontroller to manage inputs from an HC-SR04 ultrasonic sensor and two IR sensors, and to control a servo and two DC motors through an L298N motor driver. A 9V battery powers the circuit, and a rocker switch is used to turn the power supply on and off.
Component List
HC-SR04 Ultrasonic Sensor
- Description: This sensor measures distance by emitting ultrasonic waves and measuring the time it takes for the echo to return.
- Pins: VCC, TRIG, ECHO, GND
Arduino UNO
- Description: A microcontroller board based on the ATmega328P, used for controlling the various components in the circuit.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
IR Sensor
- Description: An infrared sensor used for line detection.
- Pins: out, gnd, vcc
L298N DC Motor Driver
- Description: A motor driver module that controls the direction and speed of two DC motors.
- Pins: OUT1-OUT4, 12V, GND, 5V, 5V-ENA-JMP-I/O, +5V-J1/J2, ENA/ENB, IN1-IN4
Rocker Switch
- Description: A switch used to connect or disconnect the power supply to the circuit.
- Pins: output, input
Hobby Gearmotor with 48:1 Gearbox
- Description: A DC motor with a gearbox used to drive the robot's wheels.
- Pins: pin 1, pin 2
9V Battery
- Description: The power source for the circuit.
- Pins: -, +
Servo
- Description: A motor capable of precise control of angular position, used for steering or other actuation.
- Pins: gnd, vcc, pulse
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V power supply
- TRIG connected to Arduino UNO pin D8
- ECHO connected to Arduino UNO pin D9
- GND connected to common ground
Arduino UNO
- 5V and GND pins provide power and ground to other components
- Digital pins D8 and D9 connected to HC-SR04 Ultrasonic Sensor
- Digital pins D12, D13, D10, D11 control the L298N motor driver
- Digital pins D6 and D5 control the speed of the motors via the L298N motor driver
- Digital pins D4 and D2 connected to IR sensors
- Digital pin D1 connected to Servo pulse pin
IR Sensors
- out pin of one sensor connected to Arduino UNO pin D4
- out pin of the other sensor connected to Arduino UNO pin D2
- vcc pins connected to 5V power supply
- gnd pins connected to common ground
L298N DC Motor Driver
- IN1-IN4 connected to Arduino UNO pins D12, D13, D10, D11 for motor control
- ENA and ENB connected to Arduino UNO pins D5 and D6 for speed control
- OUT1-OUT4 connected to the pins of two Hobby Gearmotors
- 12V connected to the output of the rocker switch for power
- 5V and GND connected to 5V power supply and common ground, respectively
Rocker Switch
- input pin connected to the + pin of the 9V Battery
- output pin connected to the 12V pin of the L298N motor driver
Hobby Gearmotors
- pin 1 of one motor connected to OUT1 of the L298N motor driver
- pin 2 of the same motor connected to OUT2 of the L298N motor driver
- pin 1 of the other motor connected to OUT4 of the L298N motor driver
- pin 2 of the other motor connected to OUT3 of the L298N motor driver
9V Battery
-
- pin connected to the input of the rocker switch
-
- pin connected to common ground
Servo
- gnd pin connected to common ground
- vcc pin connected to 5V power supply
- pulse pin connected to Arduino UNO pin D1
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 ==