Controlling 16 LED's with two 74HC595 Shift register

Hello,

I am trying to controll 16 LED's light using TWO 74HC595 shift registers. (see schematic in attachment).
I am trying to run a simple program in a loop that would turn all the light OFF at the same time and then turn them back on (still in unisome).

Now, when I run my program, this is the result I am getting:

The first 8 light would go off.
The next 8 would go off.
Then the first 8 light would go ON,
The next 8 lights would go ON.
And son on ....

Can anyone please tell me what I am doing wrong.

int clockPin = 11;
int latchPin = 8;
int dataPin =12;

byte LED1 = 0b1111111111111111;
byte LED2 = 0b0000000000000000;

void setup (){

  pinMode (clockPin, OUTPUT);
  pinMode (latchPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
}


void loop (){

  digitalWrite (latchPin, LOW);
  shiftOut (dataPin, clockPin, MSBFIRST, LED1);
  digitalWrite (latchPin, HIGH);

  delay (1000);

  digitalWrite (latchPin, LOW);
  shiftOut (dataPin, clockPin, MSBFIRST, LED2);
  digitalWrite (latchPin, HIGH);

  delay (1000);
  
}

Hello,

A byte is 8 bits

Explaination of what your code is doing :

Send 11111111 to the first 595
Delay
Send 00000000 to the first 595, so the second 595 will be 11111111 (as all 8 bits of the first 595 were shifted into the second)
Delay
Send 11111111 to the first 595, so the second 595 will be 00000000 (as all 8 bits of the first 595 were shifted into the second)
etc...

Solution :

You have to change the data type to a 16 bits type (uint16_t), and call shiftOut twice, each with half the bits of your 16 bits of data

uint16_t LED1 = 0b1111111111111111;
...
shiftOut(dataPin, clockPin, MSBFIRST, LED1 >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, LED1);

The compiler should have warned you about this. If it didn't, go to your Arduino IDE preferences right now and enable all warnings.

warning: large integer implicitly truncated to unsigned type [-Woverflow]
 byte LED1 = 0b1111111111111111;
             ^~~~~~~~~~~~~~~~~~

Pieter

PieterP:
The compiler should have warned you about this. If it didn't, go to your Arduino IDE preferences right now and enable all warnings.

warning: large integer implicitly truncated to unsigned type [-Woverflow]

byte LED1 = 0b1111111111111111;
            ^~~~~~~~~~~~~~~~~~



Pieter

Hello Pieter,

Actually, I got that warning.
But after changing the code, I am getting this error:

exit status 1
'unit16_t' does not name a type

Not unit16_t but uint16_t, as in unsigned integer of 16 bits :slight_smile:

You may want to try this little library: GitHub - bildr-org/Shift-Register-8-Bit-74HC595: Arduino code to control the 74HC595 8-bit Shift-Register

guix:
Not unit16_t but uint16_t, as in unsigned integer of 16 bits :slight_smile:

You may want to try this little library: GitHub - bildr-org/Shift-Register-8-Bit-74HC595: Arduino code to control the 74HC595 8-bit Shift-Register

You are the best.
Thank you so much

guix:
Explaination of what your code is doing :

Send 11111111 to the first 595
Delay
Send 00000000 to the first 595, so the second 595 will be 11111111 (as all 8 bits of the first 595 were shifted into the second)
Delay
Send 11111111 to the first 595, so the second 595 will be 00000000 (as all 8 bits of the first 595 were shifted into the second)
etc...

Solution :

You have to change the data type to a 16 bits type (uint16_t), and call shiftOut twice, each with half the bits of your 16 bits of data

uint16_t LED1 = 0b1111111111111111;

...
shiftOut(dataPin, clockPin, MSBFIRST, LED1 >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, LED1);

Hello Quix
If I added a third 74HC595 to control an additional 8 LEDs to make it 24 LEDs in total, can I modify the code as below:

uint32_t LED1 = 0b1111111111111111;
...
shiftOut(dataPin, clockPin, MSBFIRST, LED1 >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, LED1 >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, LED1);

Check the lengths of your right shifts.

As AWOL said (but you probably didn't understand), you have to right shift ( >> ) the correct amount of bits

//                XWVUTSRQPONMLKJIHGFEDCBA
uint32_t LED1 = 0b111111111111111111111111;
...
shiftOut(dataPin, clockPin, MSBFIRST, LED1 >> 16); // send bits X-Q
shiftOut(dataPin, clockPin, MSBFIRST, LED1 >> 8);  // send bits P-I
shiftOut(dataPin, clockPin, MSBFIRST, LED1);       // send bits H-A

Add 0.1uF caps from the '595 VCC pins to Gnd.
I don't see pin 9 from one chip going to 14 on the other, that is needed to pass the data from device to device.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.