Controlling DC motors direction with L293D issue

Greetings, I just bought this arduino and the L293D motor driver. I have quite a lot of trouble trying to controlling the turning. In fact, I should precise that the motor turn in one direction and doesn’t reverse, as I wish to.

Instead, the motor would vibrating if I drop the tension quickly. From what I understand, it is the Voltage dip phenomenon and therefore, I have read it is advised to put a delay. I did as some blogs suggest to.

Unfortunately, the motor does not reverse, it is trying to, but stop instead, as I could feel the shaft forcing itself to turn.

This is the code attach to it and the diagram.

A little extra information, if I were to change the reverse for forward and forward to reverse, this time, the motor would reverse and cannot go forward.

Thank you for you time,
regards

          //Var declaration
          int motorpin1 = 3;                  //define digital output pin no.
          
          int motorpin2 = 4;                  //define digital output pin no.
          int button= 6;
          unsigned int compteur=0;
          boolean condition_bouge=true;
          
          
          
          void setup () {
          
            pinMode(motorpin1,OUTPUT);        //set pin 3 as output
          
            pinMode(motorpin2,OUTPUT);        // set pin 4 as output
            pinMode(button,INPUT);  
                        }

//Procedure declaration
                  //The procedure that turns the shaft in one direction    
                  void forward() {
                        digitalWrite(motorpin1,HIGH);
                        digitalWrite(motorpin2,LOW);
                                }
                  //The procedure that turns the shaft in the opposite                                  
                  void reverse(){
                         digitalWrite(motorpin1,LOW);
                         digitalWrite(motorpin2,HIGH);                  
                                }
                  //Procedure that stop the motor from turning
                  void ForceStop(){
                         digitalWrite(motorpin1,LOW);
                         digitalWrite(motorpin2,LOW);                      
                                }
                  
                  
                  
                  
//Programme principale                 

void loop () {
//If no current is going through the switch then go reverse 
   if (digitalRead(button)!=HIGH){    
        delay(1000);    
        while (digitalRead(button)!=HIGH){
                reverse();
                                }
        ForceStop();
        delay(1000);        
                          }
          
  
//If current is going through the switch then turn forward  
   else if (digitalRead(button)==HIGH){   
     //else{
        delay(1000);     
        while (digitalRead(button)==HIGH){
                forward();
                                }
        ForceStop();
        delay(1000);        
                          }
          

             }

Firstly, I hope it's a mistake in your Fritzing and not real that you have the 293's ground pins 4 and 5 going to the 5v rail on the breadboard?

Secondly, have you checked that the motor actually works correctly in both directions by simply hooking it up to a battery of the correct voltage?

Edit... I missed this part 8) "change the reverse for forward and forward to reverse, this time, the motor would reverse and cannot go forward".... answers that question... seems the motor does actually work in both directions?

