This circuit is designed to control a pair of DC motors using an Arduino UNO microcontroller and an L298N DC motor driver. The system is capable of receiving commands via a Bluetooth HC-06 module to drive the motors forward or backward. A resistor is included in the circuit to interface between the Arduino and the Bluetooth module. The entire system is powered by a 12V battery.
#include <SoftwareSerial.h>;
SoftwareSerial BTSerial(3,4); // RX, TX
char incomingData;
int inn1 = 2; // Motor driver input 1
int inn2 = 4; // Motor driver input 2
int inn3 = 6; // Motor driver input 3
int inn4 = 8; // Motor driver input 4
void setup() {
// Initialize serial communications:
Serial.begin(9600);
BTSerial.begin(9600);
// Set motor driver pins as outputs:
pinMode(inn1, OUTPUT);
pinMode(inn2, OUTPUT);
pinMode(inn3, OUTPUT);
pinMode(inn4, OUTPUT);
}
void loop() {
// Check for Bluetooth data:
if(BTSerial.available() > 0){
incomingData = BTSerial.read();
if(incomingData == 'F'){ // Forward command
Forward();
}else if(incomingData == 'B'){ // Backward command
Backward();
}else{
Stop(); // Stop command
}
}
}
void Forward(){
digitalWrite(inn1, HIGH);
digitalWrite(inn3, HIGH);
digitalWrite(inn2, LOW);
digitalWrite(inn4, LOW);
}
void Backward(){
digitalWrite(inn1, LOW);
digitalWrite(inn3, LOW);
digitalWrite(inn2, HIGH);
digitalWrite(inn4, HIGH);
}
void Stop(){
digitalWrite(inn1, LOW);
digitalWrite(inn3, LOW);
digitalWrite(inn2, LOW);
digitalWrite(inn4, LOW);
}
Note: The code provided assumes that the incoming data from the Bluetooth module is a single character representing the command ('F' for Forward, 'B' for Backward, and any other character for Stop). The motor driver inputs are controlled accordingly to drive the motors in the desired direction.