H-Bridge with Dim LED when digital output set LOW

Modified Project 10 (Zoetrope) from the Arduino Project Book that came with the Arduino Starter Kit
Arduino Uno R3
I am using h-bridge (L293D MYS 99 728) with 20K pot
Switch 0 = on / off
Switch 1 = clockwise / counter-clockwise motor rotation
5V via Arduino using USB
9V battery on motor power side
Ground from Arduino ground to motor side ground connected
Resistors are 10K's on the switches, and 220's on the LEDs.; resistors are all on the ground side

Mechanically it works fine. Pot can regulate power low to high and momentary switches turn motor on / off and change rotation as it should.

So decided to add status lights.

Used red / green for on / off
Used blue / yellow for spin clockwise / spin counter-clockwise

Problem is that the blue / yellow will light dimly when motor is off and that specific direction is selected

When motor is switched on, the blue or yellow leds will light up bright (as they should) when that direction is selected. It is only when the blue or yellow led is in its "off" mode that the led continues to burn very low but not go off AND that direction is selected. The LED goes total dark (desired) when that direction is NOT selected.

My sketch is attached (let me know if I should add it differently...happy to).

Also attached two images of my wire-up...can add additional images at request.

I just a beginner so I ok with constructive feedback about stuff I should know that may be obvious to others...

Thanks in advance.
PK

Zoetrope.ino (3.22 KB)

1 Like

Update:
I tried a few things and learned a bit more about what is going on...

When the uno is active and the LED is dim and the motor is not yet turned on via the switch, there is a low negative voltage between the LED poles. Both Blue and Yellow LEDs have a small voltage of -0.06V and -0.025V respectively.

I moved the LEDs to the analog pins and tested all again and got the exact same results. Also tested the LED pins when not using the LEDs (LEDs on some other pins) and do NOT see a voltage (0V).

Also tried different LEDs and get same results.

Another update: swapped out the h-bridge for a TI h-bridge and tested. Exact same results. All works great but the selected direction shows a dim LED when I would expect it to be dark because the motor is off. Seems to be a code thing and not a hardware thing, but I have no clue at this point.


![|500x281](Arduino Forum?
action=dlattach;topic=671872.0;attach=352039)
Spaghetti! No way we can check that wiring for you. Can you post a schematic please? Neatly hand-drawn is fine.

Also please post your code as described in the forum guide. Can't read a .ino on our phones/tablets.

Thanks guys. Here is the code:

const int control0Pin1 = 2;
const int control0Pin2 = 3;
const int enable0Pin = 9;
const int directionSwitch0Pin = 4;
const int onOffSwitch1StateSwitch1Pin = 5;
const int pot0Pin = A0;

const int redLEDPin = 12;
const int greenLEDPin = 7;
const int blueLEDPin = 8;
// const int blueLEDPin = A1;
const int yellowLEDPin = 10;
// const int yellowLEDPin = A2;

int onOffSwitch1State = 0;
int previousOnOffSwitch1State = 0;
int directionSwitch0State = 0;
int previousDirectionSwitch0State = 0;

int motor0Enabled = 0;
int motor0Speed = 0;
int motor0Direction = 1;

void setup() {
 pinMode(directionSwitch0Pin, INPUT);
 pinMode(onOffSwitch1StateSwitch1Pin, INPUT);
 pinMode(control0Pin1, OUTPUT);
 pinMode(control0Pin2, OUTPUT);
 pinMode(enable0Pin, OUTPUT);
 digitalWrite(enable0Pin, LOW);

 pinMode(redLEDPin, OUTPUT);
 pinMode(greenLEDPin, OUTPUT);
 pinMode(blueLEDPin, OUTPUT);
 pinMode(yellowLEDPin, OUTPUT);

}

