This project demonstrates how to control two DC motors using an Arduino Nano, a L298 Motor Driver, and a Bluetooth HC-05 module. The motors can be controlled wirelessly from a mobile device using a Bluetooth app, which communicates with the Arduino via the HC-05 Bluetooth module.
Component Connections:
1. Motor Driver (L298) to Arduino Nano
- L298 Motor Driver is used to control the motors. It’s connected to the Arduino Nano as follows:
L298 Pin | Arduino Nano Pin | Function |
---|---|---|
IN1 | Pin 8 | Control Motor 1 (Forward/Backward) |
IN2 | Pin 9 | Control Motor 1 (Forward/Backward) |
ENA | Pin 10 (PWM) | Enable Motor 1 (Speed Control) |
IN3 | Pin 11 | Control Motor 2 (Forward/Backward) |
IN4 | Pin 12 | Control Motor 2 (Forward/Backward) |
ENB | Pin 13 (PWM) | Enable Motor 2 (Speed Control) |
VCC | Motor Power Supply | Power the L298 Motor Driver (from Battery) |
GND | GND (Arduino & Battery) | Common Ground (shared between Arduino, L298, and power supply) |
2. Bluetooth HC-05 Module to Arduino Nano
The Bluetooth HC-05 module is used for wireless communication between the Arduino and the mobile app. It’s connected as follows:
HC-05 Pin | Arduino Nano Pin | Function |
---|---|---|
VCC | 5V (Arduino) | Power the Bluetooth module |
GND | GND (Arduino) | Ground connection |
TXD | Pin 2 (SoftwareSerial) | Bluetooth TX (send data to Arduino) |
RXD | Pin 3 (SoftwareSerial) | Bluetooth RX (receive data from Arduino) |
3. DC Motors to L298 Motor Driver
Your motors will be connected to the OUT1, OUT2, OUT3, and OUT4 pins on the L298 motor driver:
L298 Pin | Motor Terminal | Motor 1 or Motor 2 |
---|---|---|
OUT1 | Motor 1 Terminal A | Motor 1 (Forward/Backward) |
OUT2 | Motor 1 Terminal B | Motor 1 (Forward/Backward) |
OUT3 | Motor 2 Terminal A | Motor 2 (Forward/Backward) |
OUT4 | Motor 2 Terminal B | Motor 2 (Forward/Backward) |
Note: Ensure that both motors are correctly connected to OUT1, OUT2, OUT3, and OUT4.
4. Power Supply
- Motors: Use a separate 3.7V Lithium-ion battery pack for powering the motors. Connect the positive terminal of the battery to the VCC pin of the L298 motor driver and the negative terminal to the GND pin of the L298.
- Arduino Nano: The Arduino Nano can be powered via USB from your PC or by connecting a separate 5V power supply to the 5V pin on the Arduino (if not using USB for power).
- Common Ground: Connect the GND of the Arduino, the GND of the L298, and the GND of the motor power supply to a common ground.
Summary of Connections:
Motor Driver (L298) to Arduino:
- IN1 (L298) → Pin 8 (Arduino)
- IN2 (L298) → Pin 9 (Arduino)
- ENA (L298) → Pin 10 (PWM, Arduino)
- IN3 (L298) → Pin 11 (Arduino)
- IN4 (L298) → Pin 12 (Arduino)
- ENB (L298) → Pin 13 (PWM, Arduino)
- 12v (L298) → Li-Ion Battery +ve
- GND (L298) → Li-Ion Battery -ve
Bluetooth HC-05 to Arduino Nano:
- VCC (HC-05) → 5V (Arduino)
- GND (HC-05) → GND (Arduino)
- TXD (HC-05) → Pin 2 (SoftwareSerial)
- RXD (HC-05) → Pin 3 (SoftwareSerial)
Motors to L298 Motor Driver:
- OUT1 (L298) → Motor 1 Terminal A
- OUT2 (L298) → Motor 1 Terminal B
- OUT3 (L298) → Motor 2 Terminal A
- OUT4 (L298) → Motor 2 Terminal B
Power Supply To Arduino:
- GND from (L298 Motor Driver) → Arduino GND
- 5V from (L298 Motor Driver) → Arduino VIN
Arduino Code:
#include <SoftwareSerial.h>
// Motor 1 pins (Motor A)
const int IN1 = 8;
const int IN2 = 9;
const int ENA = 10;
// Motor 2 pins (Motor B)
const int IN3 = 11;
const int IN4 = 12;
const int ENB = 13;
// Bluetooth module pins (RX, TX)
SoftwareSerial BTSerial(2, 3); // RX, TX (using SoftwareSerial)
void setup() {
// Initialize motor driver pins as outputs
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
// Initialize Bluetooth serial communication
BTSerial.begin(9600); // Set Bluetooth baud rate to 9600
Serial.begin(9600); // For debugging through Serial Monitor
// Start with motors stopped
stopMotors();
}
void loop() {
if (BTSerial.available()) {
char command = BTSerial.read(); // Read the Bluetooth command
// Print command to Serial Monitor for debugging
Serial.println(command);
// Handle different Bluetooth commands
if (command == 'F') {
// Move motors forward
moveForward();
}
else if (command == 'B') {
// Move motors backward
moveBackward();
}
else if (command == 'L') {
// Turn left
turnLeft();
}
else if (command == 'R') {
// Turn right
turnRight();
}
else if (command == 'S') {
// Stop motors
stopMotors();
}
}
}
// Move motors forward (both motors forward)
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 255); // Full speed
}
// Move motors backward (both motors backward)
void moveBackward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 255); // Full speed
}
// Turn left (Motor 1 moves forward, Motor 2 stops)
void turnLeft() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0); // Motor 2 stops
}
// Turn right (Motor 1 stops, Motor 2 moves forward)
void turnRight() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0); // Motor 1 stops
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 255); // Full speed
}
// Stop both motors
void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
Final Check:
- Motor driver power: Ensure the L298 motor driver is powered by the battery pack and not the Arduino’s 5V pin.
- Common Ground: Make sure all components (Arduino, motor driver, and Bluetooth module) share a common ground.
Once you’ve checked and double-checked these connections, your system should work as intended with the Bluetooth commands for forward, backward, left, right, and stop.