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.
5V
pin provides power to both IR sensors.GND
pin is connected to the ground of the IR sensors, L298N motor driver, and the negative terminal of the 9V battery.Vin
pin is connected to the 5V
pin of the L298N motor driver.D13
, D12
, D11
, D10
, D6
, and D5
are connected to the IN2
, IN1
, IN4
, IN3
, ENA
, and ENB
pins of the L298N motor driver, respectively.D4
and D2
are connected to the output of the IR sensors.+
terminal is connected to the 12V
pin of the L298N motor driver.-
terminal is connected to the ground of the circuit.pin 1
is connected to OUT2
of the L298N motor driver.pin 2
is connected to OUT1
of the L298N motor driver.pin 1
is connected to OUT4
of the L298N motor driver.pin 2
is connected to OUT3
of the L298N motor driver.OUT1
, OUT2
, OUT3
, and OUT4
are connected to the respective pins of the DC motors.12V
pin is connected to the positive terminal of the 9V battery.GND
pin is connected to the ground of the circuit.5V
pin is connected to the Vin
pin of the Arduino UNO.ENA
, IN1
, IN2
, IN3
, IN4
, and ENB
are connected to the respective pins of the Arduino UNO.out
pin is connected to D4
on the Arduino UNO.gnd
pin is connected to the ground of the circuit.vcc
pin is connected to 5V
on the Arduino UNO.out
pin is connected to D2
on the Arduino UNO.gnd
pin is connected to the ground of the circuit.vcc
pin is connected to 5V
on the Arduino UNO.// 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.