Arduino and Bluetooth Controlled DC Motor Robot with L298N Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control four DC motors using an Arduino 101 microcontroller, an L298N DC motor driver, and an HC-05 Bluetooth module. The motors are powered by a combination of 3.7V batteries and a 9V battery. The Bluetooth module allows for wireless control of the motors via commands sent from a paired device.
Component List
Arduino 101
- Description: Microcontroller board based on the Intel Curie module.
- Pins: A5/SCL, A4/SDA, AREF, GND, D13/SCK, D12/MISO, D11 PWM/MOSI, D10 PWM/SS, D9 PWM, D8, D7, D6 PWM, D5 PWM, D4, D3 PWM, D2, D1/TX, D0/RX, AIN, ioref, RESET, 3V3, 5V, VIN, A0, A1, A2, A3, ICSP MISO, ICSP SCK, ICSP MOSI
L298N DC Motor Driver
- Description: Dual H-Bridge motor driver for controlling two DC motors.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
HC-05 Bluetooth Module
- Description: Bluetooth module for wireless communication.
- Pins: Key, VCC, GND, TXD, RXD, State
DC Motor
- Description: Standard DC motor.
- Pins: pin 1, pin 2
9V Battery
- Description: Standard 9V battery for powering the Arduino.
- Pins: -, +
3.7V Battery
- Description: Standard 3.7V battery for powering the motor driver.
- Pins: +, -
Wiring Details
Arduino 101
- GND: Connected to the GND pins of the L298N DC motor driver, HC-05 Bluetooth module, and the negative terminals of the 3.7V batteries and 9V battery.
- D10 PWM/SS: Connected to the ENB pin of the L298N DC motor driver.
- D9 PWM: Connected to the ENA pin of the L298N DC motor driver.
- D8: Connected to the IN1 pin of the L298N DC motor driver.
- D7: Connected to the IN2 pin of the L298N DC motor driver.
- D6 PWM: Connected to the IN3 pin of the L298N DC motor driver.
- D5 PWM: Connected to the IN4 pin of the L298N DC motor driver.
- D1/TX: Connected to the RXD pin of the HC-05 Bluetooth module.
- D0/RX: Connected to the TXD pin of the HC-05 Bluetooth module.
- 3V3: Connected to the VCC pin of the HC-05 Bluetooth module.
- 5V: Connected to the 5V pin of the L298N DC motor driver.
- VIN: Connected to the positive terminal of the 9V battery.
L298N DC Motor Driver
- GND: Connected to the GND pin of the Arduino 101 and the negative terminals of the 3.7V batteries.
- ENB: Connected to the D10 PWM/SS pin of the Arduino 101.
- ENA: Connected to the D9 PWM pin of the Arduino 101.
- IN1: Connected to the D8 pin of the Arduino 101.
- IN2: Connected to the D7 pin of the Arduino 101.
- IN3: Connected to the D6 PWM pin of the Arduino 101.
- IN4: Connected to the D5 PWM pin of the Arduino 101.
- OUT1: Connected to pin 1 of two DC motors.
- OUT2: Connected to pin 2 of two DC motors.
- OUT3: Connected to pin 2 of two DC motors.
- OUT4: Connected to pin 1 of two DC motors.
- 12V: Connected to the positive terminals of the 3.7V batteries.
- 5V: Connected to the 5V pin of the Arduino 101.
HC-05 Bluetooth Module
- RXD: Connected to the D1/TX pin of the Arduino 101.
- TXD: Connected to the D0/RX pin of the Arduino 101.
- VCC: Connected to the 3V3 pin of the Arduino 101.
- GND: Connected to the GND pin of the Arduino 101.
DC Motors
- Motor 1 (pin 1): Connected to OUT1 of the L298N DC motor driver.
- Motor 1 (pin 2): Connected to OUT2 of the L298N DC motor driver.
- Motor 2 (pin 1): Connected to OUT1 of the L298N DC motor driver.
- Motor 2 (pin 2): Connected to OUT2 of the L298N DC motor driver.
- Motor 3 (pin 1): Connected to OUT4 of the L298N DC motor driver.
- Motor 3 (pin 2): Connected to OUT3 of the L298N DC motor driver.
- Motor 4 (pin 1): Connected to OUT4 of the L298N DC motor driver.
- Motor 4 (pin 2): Connected to OUT3 of the L298N DC motor driver.
9V Battery
- -: Connected to the GND pin of the Arduino 101.
- +: Connected to the VIN pin of the Arduino 101.
3.7V Batteries
- -: Connected to the GND pin of the Arduino 101 and the L298N DC motor driver.
- +: Connected to the 12V pin of the L298N DC motor driver.
Documented Code
#include <SoftwareSerial.h>
// Pin definitions for L298N motor driver
const int ENA = 9; // PWM pin for motor A
const int IN1 = 8; // Control pin 1 for motor A
const int IN2 = 7; // Control pin 2 for motor A
const int ENB = 10; // PWM pin for motor B
const int IN3 = 6; // Control pin 1 for motor B
const int IN4 = 5; // Control pin 2 for motor B
// Bluetooth module pins
const int BT_RX = 2; // Bluetooth module TX to Arduino RX
const int BT_TX = 3; // Bluetooth module RX to Arduino TX
SoftwareSerial bluetooth(BT_RX, BT_TX);
void setup() {
// Initialize motor control pins as outputs
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Initialize serial communication for Bluetooth
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
switch (command) {
case 'F': // Move forward
moveForward();
break;
case 'B': // Move backward
moveBackward();
break;
case 'L': // Turn left
turnLeft();
break;
case 'R': // Turn right
turnRight();
break;
case 'S': // Stop
stopMotors();
break;
default:
// Unknown command
break;
}
}
}
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 255); // Full speed
}
void moveBackward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 255); // Full speed
}
void turnLeft() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 255); // Full speed
}
void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 255); // Full speed
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN