All LED's are glowing with flicker while the shiftRegiter595 program

Earlier while uploading the shiftRegister595 based program I got the following error msg:

–exit status 1 Error
compiling for board Arduino/Genuino Uno.

After some searching at the Arduino sites found that the #include statements of shiftRegister.h and shiftRegister595.h needed to be changed to #include<Arduino.h>…

Did the changes as suggested …

Now the program got compiled and uploaded with a Thank you … But what I find now is that all the LED’s are glowing with a flicker… Tried to change the write statement for one LED … but there was no change all Led’s kept glowing as before.

I tried to change the delay value to 100,000 but still there was no change…

Please inform where I have gone wrong, the program I uploaded is given below:

#include <ShiftRegister.h>
#include <ShiftRegister595.h>

byte latchPin = 9; //Pin connected to ST_CP of 74HC595
byte clockPin = 8; //Pin connected to SH_CP of 74HC595
byte dataPin = 10; //Pin connected to DS of 74HC595

ShiftRegister595 shiftRegister595 = ShiftRegister595( latchPin , clockPin , dataPin );

void setup() {

void loop() {
for( int j =1; j<8;j++){


How about simplifing it to start:

byte latchPin = 9;  //Pin connected to ST_CP of 74HC595 (RCK)
byte clockPin = 8; //Pin connected to SH_CP of 74HC595 (SRCK)
byte dataPin = 10;  //Pin connected to DS    of 74HC595
// OE/ (G/) connected to Gnd
// MRCLR connected to +5
// 0.1uF cap from +5 to Gnd located near pin 16

void setup() {
pinMode (latchPin, OUTPUT);
pinMode (clockPin, OUTPUT);
pinMode (dataPin, OUTPUT);

void loop() {
 for( int j =1; j<9; j<<1){ // walk a 1 across the outputs
 digitalWrite (latchPin, LOW);
 shiftOut (dataPin, clockPin, MSBFIRST, j);
 digitalWrite (latchPin, HIGH);

I am new to Arduino ... As a starter I compiled and uploaded the program indicated by Crossroad and it worked very well... But when it came to shiftRegister595 the same setup did not work...

Hi CrossRoads,

Yeah tried your program : “How about simplifing it to start:”

The ‘1’ did not walk through. It started and stopped at Q0…

I feel the code needs a twik @ the first line of loop () ie

" for( int j =1; j<9; j<<1){ // walk a 1 across the outputs"…

j<<1 should j++ if you need to walk through && it did when I changed it to j++… The 1 moved up upto BCD 8 and returned to 0 and repeated…

Yeah now lets proceed further…

Your help required very much for solving the shiftRegister595 library problem…


I would suggest these 2 libraries have problems then.
#include <ShiftRegister.h>
#include <ShiftRegister595.h>

As you can see, they are not really needed. I don’t know anything about them.
If you must, you can write the 3 lines as a function

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

but I’ve never really seen the point of that.

" for( int j =1; j<9; j<<1){ // walk a 1 across the outputs"…

Yes, that should have been j < 0b10000001, or 0x81, so one LED at a time came on:
00000001, 00000010, 00000100, 00001000, 00010000, 00100000, 01000000, 10000000
9 = 0b00001001, which is not what I intended.

  1. you are right Mr CrossRoads… shiftRegister595 library appears to be a failed stuff… However I would like a community response…

  2. your " j < 0b10000001, " in the program “How about simplifying it to start:” also does not work … I tried by putting this condition in the for loop the program did not move beyond Q0 …

for( byte j =1; j<0b10000001; j<<1){
(byte instead of int)
should work. Can check it out when I get home.
I suppose j==0 would also work, as after 100000000 the next shift yields 00000000

hi CrossRoads,

the dead end of the program is Q0 for all variations suggested .. I think the 'j' likes only int and rejects byte, bool etc...

the dead end of the program is Q0 for all variations suggested .. I think the 'j' likes only int and rejects byte, bool etc...

I don't understand that sentence at all. Perhaps re-read it yourself and see if it makes sense?

You may find some help from my page about the 595 chip.

Arduino shift out example.

The low byte of the int numberToDisplay gets shifted in so perhaps

for( int j = 1; j < 256; j << 1){ // walk a 1 across the outputs, bit 0 == 1, bit 7 == 128

BTW, how are you powering the shift register? What resistors are you using on the leds?
What happens when you feed it 255 (all leds on)?

hi CrossRoads,

the dead end of the program is Q0 for all variations suggested … I think the ‘j’ likes only int and rejects byte, bool etc…

I don’t think that the loop counter is being updated correctly in that code. Use j<<=1 instead of j<<1.

You da mang, Stuart! +1 karma to you.

void setup() 
  Serial.begin( 250000 );
  for ( int j = 1; j < 256; j <<= 1 )
    Serial.println( j, BIN );

void loop() {
  // put your main code here, to run repeatedly:

Here is the binary output:


Hi Nick Gammon.
All I meant was:

  1. The there no walk through at all… only the Q0 led was lit … ie the count was starting and stopping at Q0… This was with all the permutation suggested by crossRoad…
  2. the variable j in the for loop was accepting only int and was giving compiler error with bool or byte

Thanks stuart0

Your are good … your suggestion works …the code is compiling and the 1 is walking through by amounts fixed by the second term of the for loop . I have placed the for loop in the void loop( ) method… The walk through is as follows :

a) for( int j =0: j < 2; j<<=1) ---- starts and stops at Q0 ie first led
b) for( int j =0: j < 4; j<<=1) ---- stops at Q1 ie second led and the cycle repeats
c) for( int j =0: j < 4; j<<=1) ---- stops at Q2 ie third led and the cycle repeats
d) for( int j =0: j < 8; j<<=1) ---- stops at Q3 ie Fourth led and the cycle repeats
g) for( int j =0: j < 128; j<<=1) ---- stops at Q7 ie 7th led and the cycle repeats

but if we set “j <256” …the walk through is upto the 7th led and the 8th led does not glow… and the whole set-up gets dark… ie there is no further glowing of any led…
The reason I believe is that the byte becomes : 00000000 and thus there is no further action and cycle collapses.

That is because you start with int j = 0 instead of int j = 1.

The term j <<= 1 shifts all bits in j up 1 place, same result as j = j * 2. But there has to be set bits to see and difference, all 0’s shift up to all 0’s.

Do you know how bits in variables work?

Sorry sir

j = 0 instead of j=1 — will not work because you need a ‘1’ to move it to the left… As there is no 1 in the intiial byte, no led will glow…

so the ’ for ’ statement has to be :

for(int j =1; j< 128; j <<1) { } and not

for(int j =0; j< 256; j <<1) { }

for things to work

Did you see the sketch I posted on page 1 showing Stuart’s way works?

The output is the actual values of j.

If I showed leading zeros, it would have taken more code.

These are the positions of the low 8 bits of j for ( int j = 1; j < 256; j <<= 1 )
The high 8 are all zero until j becomes a value > 255.

for j starting with 1, run the loop contents then shift the bits in j 1 to the left until j >= 256.

and there it is:

00000001 j = 1
00000010 j = 2 which is 1 left-shifted by 1
00000100 j = 4 which is 2 left-shifted by 1
00001000 j = 8 which is 4 left-shifted by 1
00010000 j = 16 which is 8 left-shifted by 1
00100000 j = 32 which is 16 left-shifted by 1
01000000 j = 64 which is 32 left-shifted by 1
10000000 j = 128 which is 64 left-shifted by 1

Bits are an integral part of C because it’s close to the hardware and lean. Here’s a nice tutorial:

And the Arduino Reference page has a Bitwise Operators section if you only want to check syntax:

A lot of times I just make an array and then loop thru the array.

byte shiftArray[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
// or flip all the bits if you wanted just one 0 for creating chip selects for example

for (i = 0; i <8; i=i +1){
digitalWrite (ssPin, LOW);
SPI.transfer (shiftArray[i]);
digitalWrite (ssPin, HIGH);
// then other arrays can be accessed normally at the same time for i = 0,1,2,3,4,5,6,7
for ( uint8_t i = 0b00000001; i; i <<= 1 )
    digitalWrite(pinSS, LOW);


    digitalWrite(pinSS, HIGH);

EDIT: The key here is that ‘i’ is declared as an ‘unsigned’ 8 bit variable

for ( uint8_t i = 0b00000001; i; i <<= 1 )

Oh I LIKE that, you made me think. What a great trick for the 8-bit limit. :slight_smile:

But why not just uint8_t i = 1; ?