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

Arduino Mega 2560 and L298N Motor Driver Controlled Robotic Car with DC Motors

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 and L298N Motor Driver Controlled Robotic Car with DC Motors

Circuit Documentation

Summary

This circuit is designed to control four DC motors using an L298N DC motor driver and an Arduino Mega 2560 microcontroller. The motors are powered by a 9V battery, and the Arduino is programmed to control the direction and speed of the motors.

Component List

L298N DC Motor Driver

DC Motor (4 units)

9V Battery

Arduino Mega 2560

Wiring Details

L298N DC Motor Driver

DC Motor (4 units)

9V Battery

Arduino Mega 2560

Code Documentation

Arduino Code

int motor1 = 8;
int motor2 = 7;
int motor3 = 6;
int motor4 = 5;

void setup() {
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
  pinMode(motor4, OUTPUT);
}

void forward() {
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, LOW);
  digitalWrite(motor3, HIGH);
  digitalWrite(motor4, LOW);
}

void backward() {
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, HIGH);
  digitalWrite(motor3, LOW);
  digitalWrite(motor4, HIGH);
}

void left() {
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, HIGH);
  digitalWrite(motor3, HIGH);
  digitalWrite(motor4, LOW);
}

void right() {
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, LOW);
  digitalWrite(motor3, LOW);
  digitalWrite(motor4, HIGH);
}

void stop() {
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, LOW);
  digitalWrite(motor3, LOW);
  digitalWrite(motor4, LOW);
}

void loop() {
  forward();
  delay(2000);
  backward();
  delay(5000);
  left();
  delay(3000);
  right();
  delay(4000);
  stop();
  delay(7000);
}

This code initializes four pins on the Arduino Mega 2560 as outputs to control the L298N DC motor driver. The setup function sets the pin modes, and the loop function cycles through various motor control functions (forward, backward, left, right, stop) with delays in between.