DC motor controlled with H-Bridge L293DNE never stops nor changes speed.[SOLVED]

Hello,

I am trying to actuate the project 10 of the Arduino book included in the starter kit, it should simply connect and control a DC motor using a H-Bridge L293DNE. The project includes a switch for activating the motor, a switch to change direction, and a potentiometer to adjust speed.
I think everything has been connected finely, at least it is identical to what is shown in the book, but the motor never stops running!

It is constantly on and even the speed cannot be changed. Only the direction of the rotation can be succesfully changed.

Do you think it is possible that the H-Bridge is broken, ore maybe there is something simple to fix which I cannot see?

Thanks in advance for your help, here is the code:

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin = 4;
const int onOffSwitchStateSwitchPin = 5;
const int potPin = A0;

int onOffSwitchState = 0;
int previousOnOffSwitchState = 0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;

int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;

void setup() {
  pinMode(directionSwitchPin, INPUT);
  pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);


  analogWrite(enablePin, 0);
}

void loop() {
  onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  delay(1);
  directionSwitchState = digitalRead(directionSwitchPin);
  motorSpeed = analogRead(potPin)/4;

  if(onOffSwitchState != previousOnOffSwitchState) {
    if(onOffSwitchState == HIGH) {
      motorEnabled = !motorEnabled;
    }
  }

  if(directionSwitchState != previousDirectionSwitchState) {
    if(directionSwitchState == HIGH) {
      motorDirection = !motorDirection;
    }
  }

  if(motorDirection == 1) {
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
  }
  else {
    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, HIGH);
  }

  if (motorEnabled == 1) {
    analogWrite(enablePin, motorSpeed);
  }
  else {
    analogWrite(enablePin, 0);
  }

  previousDirectionSwitchState = directionSwitchState;
  previousOnOffSwitchState = onOffSwitchState;

}

This may or may not be the issue, but its a glaring "gotcha" in the code.

The ! operator negates a boolean value, thereafter you should treat it
as boolean. Booleans in C are either 0 (false) or not 0 (true).

      motorDirection = !motorDirection;  // motorDirection could be any value
    }
  }

  if(motorDirection == 1) {  // but here it is compared to 1.

The correct way is:

  if (motorDirection) {  // test boolean

You say the motor is constantly on, do you mean after the first activation button press, or does it not wait for that? If the button isn't doing anything at all then are you sure your code is setup for the right input pin?

p.s. You shouldn't be using a pot to control motor speed. Doing it like that means that excess energy is being dumped in the pot, and with the high currents that a motor can potentially draw, there's a good chance of burning out the pot. You should control the speed of the motor using a PWM. By that I don't necessarily mean one of the PWM outs, you can do it yourself in your loop. E.g. turning the motor on for 10us (note microsecs, not millis) then off again for 10us equates to a 50% duty cycle which the motor "sees" as half the supply voltage. You can keep the pot if you need a manual speed setting, but connect it to an analog input pin, not directly to the motor. Better still, just store a speed setting in EEPROM.

MarkT:
The ! operator negates a boolean value, thereafter you should treat it
as boolean.

The code seems ok to me. !x = (x==0 ? 1 : 0). So regardless of what x was on input, it will be 0 or 1 after. Elegance would require comparisons with TRUE and FALSE constants however, rather than magic literals.

DonMilne:
You say the motor is constantly on, do you mean after the first activation button press, or does it not wait for that? If the button isn't doing anything at all then are you sure your code is setup for the right input pin?

p.s. You shouldn't be using a pot to control motor speed. Doing it like that means that excess energy is being dumped in the pot, and with the high currents that a motor can potentially draw, there's a good chance of burning out the pot. You should control the speed of the motor using a PWM. By that I don't necessarily mean one of the PWM outs, you can do it yourself in your loop. E.g. turning the motor on for 10us (note microsecs, not millis) then off again for 10us equates to a 50% duty cycle which the motor "sees" as half the supply voltage. You can keep the pot if you need a manual speed setting, but connect it to an analog input pin, not directly to the motor. Better still, just store a speed setting in EEPROM.

The motor is on without waiting the button to be pressed, as soon as the program is uploaded to arduino the motor starts to run.

I checked the pins, and they are right, the problem is that the motor in the setup should be setup to be off, whilst it start immediately running :frowning:

And, yes, pot is not connected to the motor and the speed of the motor is controlled with PWM, at least I think so, the codeline is the following:

    analogWrite(enablePin, motorSpeed);

(I edited the code above because I realized I posted a modified version where this line was wrong)

If I was you I would take a few steps back.

You have one suspect button input. I would change your code so that you flash the main Arduino LED when this button is pressed: eliminate the h-bridge from the equation, prove that your code is receiving this input.

You have three control pins going to the L293. I would attach LEDs to each of these instead (to ground with a 220R current limiting resistor of course), so you can see the signals you're sending to the H-bridge.

Finally, if all that is proved, then note you can also drive LEDs with outputs 1 and 2 (and 3 and 4) of the H-bridge. This proves that the H-bridge is working (remember that the outputs have their own +V supply, pick an LED resistor appropriately).

Another thing you can check is what values you're getting from the motorspeed pot. Make sure they're suitable for use as PWM output values. I would have expected some kind of calculation. You do have "voltage/4", but is that correct for your motor and supply voltage?

I would suggest you post a clear picture of you wiring. Keep the picture under 1000 pixels wide.

So, finally I rebuilt all the connections, first H-bridge and motor only, then I added the on/off switch, them the direction switch and finaly the potentiometer to regulate the speed.
In parallel I rebuilt the code, and in the end it happened to be working, so maybe there was some loose connection or don't know what.
The good new is that the h-bridge is not broken and that everything is workin :slight_smile:

Thanks a lot for your help and happy new year!