I am using a Mega to control some relay boards. I have a 16 relay board near the mega, and I want to put an 8 relay board about 40 ft. away. So I figured I could use a shift register - instead of running 9 wires (8 relays and gnd) I could just run 4 (gnd, latch, clock, and data). I had some cat5e cable so I ran that from the Mega to the shift register. (I have an external 5V power supply so that does not come from the mega).
Problem is that is works, but inconsistently. I wrote a test program to turn on the relays in sequence. Sometimes it goes in order as it should but other times it skips relays or goes out of order.
I think there might be 4 sources of the problem:
1 The cat5e cable is too thin. Would a bigger wire help? Will twisting one pair together and using it like it was one wire help?
2 The shift_out code timing is off. I tried inserting some delay
statments and that seemed to help a little. But I'm not sure where to insert them.
void shift_out() {
DigitalWrite(latch_pin, LOW);
for (i = 0; i < 8; i++) {
DigitalWrite(data_pin, !!(bits & (1 << i)));
DigitalWrite(clock_pin, HIGH);
DigitalWrite(clock_pin, LOW);
}
DigitalWrite(latch_pin, HIGH);
}
DigitalWrite
is my own function that manipulates the PORTn bits instead of using Arduino's digitalWrite()
.
3 The shift register is very close to some 110V wires. Maybe interference?
4 The 5V power supply isn't powerful enough to power the relay board and the shift register?
Any suggestions welcome.
Edit: here's a pic of what I've built. The Cat5 cable runs directly back to the Arduino.