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
- Microcontroller board based on the ATmega328P
- It has a variety of digital and analog I/O pins
- Used for controlling the L298N motor drivers
L298N DC Motor Driver (x2)
- Module capable of driving two DC motors
- It has pins for motor outputs, power supply, and control inputs
- Used to drive the DC motors based on commands from the Arduino Nano
DC Motor (x4)
- Standard DC motors with two connection pins
- Used as actuators in the circuit
2 x AA Battery Mount (x2)
- Battery holders for powering the circuit
- Each mount holds two AA batteries
Raspberry Pi 5
- A single-board computer with a variety of I/O options
- Its role in this circuit is not specified
Wiring Details
Arduino Nano
VINconnected to the positive terminal of the AA battery mountsGNDconnected to the negative terminal of the AA battery mounts5Vconnected to the 5V pin on both L298N motor drivers- Digital pins
D2,D3,D4,D5,D6,D7,D8,D9,D10,D11/MOSI,D12/MISO,D13/SCKare used to control the L298N motor drivers
L298N DC Motor Driver
12VandGNDconnected to the corresponding terminals of the AA battery mounts5Vconnected to the 5V output of the Arduino NanoENA,ENB,IN1,IN2,IN3,IN4controlled by the Arduino NanoOUT1,OUT2,OUT3,OUT4connected to the DC motors
DC Motors
- Each motor has two pins connected to the outputs of the L298N motor drivers
2 x AA Battery Mount
+and-terminals provide power to the Arduino Nano and L298N motor drivers
Raspberry Pi 5
- No specific wiring details provided
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);
