0

Hello I have an RC car which has two 3v motors (one for left/right and the other one for forward/back). The left and right motor is working fine but when I try to rotate the other motor it rotates only back. I've tried the motor separately and it works in both direction without the controller.

My code is the following:

int enablePinMotorAF = 3;
int in1PinMotorAF = 5;
int in2PinMotorAF = 6;
int enablePinMotorLR = 11;
int in1PinMotorLR = 10;
int in2PinMotorLR = 9;
boolean reverse = true;

void setup() {
  pinMode(enablePinMotorAF, OUTPUT);
  pinMode(in1PinMotorAF, OUTPUT);
  pinMode(in2PinMotorAF, OUTPUT);
  pinMode(enablePinMotorLR, OUTPUT);
  pinMode(in1PinMotorLR, OUTPUT);
  pinMode(in2PinMotorLR, OUTPUT);
}

void loop() {
  //go forward  ->not working
  analogWrite(enablePinMotorAF, 230);  //max speed
  digitalWrite(in1PinMotorAF, reverse);
  digitalWrite(in2PinMotorAF, !reverse);
  delay(3000);
  //go back -> working
  analogWrite(enablePinMotorAF, 230); //max speed
  digitalWrite(in1PinMotorAF, !reverse);
  digitalWrite(in2PinMotorAF, reverse);
  delay(3000);
  //go right -> working
  analogWrite(enablePinMotorLR, 230); //max speed
  digitalWrite(in1PinMotorLR, !reverse);
  digitalWrite(in2PinMotorLR, reverse);
  delay(3000);
  //go left  -> working
  analogWrite(enablePinMotorLR, 230); //max speed
  digitalWrite(in1PinMotorLR, reverse);
  digitalWrite(in2PinMotorLR, !reverse);
  delay(3000);
}

Here is the wiring too:

Wiring

The green and orange wires are for a Bluetooth module.

Do you have any idea how can I solve this problem and to make it work?

Thank you.

dda
  • 6,030
  • 2
  • 25
  • 34
Hunor Gocz
  • 137
  • 12

1 Answers1

0

To reverse the motors, you need four pins, two for each motor. On a readily-available L293 module, they are often labeled IN1, IN2, IN3, and IN4.

To make one motor go forward, you might set IN1 to 5V and IN2 to 0V. To reverse it, simply switch the inputs, IN1 to 0V and IN2 to 5V. In this case 5V is a digitalWrite(pin, HIGH).

Similar for the other two pins for the other motor. I start my answer with this because the wiring of what output pins to what input pins is vitally important.

The Enable pins is where you've gone wrong, it seems. Enable2 and Enable1 should be connected to the pins to which you're doing the analogWrite() but enablePinMotorAF = 3 for example connects to a motor signal input, not to Enable2 as it probably should. Start with fixing that... your two pins 3 and 11 should be connected to Enable1 and Enable2. You only need PWM on the Enable pins. The others should simply be activated with digitalWrite().

Once you get the Enable n pins connected to PWM, then you'll have a good PWM enable signal. Simply connect the other pins on the same side of the chip (IN1 and IN2 for Enable1 and IN3 and IN4 for Enable2), and turn them on and off with `digitalWrite(pin, HIGH) and you'll be good to go.

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • The problem was that I had to swap pin 6 with pin 3 because they were declared wrong. Now the direction is ok, it works but I after some test it just rotates very slowly and the left/right motor is just shaking. I tried with a new battery and I had the same result. So you mean @JLH that I should connect the 4 IN pins to non PWM ports? Can that be the problem? – Hunor Gocz Jul 04 '17 at 09:45
  • No. Simply this: the two pins, whatever they are, that do analogWrites, they need to be connected to the Enable pins on L293. The other pins doesn't matter what they're hooked to on the Arduino, so long as you can do digitalWrites() to turn them off and on (to go forward and reverse). – TomServo Jul 04 '17 at 09:49
  • Now I have like this: the two Enable pins are connected to 6, 11 (PWM ports) and I do analogwrite to change speed and the other ones (IN1, IN2, IN3, IN4) are connected to other PWM ports and I do digitalWrite to change the direction. But they still move very slowly. Why is that? – Hunor Gocz Jul 04 '17 at 10:05
  • Do they go left, right, forward, and reverse properly? – TomServo Jul 04 '17 at 10:10
  • No. Now the left/right is just shaking and the forward/reverse rotates very slowly. Sorry I missunderstood your question, so yes they go to the good direction but the speed is not enough. – Hunor Gocz Jul 04 '17 at 10:12
  • @HunorGocz Sorry I don't think I can help you then, I've exhausted what I can do from here. – TomServo Jul 04 '17 at 10:15
  • Thank you anyway. Do you think that my L293D might damaged or something? I tried it again and now it goes forward a bit faster but the other diresctions are still not good. – Hunor Gocz Jul 04 '17 at 10:21
  • @HunorGocz Could be. I got a six of them for $USD30 and two were bad, cheap chinese imports. So that could be it. Can you accept and/upvote my answer to acknowledge the effort I've spent working with you? And do try to get a full 293 module, not just the chip. These things put out a lot of heat, the modules are better. – TomServo Jul 04 '17 at 10:28
  • I'll try to buy a new one. I just need to finish this until friday so it's not so good.. Thank you for your help. – Hunor Gocz Jul 04 '17 at 10:32