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

Arduino Nano Controlled Quad DC Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino Nano Controlled Quad DC Motor Driver

Circuit Documentation

Summary

This circuit is designed to control four DC motors using two L298N DC motor drivers, which are interfaced with an Arduino Nano microcontroller. The motors are powered by two sets of AA batteries. The Arduino Nano receives commands via its serial interface to control the direction and speed of the motors. The circuit also includes a Raspberry Pi 5, although its specific role in the circuit is not defined in the provided information.

Component List

Arduino Nano

L298N DC Motor Driver (x2)

DC Motor (x4)

2 x AA Battery Mount (x2)

Raspberry Pi 5

Wiring Details

Arduino Nano

L298N DC Motor Driver

DC Motors

2 x AA Battery Mount

Raspberry Pi 5

Documented Code

// Define control pins for the first L298N motor driver
int ena1 = 3;
int motor1in1 = 2;
int motor1in2 = 4;
int motor2in1 = 6;
int motor2in2 = 7;
int enb1 = 5;

// Define control pins for the second L298N motor driver
int ena2 = 9;
int motor3in1 = 8;
int motor3in2 = 11;
int motor4in1 = 12;
int motor4in2 = 13;
int enb2 = 10;

String inputBuff;  // Buffer to hold serial input
int i = 0;        // Index for processing input

void setup() {
  // Initialize motor pins as outputs
  pinMode(ena1, OUTPUT);
  pinMode(motor1in1, OUTPUT);
  pinMode(motor1in2, OUTPUT);
  pinMode(enb1, OUTPUT);
  pinMode(motor2in1, OUTPUT);
  pinMode(motor2in2, OUTPUT);

  pinMode(ena2, OUTPUT);
  pinMode(motor3in1, OUTPUT);
  pinMode(motor3in2, OUTPUT);
  pinMode(enb2, OUTPUT);
  pinMode(motor4in1, OUTPUT);
  pinMode(motor4in2, OUTPUT);
  
  Serial.begin(9600);  // Initialize serial communication
}

char direction;
int speed;

void loop() {
  // Check if data is available on serial
  inputBuff = "";
  if (Serial.available() > 0) {
    inputBuff = Serial.readStringUntil('\n');  // Read input until newline
    if (inputBuff.length() != 0) {
      direction = inputBuff.charAt(0);
      speed = inputBuff.substring(1).toInt();
      processInput(direction, speed);
      i += 1;
      if (i >= inputBuff.length()) {
        i = 0;  // Reset index after processing the buffer
      }
    }
  }
}

// Function to process the input character
void processInput(char input, int speed) {
  switch (input) {
    case 'f':  // Move forward
      moveForward(speed);
      break;
    case 'b':  // Move backward
      moveBackward(speed);
      break;
    case 'a':  // Turn left
      turnRight(speed);
      break;
    case 'd':  // Turn right
      turnLeft(speed);
      break;
    case 'r':
      rotateRight(speed);
      break;
    case 'l':
      rotateLeft(speed);
      break;
    default:
      stopAllMotors();  // Stop if input is not recognized
      break;
  }
}

// Function to move forward
void moveForward(int speed) {
  // Set speed and direction for motors 1 and 2
  analogWrite(ena1, speed);
  digitalWrite(motor1in1, HIGH);
  digitalWrite(motor1in2, LOW);

  analogWrite(enb1, speed);
  digitalWrite(motor2in1, HIGH);
  digitalWrite(motor2in2, LOW);

  // Set speed and direction for motors 3 and 4
  analogWrite(ena2, speed);
  digitalWrite(motor3in1, HIGH);
  digitalWrite(motor3in2, LOW);

  analogWrite(enb2, speed);
  digitalWrite(motor4in1, HIGH);
  digitalWrite(motor4in2, LOW);
}

// Function to move backward
void moveBackward(int speed) {
  // Set speed and reverse direction for motors 1 and 2
  analogWrite(ena1, speed);
  digitalWrite(motor1in1, LOW);
  digitalWrite(motor1in2, HIGH);

  analogWrite(enb1, speed);
  digitalWrite(motor2in1, LOW);
  digitalWrite(motor2in2, HIGH);

  // Set speed and reverse direction for motors 3 and 4
  analogWrite(ena2, speed);
  digitalWrite(motor3in1, LOW);
  digitalWrite(motor3in2, HIGH);

  analogWrite(enb2, speed);
  digitalWrite(motor4in1, LOW);
  digitalWrite(motor4in2, HIGH);
}

// Function to rotate right
void rotateRight(int speed) {
  // Motors 1 and 2 go backward
  analogWrite(ena1, speed);
  digitalWrite(motor1in1, LOW);
  digitalWrite(motor1in2, HIGH);

  analogWrite(enb1, speed);
  digitalWrite(motor2in1, LOW);
  digitalWrite(motor2in2, HIGH);

  // Motors 3 and 4 go forward
  analogWrite(ena2, speed);
  digitalWrite(motor3in1, HIGH);
  digitalWrite(motor3in2, LOW);

  analogWrite(enb2, speed);
  digitalWrite(motor4in1, HIGH);
  digitalWrite(motor4in2, LOW);
}

// Function to rotate left
void rotateLeft(int speed) {
  // Motors 1 and 2 go forward
  analogWrite(ena1, speed);
  digitalWrite(motor1in1, HIGH);
  digitalWrite(motor1in2, LOW);

  analogWrite(enb1, speed);
  digitalWrite(motor2in1, HIGH);
  digitalWrite(motor2in2, LOW);

  // Motors 3 and 4 go backward
  analogWrite(ena2, speed);
  digitalWrite(motor3in1, LOW);
  digitalWrite(motor3in2, HIGH);

  analogWrite(enb2, speed);
  digitalWrite(motor4in1, LOW);
  digitalWrite(motor4in2, HIGH);
}

// Function to turn right (pivot to the right)
void turnRight(int speed) {
  // Motors on the left side go backward
  analogWrite(ena1, speed);
  digitalWrite(motor1in1, HIGH);
  digitalWrite(motor1in2, LOW);

  analogWrite(enb1, speed);
  digitalWrite(motor2in1, LOW);
  digitalWrite(motor2in2, HIGH);

  // Motors on the right side go forward
  analogWrite(ena2, speed);
  digitalWrite(motor3in1, LOW);
  digitalWrite(motor3in2, HIGH);

  analogWrite(enb2, speed);
  digitalWrite(motor4in1, HIGH);
  digitalWrite(motor4in2, LOW);
}

// Function to turn left (pivot to the left)
void turnLeft(int speed) {
  // Motors on the left side go backward
  analogWrite(ena1, speed);
  digitalWrite(motor1in1, LOW);
  digitalWrite(motor1in2, HIGH);

  analogWrite(enb1, speed);
  digitalWrite(motor2in1, HIGH);
  digitalWrite(motor2in2, LOW);