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

Arduino Mega 2560 Controlled 4-Wheel Line Following Robot with IR Sensors and L298N Motor Drivers

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Controlled 4-Wheel Line Following Robot with IR Sensors and L298N Motor Drivers

Circuit Documentation

Summary

The circuit is designed to control a 4-wheel line-following robot using an Arduino Mega 2560 as the central processing unit. The robot utilizes two L298N motor drivers to control four yellow hobby gear motors, and it detects the line using two IR sensors. Additionally, the circuit includes an MLX90614 infrared thermometer for temperature sensing, a DS3231 Real-Time Clock (RTC) for timekeeping, a Graphic LCD Display (GLCD 128x64) for visual output, and power is supplied by an 18650 Li-Ion battery. The circuit also features a resistor and a trimmer potentiometer for voltage adjustments.

Component List

Microcontroller

Motors

Motor Drivers

Sensors

Display

Power Supply

Passive Components

Wiring Details

Arduino Mega 2560

Motor Amarillo Motorreductor Hobby

L298N DC Motor Driver

IR Sensor

MLX90614

DS3231 RTC

Graphic LCD Display (GLCD 128x64)

Resistor

Trimmer Potentiometer

18650 Li-Ion Battery

Documented Code

/*
 * Arduino Sketch for 4-Wheel Line Following Robot
 *
 * This code controls a 4-wheel robot that follows a line using IR sensors.
 * The robot uses two L298N motor drivers to control the motors and IR sensors
 * to detect the line. The motors are connected to the motor drivers, which are
 * controlled by the Arduino Mega 2560. The IR sensors provide input to the
 * Arduino to determine the position of the line.
 */

// Pin definitions
#define ENA 9
#define ENB 10
#define IN1 A0
#define IN2 A1
#define IN3 A2
#define IN4 A3
#define ENA2 11
#define ENB2 12
#define IN1_2 A4
#define IN2_2 A5
#define IN3_2 A6
#define IN4_2 A7
#define IR_LEFT A8
#define IR_RIGHT A9

void setup() {
  // Initialize motor control pins
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA2, OUTPUT);
  pinMode(ENB2, OUTPUT);
  pinMode(IN1_2, OUTPUT);
  pinMode(IN2_2, OUTPUT);
  pinMode(IN3_2, OUTPUT);
  pinMode(IN4_2, OUTPUT);
  // Initialize IR sensor pins
  pinMode(IR_LEFT, INPUT);
  pinMode(IR_RIGHT, INPUT);
}

void loop() {
  // Read IR sensor values
  int leftSensor = digitalRead(IR_LEFT);
  int rightSensor = digitalRead(IR_RIGHT);

  // Determine motor actions based on sensor values
  if (leftSensor == HIGH && rightSensor == LOW) {
    // Turn right
    moveRight();
  } else if (leftSensor == LOW && rightSensor == HIGH) {
    // Turn left
    moveLeft();
  } else if (leftSensor == LOW && rightSensor == LOW) {
    // Move forward
    moveForward();
  } else {
    // Stop
    stopMotors();
  }
}

void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(IN1_2, HIGH);
  digitalWrite(IN2_2, LOW);
  digitalWrite(IN3_2, HIGH);
  digitalWrite(IN4_2, LOW);
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);
  analogWrite(ENA2, 255);
  analogWrite(ENB2, 255);
}

void moveLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(IN1_2, LOW);
  digitalWrite(IN2_2, HIGH);
  digitalWrite(IN3_2, HIGH);
  digitalWrite(IN4_2, LOW);
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);
  analogWrite(ENA2, 255);
  analogWrite(ENB2, 255);
}

void moveRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(IN1_2, HIGH);
  digitalWrite(IN2_2, LOW);
  digitalWrite(IN3_2, LOW);
  digitalWrite(IN4_2, HIGH);
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);
  analogWrite(ENA2, 255);
  analogWrite(ENB2, 255);
}

void stopMotors() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(IN1_2, LOW);
  digitalWrite(IN2_2, LOW);
  digitalWrite(IN3_2, LOW);
  digitalWrite(IN4_2, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
  analogWrite(ENA2, 0);
  analogWrite(ENB2, 0);
}

This code is responsible for controlling the robot's movement based on the input from the IR sensors. It sets up the necessary pins as inputs or outputs and contains functions to move the robot forward, left, right, or stop it entirely. The IR sensors' readings determine the robot's direction, while the motor drivers receive signals from the Arduino to control the motors' speed and