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

Arduino Nano Controlled Line Follower Robot with L298N Motor Driver and IR Sensors

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

Circuit Documentation

Summary

This circuit is designed to control a line follower robot using an Arduino Nano as the central processing unit. The robot is equipped with an L298N DC motor driver to control two DC Mini Metal Gear Motors, a 5-channel IR sensor for line tracking, and multiple pushbuttons and LEDs for user interaction and status indication. The circuit also includes a buck converter and a boost converter to manage power distribution, and it is powered by a 12V battery. The Arduino Nano is programmed to handle sensor inputs, motor control, and user inputs to navigate the robot along a predefined path.

Component List

Arduino Nano

A compact microcontroller board based on the ATmega328P, featuring digital and analog I/O pins, which serves as the brain of the robot.

L298N DC Motor Driver

A module capable of driving two DC motors with direction and speed control, used for actuating the robot's wheels.

Buck Converter

A DC-DC converter that steps down voltage from a higher level to a lower level, ensuring that the components receive the appropriate voltage.

XL6009E1 Boost Converter

A DC-DC converter that steps up voltage from a lower level to a higher level, used to increase voltage when necessary.

DC Mini Metal Gear Motor (x2)

Small DC motors with gear reduction, providing the necessary torque and speed for the robot's movement.

LED: Two Pin (red) (x2)

Simple red LEDs used for status indication on the robot.

Pushbutton (x4)

Tactile switches used to change modes and control the robot manually.

Resistor

A 220 Ohm resistor used to limit current to the LEDs, protecting them from damage due to overcurrent.

5 Channel IR Sensor

An array of infrared sensors used to detect the line on the surface and provide feedback for the line following algorithm.

Battery 12V

A 12V battery that provides power to the entire circuit.

Wiring Details

Arduino Nano

L298N DC Motor Driver

Buck Converter

XL6009E1 Boost Converter

DC Mini Metal Gear Motor

LED: Two Pin (red)

Pushbutton

Resistor

5 Channel IR Sensor

Battery 12V

Documented Code

/*
 * Arduino Sketch for Line Follower Robot
 * This code controls a line follower robot using an Arduino Nano, L298N motor driver,
 * and IR sensors. The robot follows a line based on sensor readings and uses PID
 * control for smooth movement. It also includes pushbutton controls for different
 * modes and LED indicators.
 */

// Motor pin defines
#define rmf 4 // IN1
#define rmb 7 // IN2
#define lmf 3 // IN3
#define lmb 2 // IN4
#define rms 6 // ENA
#define lms 5 // ENB

// Pushbutton and LED pin defines
int button1 = 8;
int button2 = 9;
int button3 = 10;
int button4 = 11;
int led12 = 12;
int led13 = 13;
bool button1_state, button2_state, button3_state, button4_state;

// Line follower variables
int s[6], sum;
int position[6] = {1, 2, 3, 4, 5, 6};
int threshold = 512, sensor;
int lbase = 200, rbase = 200;
int lmotor, rmotor;
float avg;
int kp = 50, kd = 1000;
int PID;
float error, last_error;
char turn;
int base[6] = {1, 2, 4, 8, 16, 32};
int tsp = 100;

void setup() {
  // Motor driver pins as output
  pinMode(lmf, OUTPUT);
  pinMode(lmb, OUTPUT);
  pinMode(rmf, OUTPUT);
  pinMode(rmb, OUTPUT);
  pinMode(rms, OUTPUT);
  pinMode(lms, OUTPUT);

  // Button pins as input with pull-up resistors
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);

  // LED pins as output
  pinMode(led12, OUTPUT);
  pinMode(led13, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  digitalWrite(led13, HIGH);
  button_status();

  // Line following mode
  while (button1_state != 1) {
    digitalWrite(led13, LOW);
    for (int i = 0; i < 5; i++) {
      digitalWrite(led12, HIGH);
      delay(100);
      digitalWrite(led12, LOW);
      delay(100);
    }
    PID_LINE_FOLLOW();
  }

  // Motor test mode
  while (button2_state != 1) {
    digitalWrite(led13, LOW);
    for (int i = 0; i < 3; i++) {
      digitalWrite(led12, HIGH);
      delay(500);
      digitalWrite(led12, LOW);
      delay(500);
    }
    motor(250, 250);
    delay(3000);
    motor(0, 0);
    button_status();
  }

  // Sensor test mode
  while (button3_state != 1) {
    digitalWrite(led13, LOW);
    for (short int i = 0; i <= 5; i++) {
      Serial.print(String(analogRead(i)) + " ");
    }
    Serial.println();
    digitalWrite(led12, HIGH);
    delay(50);
    digitalWrite(led12, LOW);
    delay(50);
  }

  // Stop mode
  while (button4_state != 1) {
    motor(0, 0);
    digitalWrite(led13, HIGH);
    button_status();
  }
}

void button_status() {
  button1_state = digitalRead(button1);
  button2_state = digitalRead(button2);
  button3_state = digitalRead(button3);
  button4_state = digitalRead(button4);
}

void PID_LINE_FOLLOW() {
  sensor = 0;
  sum = 0;
  for (byte i = 0; i < 6; i++) {
    s[i] = analogRead(i);
    s[i] = (s[i] > threshold) ? 1 : 0;
    sum += s[i] * position[i];
  }