Last, it's not regarded as good practice to power equipment from the Arduino, as you are doing from Vin, because of current availability. But apart from that, you don't say what voltages you are working with- assuming Vin is high enough for the motor, it might not be once it's been through the 293 which (if I read the datasheet correctly) drops 1.2V or so. I don't think a delay has any effect on recovering a voltage drop. (But that doesn't explain why the motor works one way and not the other....)

Here's another thought... your input pin might not be going low, might be floating... I'm not 100% sure how that switch is wired. You need the pin pulled to ground if it's not deliberately pulled to 5v through the switch.

Take the other end of pin 6's wire directly to 5v or ground as a temporary test, bypassing the switch.

JimboZA:
Firstly, I hope it's a mistake in your Fritzing and not real that you have the 293's ground pins 4 and 5 going to the 5v rail on the breadboard?

Secondly, have you checked that the motor actually works correctly in both directions by simply hooking it up to a battery of the correct voltage?

Last, it's not regarded as good practice to power equipment from the Arduino, as you are doing from Vin, because of current availability. But apart from that, you don't say what voltages you are working with- assuming Vin is high enough for the motor, it might not be once it's been through the 293 which (if I read the datasheet correctly) drops 1.2V or so. I don't think a delay has any effect on recovering a voltage drop. (But that doesn't explain why the motor works one way and not the other....)

Hello!

Yes, it was my mistake on Fritzing. Thanks for noticing me :slight_smile:

I just tested as I am writting, and the 3V motor turns in both direction with a 2 battery AA.

And to verify if it works with the arduino basic code, it works as well with the USB power source.

void loop (){
      forward();
               }

//or
//void loop() {
//      reverse();
//                }

To be honest, I used the following link as reference and I wasn't expecting about the Vin issue.

Have you tried my suggestion of by-passing the switch by taking pin6's wire to 5v or ground directly?

JimboZA:
Here's another thought... your input pin might not be going low, might be floating... I'm not 100% sure how that switch is wired. You need the pin pulled to ground if it's not deliberately pulled to 5v through the switch.

Take the other end of pin 6's wire directly to 5v or ground as a temporary test, bypassing the switch.

I removed the switch, and simulate it by connecting/unplugging the wire. When connecting, it does run in direction, however it doesn't turn in the opposite direction when I pulled off. It is indeed strange.

Edit: Yup, I did what you suggest me to do.

Edit: Yup, I did what you suggest me to do.

Just to clarify... you did ground it to 0v, not just left it floating?

If so, then maybe the 293 is faulty? With the motors disconnected do you get a reversal of voltage at the 293 outputs?

Maybe try the other side of the 293? (Pins 10/11 and 14/15)

JimboZA:

Edit: Yup, I did what you suggest me to do.

Just to clarify... you did ground it to 0v, not just left it floating?

If so, then maybe the 293 is faulty? With the motors disconnected do you get a reversal of voltage at the 293 outputs?

Maybe try the other side of the 293? (Pins 10/11 and 14/15)

Sorry, my litteracy might failing me with your last post. If I understand you correctly, you mean either there's no current going to the input or do going right?

In that case, yes.

I will try with the other pins from the 293. :slight_smile:

EDIT: Quick update. I just rotate the 293 so the output used is the 3rd and 4th, the problem remains the same as the first topic. However, it seems that the motor is consuming much more current, as it makes a way higher noise when turning.

Ok I just tested you code and setup, with two minor differences…

  1. I didn’t want to power the motor from Vin so I hooked 293 pin 8 (Vcc) to my external supply, and connected the supply ground to the Arduin / breadboard ground

  2. I didn’t use a switch… I just put the other end of pin6’s wire to 5v rail or to 0v rail to simulate the switch.

Everything works perfectly… motor goes one way when pin6 wire is stuck into 5v, and the other way when it’s at 0v.

BUT… when the pin6 wire isn’t plugged into either 5v or 0v, ie floating, I can feel the motor trying to move from time to time… kind of “ticking”… so I suspect your problem is the wiring of the switch. Check the reference page for using the internal pullup and wire the switch to ground. That way you’ll have definite 5v or 0v, not a possible ???V

JimboZA:
Ok I just tested you code and setup, with two minor differences…

  1. I didn’t want to power the motor from Vin so I hooked 293 pin 8 (Vcc) to my external supply, and connected the supply ground to the Arduin / breadboard ground

  2. I didn’t use a switch… I just put the other end of pin6’s wire to 5v rail or to 0v rail to simulate the switch.

Everything works perfectly… motor goes one way when pin6 wire is stuck into 5v, and the other way when it’s at 0v.

BUT… when the pin6 wire isn’t plugged into either 5v or 0v, ie floating, I can feel the motor trying to move from time to time… kind of “ticking”… so I suspect your problem is the wiring of the switch. Check the reference page for using the internal pullup and wire the switch to ground. That way you’ll have definite 5v or 0v, not a possible ???V

Is it too much to ask, if you can link a photo/image ? Sometime, I might not find the obvious problem :o
Thanks for the help :slight_smile:

if you can link a photo/image

Yeah I’m just going to make a Fritz of it.

I just tested it with a push switch on pin6, changing your code from pinMode(button,INPUT); to pinMode(button,INPUT_PULLUP); so pin 6 is high when button is not pushed and goes low when pushed… works correctly.

Stand by for a Fritz of my set up… I need to get coffee though 8)

JimboZA:

if you can link a photo/image

Yeah I'm just going to make a Fritz of it.

I just tested it with a push switch on pin6, changing your code from pinMode(button,INPUT); to pinMode(button,INPUT_PULLUP); so pin 6 is high when button is not pushed and goes low when pushed... works correctly.

Stand by for a Fritz of my set up... I need to get coffee though 8)

Cheers! :slight_smile:

Right, here it is…

Essentially same as yours with differences already discussed which are:

  • 293’s Vcc comes from external source to drive the motor
  • Switch is wired to pull pin6 down to ground when pushed
  • Pin6 is configured in code as pinMode(button,INPUT_PULLUP); so it’s high until the button is pressed

Thanks, I'll see if I can fix it tomorrow morning. Going to bed since it's 3:00 AM .

Have a good night!

Minor error in my Fritzing which was correct in real life but not shown on pic: 293 pin 16 (Vcc1) must be to 5v, but yours is like that anyway.

Hi jimboz, I would like to update my situation.

It works, you were right about the pullup. I only used a 1k resistor instead of a resistor that use something higher. Now I use 10k ohms and it works like a charms :P!!!!

Good stuff....

Jim

I know this is an old post/solution - but @JimboZA is a lifesaver! The jumper from 16 -> 1 (5V control) was the key fix for my reversing issue. Still not 100% clear on why this allows the motor to properly reverse, but it works great!