void loop() {
 onOffSwitch1State = digitalRead(onOffSwitch1StateSwitch1Pin);
 delay(1);
 directionSwitch0State = digitalRead(directionSwitch0Pin);
 motor0Speed = analogRead(pot0Pin)/4;

 if(onOffSwitch1State != previousOnOffSwitch1State){
   if(onOffSwitch1State == HIGH){
     motor0Enabled = !motor0Enabled;
     digitalWrite(greenLEDPin, HIGH); // motor is on
     digitalWrite(redLEDPin, LOW); // motor is not off
   }
 }
 if(directionSwitch0State != previousDirectionSwitch0State){
   if(directionSwitch0State == HIGH){
     motor0Direction = !motor0Direction;
   }
 }

 if(motor0Direction == 1){
   digitalWrite(control0Pin1, HIGH);
   digitalWrite(control0Pin2, LOW);
   digitalWrite(blueLEDPin, HIGH); // motor is spinning in clockwise direction (default)
   // analogWrite(blueLEDPin, 255); // motor is spinning in clockwise direction (default)
   digitalWrite(yellowLEDPin, LOW); // motor is not spinning in counter-clockwise direction
   // analogWrite(yellowLEDPin, 0); // motor is not spinning in counter-clockwise direction
 }
 else {
   digitalWrite(control0Pin1, LOW);
   digitalWrite(control0Pin2, HIGH);
   digitalWrite(blueLEDPin, LOW); // motor is not spinning in clockwise direction (default)
   // analogWrite(blueLEDPin, 0); // motor is not spinning in clockwise direction (default)
   digitalWrite(yellowLEDPin, HIGH); // motor is spinning in counter-clockwise direction
   // analogWrite(yellowLEDPin, 255); // motor is spinning in counter-clockwise direction
 }

 if(motor0Enabled == 1){
   analogWrite(enable0Pin, motor0Speed);
 }
 else {
   // start in the off mode with green LED off, red LED on, blue LED off, and yellow LED off
   analogWrite(enable0Pin, 0);
   digitalWrite(greenLEDPin, LOW);
   digitalWrite(redLEDPin, HIGH);
   digitalWrite(blueLEDPin, LOW); // motor is not spinning in clockwise direction (default)
   // analogWrite(blueLEDPin, 0); // motor is not spinning in clockwise direction (default)
   digitalWrite(yellowLEDPin, LOW); // motor is not spinning in counter-clockwise direction
   // analogWrite(yellowLEDPin, 0); // motor is not spinning in counter-clockwise direction
 }
 
 previousDirectionSwitch0State = directionSwitch0State;
 previousOnOffSwitch1State = onOffSwitch1State;
}

Spaghetti indeed. So will send the schematic from the book. Only difference are the 4 LEDs I added. I will post an image where they were added as well, as best I can. Working it now.

Attached is the basic diagram. I am working on annotating where the LEDs are added. They are on their own pins in basic config with 220 resistors.

Please find attached the schematic with the added LEDs. BTW, anyone know of a free schematic tool :frowning: ?

packetkiller:
Thanks guys. Here is the code:

PaulRB:
please post your code as described in the forum guide.

In fact, please modify post #4.

Circuit is fine. The problem is in the code. The code is switching the dim leds on for short periods every time loop() executes. This happens hundreds of times per second, so the leds appear dim. You need to tidy up the logic.

For example:

    if(onOffSwitch1State == HIGH){
      motor0Enabled = !motor0Enabled;
      digitalWrite(greenLEDPin, HIGH); // motor is on
      digitalWrite(redLEDPin, LOW); // motor is not off
    }

This code can either enable or disable the motor when the on/off switch is pushed. If it was enabled, it gets disabled and vice-versa. That's what the "!" does. But the green led & red led are set to show motor is on whether that's true or not.

Thanks PaulRB! That makes perfect sense now that you pointed it out for me.

I will clean up the code and find a better way to turn on and off the LEDs.

Appreciate the help.

Will post back my updates / next try later tonight or when I can get to it so others may also benefit.

packetkiller:
Appreciate the help.

Please show that by reading the forum guide and fixing post #4.

Happy to fix post #4. I read the forum guide but was not easily able to understand what needs to be done. From reading other similar post issues, I think you are trying to tell me to put the code in a code block. Trying to figure out how to do that. May need some help. A link to where in the guide it provides instruction to "how" to do that would help me. I have the fixed code (thanks again for the help getting it fixed). I will hold off on posting that till I get post#4 fixed so I can make the last post appropriatly.

In the meantime, I will keep reading and try to figure it out on my own...

Ok, played around with post#4. I think it is what you were asking for. If it is, please let me know and I will do the same thing when I post the fixed code.

Thanks again.

Here is the fixed code, thanks for the help along the way, it was fun:

const int control0Pin1 = 2; // clockwise rotation on/off
const int control0Pin2 = 3; // couter-clockwise rotation on/off
const int enable0Pin = 9; // motor contol on/off
const int directionSwitch0Pin = 4; // switch0 to control direction
const int onOffSwitch1StateSwitch1Pin = 5; // switch1 to contol motor on/off
const int pot0Pin = A0; // speed variable pot

const int redLED0Pin = 12; // motor off indicator
const int greenLED0Pin = 7; // motor on indicator
const int blueLED0Pin = 8; // motor clockwise spin indicator
const int yellowLED0Pin = 10; // motor counter-clockwise indicator

int onOffSwitch1State = 0; // switch1 = motor on/off
int previousOnOffSwitch1State = 0; // switch1 state from last loop cycle
int directionSwitch0State = 0; // switch0 = motor spin direction
int previousDirectionSwitch0State = 0; // switch0 state from last loop cycle

