Ok, been puzzled about this problem I have. So I've hooked up my 8-channel relay on the shift register (SN74HC595N) and I'm trying to simulate a simple on-off sequence for testing (using pi4j).

So far I have managed to do a complete sequence from IN1 to IN8 on a 1 second sleep interval without issues. Problem comes when I change the sleep interval down to 500ms. The relay turn on/off sequentially from channel 1 to 5 but as soon as it hits the 6th channel, it starts to go random.

I'm wondering, since the timing between shifts seems to be causing the issue (not entirely sure)...Does this have anything to do with the latch/clock pulse timings?

Please bear with me as I am a complete beginner on this subject.

Below is the code I used for testing.

//Starting Value for Shift Register
int initVal = 0b11111111; //All channels off

for (int x=7; x>-1;x--)
    int value = setBitFromPos(initVal, x, false);
    info("%02d == %s", value, StringUtils.leftPad(Integer.toBinaryString(value), 8, '0'));
    Thread.sleep(500); //Works OK on 1000ms, goes nuts when set to 500

//Set/Unset a bit from a specific location and return it's decimal equivalent
private static int setBitFromPos(int value, int pos, boolean state)
    return (state) ? (value ^ 1 << pos) : (value & ~(1<<pos));

private void shiftOut(int value) throws InterruptedException
    //using pi4j Shift class
    Shift.shiftOut((byte)dataPin.getPin().getAddress(), (byte)clockPin.getPin().getAddress(), (byte)Shift.LSBFIRST, (byte)value);

private void latch() throws InterruptedException {
  • Are you sure it's not a wiring problem. If you power the shift register from 5V but feed the serial data in with 3V3 (i.e. from a GPIO) you will get erratic operation.
    – joan
    Dec 2, 2015 at 6:41

1 Answer 1


I'm aware this was posted last Dec 2015. Just thought of sharing what I found to be the culprit of this problem. The CLEAR pin was not connected at all, so when I put it on HIGH, the problem went away and the relay was working as expected. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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