Motor not working

Hi :). I am creating an arduino bluetooth car. I am using the L298n h bridge and 2 9volt motor with a current draw of 70ma. For the bluetooth I used an hc 05 bluetooth module.
Problem:
I can control both motors if i program it to run together, I can also run motor b individually if I want to turn right, but I cannot control motor a individually so I cannot turn left.

I have connected everything properly. I connected "enA" and "In1, In2" pins for both motors on the digital pins of my arduino. I used 9v power supply for my l298n and the arduino has its own power via usb.

Looking forward to your reply. Thanks!

int enA = 8;
int In1 = 9;
int In2 = 10;
int enB = 11;
int In3 = 12;
int In4 = 13;
int data;

void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
pinMode(In3, OUTPUT);
pinMode(In4, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (Serial.available()>0){
    data = Serial.read();
}
switch(data){

case '1': //Forward
    digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
    digitalWrite(In3, HIGH);
    digitalWrite(In4, LOW);
    analogWrite(enA, 255);
    analogWrite(enB, 255);
    break;

case '2': //Left
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    digitalWrite(In3, HIGH);
    digitalWrite(In4, LOW);
    analogWrite(enA, 255);
    analogWrite(enB, 0);
    break;

case '3': //Right
     digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
    digitalWrite(In3, Low);
    digitalWrite(In4, LOW);
    analogWrite(enA, 0);
    analogWrite(enB, 255);
    break;

case '4': //Full Stop
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    digitalWrite(In3, LOW);
    digitalWrite(In4, LOW);
    analogWrite(enA, 0);
    analogWrite(enB, 0);
    break;

default:
break;
    }
}

All works except for left.

I suggest you move the bluetooth device to a SoftwareSerial port on two other pins so as to free up the HardwareSerial port for debug messages.

Then you could print the values that are being received. I suspect they are not what you think they are because I don't see a problem with the code for case '2'

...R

Thanks for the reply. So I need to add the software serial library to connect it to the other pins right? And just to verify, when the tx or rx led blinks it means the arduino recieved data right?

Are you using a Uno? If so then enA is on a non-PWM pin so analogWrite() won't work correctly. Try swapping enA to pin 9 and put in1 on pin 8.

But since you only ever use 0 and 255 that should not cause your current problem. As Robin2 says you need to check if whatever is on the other end of Bluetooth is actually sending what you think it should.

Steve

kyletheflipper:
Thanks for the reply. So I need to add the software serial library to connect it to the other pins right? And just to verify, when the tx or rx led blinks it means the arduino recieved data right?

I would not rely on those LEDs at all. In any case when you move to SoftwareSerial there are no tell-tale LEDs.

...R

slipstick:
Are you using a Uno? If so then enA is on a non-PWM pin so analogWrite() won't work correctly. Try swapping enA to pin 9 and put in1 on pin 8.

But since you only ever use 0 and 255 that should not cause your current problem. As Robin2 says you need to check if whatever is on the other end of Bluetooth is actually sending what you think it should.

Steve

Hi Thanks for the reply. I used mit app inventor for my app and when I check my serial monitor it displays the character that it should but it does not execute the command that its supposed to do when it recieves that character.

And also the motor is weak because I guess I only used 9 volts and the 2 motors are fighting for the same power supply. So can you reccomend me a voltage that is enough to power the motors stronger?

kyletheflipper:
And also the motor is weak because I guess I only used 9 volts and the 2 motors are fighting for the same power supply.

It is far more likely that the problem is a shortage of current. If you are using a mains to 9v adapter please tell us how much current it can produce.

If you are using batteries try a pack of 6 x AA alkaline cells (9v) or a pack of 8 x AA rechargeable NiMh cells (9.6v).

...R

Robin2:
If you are using batteries try a pack of 6 x AA alkaline cells (9v) or a pack of 8 x AA rechargeable NiMh cells (9.6v).

...R

I am using 6 AA batteries.

I did your advice to move the rx tx cables to another pin @Robin2. It still did not work. Actually it got worse, it was no longer recieving data. moved my tx rx pins to 2 and 3

//I just added this but its the same code.

#include<SoftwareSerial.h>
SoftwareSerial mySerial(2,3);


void setup() {
Serial.begin(9600);
//I added this one:
mySerial.begin(9600);

void loop() {
//instead of using serial.available I used mySerial
if(mySerial.available()>0){
    data = mySerial.read();
}
//then the same program

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.