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.
//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– Gerben2016年01月20日 13:41:50 +00:00Commented Jan 20, 2016 at 13:41
-
1Rejiggling the pins seemed to do it,Bob Jones– Bob Jones2016年01月20日 15:58:30 +00:00Commented Jan 20, 2016 at 15:58
-
It's always the stupidest thing that takes the most time to debug (-: Glad you got it working.Gerben– Gerben2016年01月20日 16:06:13 +00:00Commented Jan 20, 2016 at 16:06
1 Answer 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– Gerben2016年01月20日 13:37:41 +00:00Commented Jan 20, 2016 at 13:37 -
No,
clockPin
should be low, according to the blurb.CharlieHanson– CharlieHanson2016年01月20日 13:38:49 +00:00Commented Jan 20, 2016 at 13:38 -
Aha, clockPin; not latchPin. Missed that after you edited your answer.Gerben– Gerben2016年01月20日 16:05:09 +00:00Commented Jan 20, 2016 at 16:05