Controlling Relays using Shift Registers

I have been trying to use shift registers (74HC595) in one of my projects and I am using daisy chaining in the same since I have to control switching of 9 devices.

I only have 3 GPIO available on my ESP32 board, need to control 9 relays and 3 LEDs using 2x74HC595 Shift registers.

The issue I have is, no matter what I try, the relay switches do not seem to work as expected.

Ideally, a shift register works in this way.

//Pin connected to ST_CP of 74HC595
int latchPin = 1;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 0;


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

void loop() {

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

When I use "B10010100" as the value, my understanding is out of 9 relays, I should be able to control 8 using above code.

With B10010100, I should have below configuration.

  • Relay 1 - ON
  • Relay 2 - OFF
  • Relay 3 - OFF
  • Relay 4 - ON
  • Relay 5 - OFF
  • Relay 6 - ON
  • Relay 7 - OFF
  • Relay 8 - OFF

But, I have spend atleast 2 days trying to figure out, why am I not able to control them in the way I expect. I am still able to control them using 595's, using different codes available on internet (mostly LED), which means the schematic is right.

I have attached a wiring diagram, with just one Shift Register, to make it simple to understand, and finally the three transistor on the left have their Emitter connected to relays. I have 9 of such transistor and 9 relays connected.

the relay switches do not seem to work as expected.

Please describe exactly what does happen?

The order of the relay being switched off/on is just unpredictable.

11001100

Should have first, second, fifth and sixth switched ON, but in reality, I have 3,4,7 and 8 switched ON. Which does look straight forward, where 0 means relay is ON.

However,

  • if I used, 00110011, then I only have 1,2 and 5th switched ON.
  • if I used, 01111111, then I only have 1,2,3,4,5 and 7th switched ON.
  • if I used, 00000001, then I have 1 switched ON, which is right, but then first logic becomes wrong

cyberpks:
11001100

Should have first, second, fifth and sixth switched ON, but in reality, I have 3,4,7 and 8 switched ON. Which does look straight forward, where 0 means relay is ON.

I looks to me as if "1" means "relay is ON", and they are numbered 8,7,6,5,4,3,2,1 !!

cyberpks:

  • if I used, 00110011, then I only have 1,2 and 5th switched ON.
  • if I used, 01111111, then I only have 1,2,3,4,5 and 7th switched ON.

Looks like 6th is not working.

cyberpks:

  • if I used, 00000001, then I have 1 switched ON, which is right, but then first logic becomes wrong

Which is correct: relay 1 is connected to the least significant bit (LSB)!

You should check this by activating one relay at a time, like this (pseudo code): send 00000001 to the shift register, delay(1000), send 00000010, delay(1000), etc. up to 10000000.

Erik_Baas:
Looks like 6th is not working.

Actually, yes!! :open_mouth: I did have that feeling earlier but I checked most components and they worked well. However, the transistor was faulty. Thanks a lot for pointing this out. I changed it and its working now.

1 last thing, if you can please help.

How do I control the other 595 with this code,

I can only send 8 bit at a time. But I need to send it to next chip now. Its connected in daisy chaining format.

In pseudo code:

latchPin LOW
shiftOut 8 bits for the second(!) 595
shiftOut 8 bits for the first(!) 595
latchPin HIGH

At least, I think that's how it works, I never actually used it. :wink:

Yeah, other people on the internet have used it in the same way, but for me, it seems like its just overwriting the data on first one.

Exactly how are the 595’s connected?

Exactly this way - https://forum.arduino.cc/index.php?topic=148704.0 (credits to the OP)

Really? I see 3 wires connected to pins 8, 9 and 10, but your code says:

int latchPin = 1;
int clockPin = 12;
int dataPin = 0;

Oh well, as long as clock and latch are connected to both 595's, and data_out from the first is connected to data_in of the second 595.

Are you aware that the first 8 bits go to the second shift register? Just checking... :wink:

Oh wait, that's an Uno. Now I'm lost...

Erik_Baas:
Really? I see 3 wires connected to pins 8, 9 and 10, but your code says:

Yes, I mean the code connections were similar, but the pins on the MCU would be different, since the OP used an Arduino but I'm using ESP32 :slight_smile:

Anyways, I figured out the issue. I missed one connection on Pin 8 (GND) of the second 595.

This fixes all of it!! Awesome!

Really appreciate your help @Erik_Baas :art: . You've been a great :). Mostly it was my ignorance, but somehow posting it here helped a lot. I already wasted 2 days trying to figure out the issue, but I fixed it within a few hours here.