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

Arduino UNO Line Follower Robot with IR Sensors and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO Line Follower Robot with IR Sensors and L298N Motor Driver

Circuit Documentation

Summary

This document provides a detailed overview of a line follower robot circuit. The circuit uses two IR sensors to detect the line and two DC motors to adjust the robot's direction. The control logic is implemented using an Arduino UNO microcontroller, which interfaces with an L298N DC motor driver to control the motors. The circuit is powered by two 18650 Li-ion batteries.

Component List

  1. IR Sensor

    • Description: Infrared sensor used to detect the line.
    • Pins: out, gnd, vcc
  2. L298N DC Motor Driver

    • Description: Motor driver used to control the DC motors.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  3. DC Mini Metal Gear Motor

    • Description: DC motor used for driving the robot.
    • Pins: IN1, IN2
  4. 18650 Li-ion Battery x 2

    • Description: Power source for the circuit.
    • Pins: +, -
  5. Arduino UNO

    • Description: Microcontroller used to control the robot.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0

Wiring Details

IR Sensor 1

IR Sensor 2

L298N DC Motor Driver

DC Mini Metal Gear Motor 1

DC Mini Metal Gear Motor 2

18650 Li-ion Battery x 2

Code Documentation

/*
 * Arduino Line Follower Robot
 * This code controls a line follower robot using two IR sensors and two DC motors.
 * The IR sensors detect the line and the motors adjust the robot's direction.
 */

// Pin definitions
const int irSensorLeft = 8;
const int irSensorRight = 9;
const int motorLeftForward = 7;
const int motorLeftBackward = 4;
const int motorRightForward = 3;
const int motorRightBackward = 2;
const int enableLeft = 6;
const int enableRight = 5;

void setup() {
  // Initialize IR sensor pins as inputs
  pinMode(irSensorLeft, INPUT);
  pinMode(irSensorRight, INPUT);
  
  // Initialize motor control pins as outputs
  pinMode(motorLeftForward, OUTPUT);
  pinMode(motorLeftBackward, OUTPUT);
  pinMode(motorRightForward, OUTPUT);
  pinMode(motorRightBackward, OUTPUT);
  pinMode(enableLeft, OUTPUT);
  pinMode(enableRight, OUTPUT);
  
  // Enable motors
  digitalWrite(enableLeft, HIGH);
  digitalWrite(enableRight, HIGH);
}

void loop() {
  // Read IR sensor values
  int leftSensorValue = digitalRead(irSensorLeft);
  int rightSensorValue = digitalRead(irSensorRight);
  
  // Determine motor actions based on sensor values
  if (leftSensorValue == LOW && rightSensorValue == LOW) {
    // Both sensors on the line, move forward
    moveForward();
  } else if (leftSensorValue == LOW && rightSensorValue == HIGH) {
    // Left sensor on the line, right sensor off the line, turn right
    turnRight();
  } else if (leftSensorValue == HIGH && rightSensorValue == LOW) {
    // Right sensor on the line, left sensor off the line, turn left
    turnLeft();
  } else {
    // Both sensors off the line, stop
    stopMoving();
  }
}

void moveForward() {
  digitalWrite(motorLeftForward, HIGH);
  digitalWrite(motorLeftBackward, LOW);
  digitalWrite(motorRightForward, HIGH);
  digitalWrite(motorRightBackward, LOW);
}

void turnRight() {
  digitalWrite(motorLeftForward, HIGH);
  digitalWrite(motorLeftBackward, LOW);
  digitalWrite(motorRightForward, LOW);
  digitalWrite(motorRightBackward, HIGH);
}

void turnLeft() {
  digitalWrite(motorLeftForward, LOW);
  digitalWrite(motorLeftBackward, HIGH);
  digitalWrite(motorRightForward, HIGH);
  digitalWrite(motorRightBackward, LOW);
}

void stopMoving() {
  digitalWrite(motorLeftForward, LOW);
  digitalWrite(motorLeftBackward, LOW);
  digitalWrite(motorRightForward, LOW);
  digitalWrite(motorRightBackward, LOW);
}

This code controls the line follower robot by reading the values from the IR sensors and adjusting the motor directions accordingly. The setup function initializes the pins, and the loop function continuously checks the sensor values to determine the robot's movement. The helper functions moveForward, turnRight, turnLeft, and stopMoving control the motor actions.