Hello, I'm trying to build a car using an Arduino Uno, an L298N motor driver board, and an HC-05 Bluetooth module, but no matter what I do, I can't get the motors to turn. All the motors are working fine individually. I'm sharing the wiring diagram and code below.
// L298N Connection
const int motorA1 = 5; // L298N's IN3 Input
const int motorA2 = 6; // L298N's IN1 Input
const int motorB1 = 10; // L298N's IN2 Input
const int motorB2 = 9; // L298N's IN4 Input
int i = 0; // A random variable assigned for loops
int j = 0; // A random variable assigned for loops
int state; // Variable for the signal coming from the Bluetooth device
int vSpeed = 255; // Standard speed, can be a value between 0-255
void setup() {
// Define our pins
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
// Open a serial port at 9600 baud
Serial.begin(9600);
}
void loop() {
/* Stop the car when the Bluetooth connection is lost or disconnected.
(To activate, remove the "//" from the following line.) */
// if(digitalRead(BTState) == LOW) { state = 'S'; }
// Save the incoming data to the 'state' variable
if(Serial.available() > 0) {
state = Serial.read();
}
/* 4 speed levels adjustable from the app (values should be between 0-255) */
if (state == '0') {
vSpeed = 0;
} else if (state == '1') {
vSpeed = 100;
} else if (state == '2') {
vSpeed = 180;
} else if (state == '3') {
vSpeed = 200;
} else if (state == '4') {
vSpeed = 255;
}
/***********************Forward****************************/
// If incoming data is 'F', the car moves forward.
if (state == 'F') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, vSpeed); analogWrite(motorB2, 0);
}
/**********************Forward Left************************/
// If incoming data is 'G', the car moves forward left (diagonal).
else if (state == 'G') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 100); analogWrite(motorB2, 0);
}
/**********************Forward Right************************/
// If incoming data is 'I', the car moves forward right (diagonal).
else if (state == 'I') {
analogWrite(motorA1, 100); analogWrite(motorA2, 0);
analogWrite(motorB1, vSpeed); analogWrite(motorB2, 0);
}
/***********************Backward****************************/
// If incoming data is 'B', the car moves backward.
if (state == 'B') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed);
}
/**********************Backward Left************************/
// If incoming data is 'H', the car moves backward left (diagonal).
else if (state == 'H') {
analogWrite(motorA1, 0); analogWrite(motorA2, 100);
analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed);
}
/**********************Backward Right************************/
// If incoming data is 'J', the car moves backward right (diagonal).
else if (state == 'J') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 100);
}
/***************************Left*****************************/
// If incoming data is 'L', the car turns left.
else if (state == 'L') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 150);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/***************************Right*****************************/
// If incoming data is 'R', the car turns right.
else if (state == 'R') {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, vSpeed); analogWrite(motorB2, 150);
}
/************************Stop*****************************/
// If incoming data is 'S', the car stops.
else if (state == 'S') {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
}