74HC595 - Controlling 8 LEDs

I’m having a problem controlling 8 LEDs that are hooked up to an 74HC595 Serial to Parallel IC.

First problem:

For demonstration purposes, I am using a for loop to shift though each of the 8 LEDs back and forth. For some reason, the second for loop doesn’t do what its supposed to do and shift the LEDs the opposite direction. No matter what I change i.e. LSBFIRST, changing << to >> etc. it just seems to want to start from Q0 and end at Q7.

void loop() {
  
  for (int i=0; i<8; i++) {
    shiftOut(dataPin, clockPin, MSBFIRST, B00000001 << i);
    digitalWrite(latchPin, HIGH);
    delay(125);  // 125*8 is 1 second
    digitalWrite(latchPin, LOW);
  }
  
  for (int i=0; i<8; i++) {
    shiftOut(dataPin, clockPin, MSBFIRST, B10000000 >> i);
    digitalWrite(latchPin, HIGH);
    delay(125);  // 125*8 is 1 second
    digitalWrite(latchPin, LOW);
  }
  
}

Second problem:

Again for demonstration purposes, I tried to switch the first 4 LEDs on followed by the last 4 LEDs like so:

void loop() {
  
  shiftOut(dataPin, clockPin, MSBFIRST, B00001111);
  digitalWrite(latchPin, HIGH);
  delay(5000);
  digitalWrite(latchPin, LOW);
  
  shiftOut(dataPin, clockPin, MSBFIRST, B11110000);
  digitalWrite(latchPin, HIGH);
  delay(5000);
  digitalWrite(latchPin, LOW);
  
}

This didn’t work and produced some strange results.

byte i;
byte blah;

void setup() 
{
  //set pins to output so you can control the shift register
  pinMode(latchPin,OUTPUT);
  digitalWrite(latchPin,LOW);
  pinMode(clockPin,OUTPUT);
  digitalWrite(clockPin,LOW);
  pinMode(dataPin,OUTPUT);
  digitalWrite(dataPin,LOW);
}

void loop() 
{
  blah = 1;
  for(i=0; i<8; i++) 
  {
    shiftOut(dataPin, clockPin, MSBFIRST, blah);
    digitalWrite(latchPin, HIGH);
    digitalWrite(latchPin, LOW);
    delay(125);  // 125*8 is 1 sec
    blah = blah << 1;
  }
  blah = 64;
  for(i=0; i<6; i++) 
  {
    shiftOut(dataPin, clockPin, MSBFIRST, blah);
    digitalWrite(latchPin, HIGH);
    digitalWrite(latchPin, LOW);
    delay(125);  // 125*8 is 1 sec
    blah = blah >> 1;
  }
}

Thanks, all seems to be in order now.

If I was going to hardwire a 74HC595 with a lot of LEDs, would it be best to implement a tactile button for the "master reset" and the "output enable" pins? If its yes for the both, why the "output enable" also?

daz1761: If I was going to hardwire a 74HC595 with a lot of LEDs, would it be best to implement a tactile button for the "master reset" and the "output enable" pins? If its yes for the both, why the "output enable" also?

I'm not sure why any of that would be necessary. You could call a function in setup() to knock out all 0's to clear it out (and just use the Reset button from there.)

would it be best to implement a tactile button for the "master reset" and the "output enable" pins?

No.

I'm not sure why any of that would be necessary. You could call a function in setup() to knock out all 0's to clear it out (and just use the Reset button from there.)

I see, so I would have something like this:

void setup() {
  pinMode(clockPin, OUTPUT);
  digitalWrite(clockPin,LOW);
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin,LOW);
  pinMode(latchPin, OUTPUT);
  digitalWrite(latchPin,LOW);
  
  shiftOut(dataPin, clockPin, MSBFIRST, 0b00000000);
  digitalWrite(latchPin, HIGH);
  digitalWrite(latchPin, LOW);
}

Also, I have a wiring query: I have seen two ways in which 8 LEDs can be wired up with this IC...

  1. Only using one 220Ohm resistor where all 8 LEDs and the OE pin share the same resistance.

  2. Using a 220Ohm resistor with each LED individually like the Arduino 595 tutorial states.

I've actually tried both and all seems to work, but which is the best/safest solution?

No. 2

With 1, you will find that the leds fade as you light more of them.

Not sure what you mean about OE. You should connect that to 5V, or to an Arduino PWM output if you want to fade the leds.

Paul

No. 2

With 1, you will find that the leds fade as you light more of them.

Paul

Thanks, that's good to know :)

You wouldn't know where to point me in the right direction in terms of applying PWM to each LED?

shiftPWM library google that

Thanks :)

Not sure what you mean about OE. You should connect that to 5V, or to an Arduino PWM output if you want to fade the leds.

OE should connect to Gnd to be always on, or to PWM for brightness/fade control. Connecting to 5V will disable the outputs.

CrossRoads: Connecting to 5V will disable the outputs.

Thanks Bob, I forgot it is actually "/OE" !

OE should connect to Gnd to be always on, or to PWM for brightness/fade control. Connecting to 5V will disable the outputs.

That's interesting - I did previously mention how I could incorporate PWM and I came across ElcoJacob's PWM library. Tbh, I don't need individual LEDs changing brightness. I just would like all 8 LED's of the one IC to fade in - hold for so many seconds - then fade out.

I've not got an Arduino to hand atm, but would something like this work if I connect the output enable (OE) to a PWM pin on the Arduino:

const int output_enable_pin = 6;

void loop() {

  // fade in example?
   for (int i=255; i>0; i--) {
    analogWrite(output_enable_pin, i);
    delay(10);
  }
  // keep LOW
  // fade out..
}

daz1761: I've not got an Arduino to hand atm, but would something like this work if I connect the output enable?

Looks about right.

Great :-)

Just tried my code out and it works fine. I have the first 4 LEDs fade in (0b00001111), then the last 4 fade in (0b11110000).

The only strange thing I noticed was when I pulled the jumper out that took the Output Enable (OE) straight to ground while the Arduino was on, I expected the all of the LEDs to power down until I put the pin into a PWM pin and re-programmed the Arduino.

Was this because the OE was in a floating state (neither gnd or high)?

When pins are floating, you can't rely on what the chip does. You must not be surprised either!

Thanks - thats answered that question :)