int redLED0State = 0; // red indicator; off by default
int greenLED0State = 0; // green indicator; off by default
int blueLED0State = 0; // blue indicator off; by default
int yellowLED0State = 0; // yellow indicator; off by default
// note: there is no need to keep up with the LED state changes from the last loop as they align with the switch states

int motor0Enabled = 0; // motor on/off tracking variable; 0 = off, 1 = on
int motor0Speed = 0; // motor speed value; 0 = no spin, 1 to 1023 = speed value
int motor0Direction = 1; // motor direction value; 0 = couter-clockwise, 1 = clockwise

void setup() {
  // setup pin types (input or outputs)
  pinMode(directionSwitch0Pin, INPUT);
  pinMode(onOffSwitch1StateSwitch1Pin, INPUT);
  pinMode(control0Pin1, OUTPUT);
  pinMode(control0Pin2, OUTPUT);
  pinMode(enable0Pin, OUTPUT);

  pinMode(redLED0Pin, OUTPUT);
  pinMode(greenLED0Pin, OUTPUT);
  pinMode(blueLED0Pin, OUTPUT);
  pinMode(yellowLED0Pin, OUTPUT);

  // set motor to off by default
  digitalWrite(enable0Pin, LOW);

  // set LEDs to off by default, except redLED0 motor indicator that is set to on by default
  digitalWrite(redLED0Pin, HIGH);
  digitalWrite(greenLED0Pin, LOW);
  digitalWrite(blueLED0Pin, LOW);
  digitalWrite(yellowLED0Pin, LOW);
}

void loop() {
  // read switch1 pin to see if set to on or off
  // update the onOffSwitch1State variable to match the pin state
  onOffSwitch1State = digitalRead(onOffSwitch1StateSwitch1Pin); // read switch1 pin to see if set to on or off
  delay(1);
  // read the switch0 pin to see what direction the motor is set to spin
  // update the directionSwitch0State variable to match the spin direction state
  directionSwitch0State = digitalRead(directionSwitch0Pin);
  // set the motor speed to something a bit slower than default voltage values
  motor0Speed = analogRead(pot0Pin)/4;

  // check the state of the on / off switch1
  if(onOffSwitch1State != previousOnOffSwitch1State){
    if(onOffSwitch1State == HIGH){
      // invertend value of notor0Enabled is stored in motor0Enabled as HIGH/1 or on
      motor0Enabled = !motor0Enabled;
      redLED0State = 0; // turn off red indicator state
      greenLED0State = 1; // turn on green indicator state
    }
  }

  // check the state of the direction switch0
  if(directionSwitch0State != previousDirectionSwitch0State){
    if(directionSwitch0State == HIGH){
      // inverted value of motor0Direction is stored in motor0Direction as LOW/0 or couter-clockwise
      motor0Direction = !motor0Direction;
      blueLED0State = 1; // turn on blue indicator state
      yellowLED0State = 0; // turn off yellow indicator state
     }
  }

  // set motor direction outputs to new values
  if(motor0Direction == 1){
    digitalWrite(control0Pin1, HIGH);
    digitalWrite(control0Pin2, LOW);
  }
  else {
    digitalWrite(control0Pin1, LOW);
    digitalWrite(control0Pin2, HIGH);
  }

  // set motor on or off and LED outputs to new values
  if(motor0Enabled == 1){
    analogWrite(enable0Pin, motor0Speed);
    digitalWrite(redLED0Pin, LOW);
    digitalWrite(greenLED0Pin, HIGH);
    if(motor0Direction == 1){
    digitalWrite(blueLED0Pin, HIGH);
    digitalWrite(yellowLED0Pin, LOW);
    }
    else {
          digitalWrite(blueLED0Pin, LOW);
          digitalWrite(yellowLED0Pin, HIGH);
    }
  }
  else {
    analogWrite(enable0Pin, 0);
    digitalWrite(redLED0Pin, HIGH);
    digitalWrite(greenLED0Pin, LOW);
    digitalWrite(blueLED0Pin, LOW);
    digitalWrite(yellowLED0Pin, LOW);
  }

  previousDirectionSwitch0State = directionSwitch0State;
  previousOnOffSwitch1State = onOffSwitch1State;
  
}

Thanks for fixing post #4. The guidance about that is here.

packetkiller:
Here is the fixed code

Its different but still wrong. For example the part I pointed out before:

      // invertend value of notor0Enabled is stored in motor0Enabled as HIGH/1 or on
      motor0Enabled = !motor0Enabled;
      redLED0State = 0; // turn off red indicator state
      greenLED0State = 1; // turn on green indicator state

This code could be setting the motor state to enabled or disabled. But the led states are always set the same.

What are the led state variables for, anyway? They are set but I don't think they are used.