1

I am trying to learn how to use a shift register to control 8 LEDs but for some reason my shift register is misbehaving and none of the LEDs are lighting up. I imagine that it's a problem with my wiring? I have replicated the circuit on 123D Circuits.

circuit diagram

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

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

void loop() {
  // count from 0 to 255 and display the number 
  // on the LEDs
  for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
    // take the latchPin low so 
    // the LEDs don't change while you're sending in bits:
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);  

    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
    // pause before next value:
    delay(500);
  }
}
  • I don't see anything wrong with the image or the code. Try jiggeling all the connections. I've sometimes had wires not connecting properly in a breadboard. – Gerben Jan 20 '16 at 13:41
  • 1
    Rejiggling the pins seemed to do it, – Bob Jones Jan 20 '16 at 15:58
  • It's always the stupidest thing that takes the most time to debug (-: Glad you got it working. – Gerben Jan 20 '16 at 16:06
1

From the Arduino website:

shiftOut()

Description

Shifts out a byte of data one bit at a time. [...]

Note: if you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW).

You need to pull clockPin low before each shiftOut() call, as the documentation says.

Also, you have misunderstood the behaviour of the latchPin. This can be high or low at any time; it's the change from low to high that latches the data into the output cells.

  • digitalWrite(latchPin, LOW); shiftOut(...); digitalWrite(latchPin, HIGH);. Isn't he doing that already? – Gerben Jan 20 '16 at 13:37
  • No, clockPin should be low, according to the blurb. – CharlieHanson Jan 20 '16 at 13:38
  • Aha, clockPin; not latchPin. Missed that after you edited your answer. – Gerben Jan 20 '16 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.