My L298N motor driver makes a buzzing sound when I press the turn 'left' command on my bluetooth app. Both motors work fine and they can go forward, backward, and right.
Here is my code:
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3; // servo name
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
int motorOne = 4;
int motorOne2 = 5;
int motorTwo = 6;
int motorTwo2 = 7;
int enA = 3;
int enB = 2;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
//initial motors pin
char command;
char Value;
void setup()
{
myservo1.attach(2); // attach servo signal wire to pin 9
myservo2.attach(8);
myservo3.attach(12);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
while (bluetooth.available() > 2) {
Value = bluetooth.read();
Serial.println(Value);
}
if ( Value == 'F') {
// Robo Pet Run Forward
digitalWrite(enA, 255);
digitalWrite(enB, 255);
digitalWrite(motorOne, HIGH);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, HIGH);
digitalWrite(motorTwo2, LOW);
} else if (Value == 'B') {
//Robo Pet Run Backward
digitalWrite(enA, 255);
digitalWrite(enB, 255);
digitalWrite(motorOne, LOW);
digitalWrite(motorOne2, HIGH);
digitalWrite(motorTwo, LOW);
digitalWrite(motorTwo2, HIGH);
} else if (Value == 'L') {
//Robo Pet Turn Left
digitalWrite(enA, 255);
digitalWrite(motorOne, LOW);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, HIGH);
digitalWrite(motorTwo2, LOW);
} else if (Value == 'R') {
//Robo Pet Turn Right
digitalWrite(enB, 255);
digitalWrite(motorOne, HIGH);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, LOW);
digitalWrite(motorTwo2, LOW);
} else if (Value == 'S') {
//Robo Pet Stop
digitalWrite(motorOne, LOW);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, LOW);
digitalWrite(motorTwo2, LOW);
}
//Read from bluetooth and write to usb serial
if(bluetooth.available()>= 2 )
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
if (realservo >= 1000 && realservo <1180) {
int servo1 = realservo;
servo1 = map(servo1, 1000, 1180, 0, 180);
myservo1.write(servo1);
Serial.println("Servo 1 ON");
delay(10);
}
if (realservo >= 2000 && realservo <2180) {
int servo2 = realservo;
servo2 = map(servo2, 2000, 2180, 0, 180);
myservo2.write(servo2);
Serial.println("Servo 2 ON");
delay(10);
}
if (realservo >= 3000 && realservo <3180) {
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180, 0, 180);
myservo3.write(servo3);
Serial.println("Servo 3 ON");
delay(10);
}
}
}
My materials:
- Arduino Nano
- HC-05 blutooth module 6-pin
- L298N motor driver
- 2 DC bo gear motors (2WD kit)
- Turnigy 7.4v lipo 2200mAh (motors)
- 3.7v lipo 1000mAh (Nano & HC-05)
The app used to control the robot:
Any help & tips would be very much appreciated.
Thank you!