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

Arduino-Based Line Following Robot with L298N Motor Driver and IR Sensor Array

View and Edit Project In Cirkit DesignerImage of Arduino-Based Line Following Robot with L298N Motor Driver and IR Sensor Array

Circuit Documentation

Summary

This document provides a detailed overview of a circuit designed to control two DC motors using an Arduino Expansion Board, an L298N motor driver, and a 5-channel IR sensor array. The circuit is powered by a LiPo battery and includes a rocker switch for power control. The Arduino is programmed to read the IR sensor values and adjust the motor speeds accordingly to follow a line.

Component List

  1. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver for controlling two 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
  2. DC Mini Metal Gear Motor

    • Description: Small DC motor with metal gears for increased torque.
    • Pins: IN1, IN2
  3. DC Mini Metal Gear Motor

    • Description: Small DC motor with metal gears for increased torque.
    • Pins: IN1, IN2
  4. LiPo Battery 2200mAH 30C

    • Description: Rechargeable lithium polymer battery.
    • Pins: VCC, GND
  5. Arduino Expansion Board

    • Description: Expansion board for Arduino with multiple I/O pins.
    • Pins: 8, 9, 10, 11, 12, 13, GND, AREF, RX, TX, 2, 3, 4, 5, 6, 7, G, V, 1, 0, 5V, SCL, SDA, 3V3, A0, A1, A2, A3, A4, A5, A6, A7, RST, NC, VIN, POWER
  6. 5 Channel IR Array

    • Description: Infrared sensor array for line following applications.
    • Pins: Ground, VCC (5V), OUT-1, OUT-2, OUT-3, OUT-4, OUT-5
  7. Rocker Switch (SPST)

    • Description: Single-pole single-throw switch for power control.
    • Pins: 1, 2

Wiring Details

L298N DC Motor Driver

DC Mini Metal Gear Motor

DC Mini Metal Gear Motor

LiPo Battery 2200mAH 30C

Arduino Expansion Board

5 Channel IR Array

Rocker Switch (SPST)

Code Documentation

// Pin definitions
const int motor1Pin1 = 2; // IN1 on L298N
const int motor1Pin2 = 3; // IN2 on L298N
const int motor2Pin1 = 4; // IN3 on L298N
const int motor2Pin2 = 5; // IN4 on L298N
const int enableA = 9;    // ENA on L298N
const int enableB = 10;   // ENB on L298N

const int irSensor1 = A1; // OUT-1 on IR array
const int irSensor2 = A2; // OUT-2 on IR array
const int irSensor3 = A3; // OUT-3 on IR array
const int irSensor4 = A4; // OUT-4 on IR array
const int irSensor5 = A5; // OUT-5 on IR array

// PID constants
float Kp = 0.5;
float Ki = 0.0;
float Kd = 0.1;

float lastError = 0;
float integral = 0;

void setup() {
  // Set motor control pins as outputs
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enableA, OUTPUT);
  pinMode(enableB, OUTPUT);

  // Set initial motor speed
  analogWrite(enableA, 200); // Adjust speed as needed
  analogWrite(enableB, 200); // Adjust speed as needed

  // Set IR sensor pins as inputs
  pinMode(irSensor1, INPUT);
  pinMode(irSensor2, INPUT);
  pinMode(irSensor3, INPUT);
  pinMode(irSensor4, INPUT);
  pinMode(irSensor5, INPUT);

  Serial.begin(9600); // Initialize serial communication for debugging
}

void loop() {
  // Read IR sensor values
  int sensor1 = analogRead(irSensor1);
  int sensor2 = analogRead(irSensor2);
  int sensor3 = analogRead(irSensor3);
  int sensor4 = analogRead(irSensor4);
  int sensor5 = analogRead(irSensor5);

  // Debugging: print sensor values
  Serial.print(sensor1);
  Serial.print(" ");
  Serial.print(sensor2);
  Serial.print(" ");
  Serial.print(sensor3);
  Serial.print(" ");
  Serial.print(sensor4);
  Serial.print(" ");
  Serial.print(sensor5);
  Serial.println();

  // Calculate error
  float error = (sensor1 * -2 + sensor2 * -1 + sensor4 * 1 + sensor5 * 2) / (sensor1 + sensor2 + sensor3 + sensor4 + sensor5);

  // PID calculations
  integral += error;
  float derivative = error -