I'm new to the arduino and electronics in general. I believe I have this 74HC595 shift register hooked up correctly but when I shiftout something like B00001111 all the lights will be on! The first 4 outputs on the shift register read 1 as on, but the last 4 outputs read 1 as off. I am completely confused as to why it does this.
For example if I shiftOut the number 27 I would expect 00011011, ie. the 4th, 5th, 7th and 8th lights on but instead I get the 4th and 6th lights on.
I have tried two different 74HC595 shift registers from two different makes and I still experience the same problem. What am I doing wrong?
//shift register test project
//setting up latch pin, connected to ST_CP
int latchPin = 8;
//setting up clock pin, connected to SH_CP
int clockPin = 12;
//setting up serial data pin, connected to DS
int dataPin = 11;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
byte data = B01100110;
void loop(){
digitalWrite(latchPin, LOW); //setting latch pin low to allow new data to be written into the shift register
shiftOut(dataPin, clockPin, MSBFIRST, data); //shifting out first byte
digitalWrite(clockPin, LOW);
digitalWrite(latchPin, HIGH);
}
tho I doubt it would make much of a difference you dont need
digitalWrite(clockPin, LOW);
B00001111 all the lights will be on! The first 4 outputs on the shift register read 1 as on, but the last 4 outputs read 1 as off. I am completely confused as to why it does this
cause thats what you told it to, the first 4 bytes are 0, and the last 4 are 1, but your sending them MSB FIRST so it starts at the leftmost bit and moves right, LSB FIRST would start on the right and move left
I cut out the digitalWrite(clockPin, LOW); and changed it to LSBFIRST but I still get the same issue. The 2nd set of 4 lights interprets '0' as HIGH not LOW. The first 4 light interprets '0' as LOW.
Is the shift register suppose to do this?
If I run this code from the tutorial:
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);
}
}
The first four lights count up properly but the 2nd set are opposite.
Can you just double check that you have the LEDs the right way round, ie that all of them have their cathodes to GND and the anodes to the relevant HC595 pin, (obviously with series resistors somewhere in there too). The only situation I can think that would result in the behaviour you describe is if accidentally the 2nd four LEDs are the wrong way round and connected to +5v instead of GND (ie with 5v to the anodes and the cathodes to the HC595 + series resistors one or other side)
Do the LEDs work right if they are just connected to normal Arduino pins?
Also try adding a supply decoupling capacitor (0.1uF between GND and +5v, close to the HC595), sometimes when things misbehave randomly its a sign you need one.
What you said Stimmer got me on the right track. Turns out it was a combination of things. I have two different LEDs, white and red ones. The red ones had the cathode as the longer length lead rather than the norm. Also the breadboard I was using had the length for the ground segmented. It just so happened that my red LEDs essentially had no ground. Whoops.