This circuit is designed to control a pair of DC motors using an L298N motor driver module, with an Arduino UNO as the central microcontroller. The system also includes an HC-05 Bluetooth module for wireless communication, allowing remote control of the motors. The motors are powered by a 12V battery, which also supplies power to the L298N module. The Arduino UNO is powered by the 5V output from the L298N module. The HC-05 Bluetooth module interfaces with the Arduino UNO for receiving commands to control the motors' direction and speed.
#include <SoftwareSerial.h>
// Define motor pins
const int motor1Pin1 = 2;
const int motor1Pin2 = 3;
const int motor2Pin1 = 4;
const int motor2Pin2 = 5;
// Define Bluetooth module pins
const int rxPin = 10;
const int txPin = 11;
// Create a SoftwareSerial object for Bluetooth communication
SoftwareSerial bluetooth(rxPin, txPin);
void setup() {
// Initialize serial communication
Serial.begin(9600);
bluetooth.begin(9600);
// Set motor pins as output
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop() {
if (bluetooth.available() > 0) {
char command = bluetooth.read();
switch (command) {
case 'F': // Forward
forward();
break;
case 'B': // Backward
backward();
break;
case 'L': // Left
left();
break;
case 'R': // Right
right();
break;
case 'S': // Stop
stop();
break;
}
}
}
// Function to move the car forward
void forward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
// Function to move the car backward
void backward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
// Function to turn the car left
void left() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
// Function to turn the car right
void right() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
// Function to stop the car
void stop() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the Bluetooth communication and sets up the motor control pins. The loop function listens for commands from the Bluetooth module and calls the appropriate function to control the motors. The functions forward()
, backward()
, left()
, right()
, and stop()
control the motors' movements.