Long Distance Shift Register 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 &lt; 8; i++)  {
         DigitalWrite(data_pin, !!(bits &amp; (1 &lt;&lt; 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.
SPI is supposed to be used for things in close proximity to each other. See Wikipedia - Serial Peripheral Interface Bus.


  The Serial Peripheral Interface (SPI) bus is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. 


(My emphasis).

At that distance, and at standard SPI speeds you are probably losing signal integrity. The default SPI speed is system clock divided by 4 (that is, 4 MHz). It's not surprising some bits go missing after 40 feet.

See my page about SPI - you can use SPI to transfer to a 595 register (as described here).

(I'm assuming you are talking about a 74HC595 shift register)

I suggest running at a somewhat lower speed - insert delays after toggling the clock pin (each way). Or use the SPI hardware at (say) SPI_CLOCK_DIV128.




  The shift register is very close to some 110V wires. Maybe interference


You might also want to look at a balanced line driver (eg. RS485) - I have a page describing that.




  The 5V power supply isn't powerful enough to power the relay board and the shift register?


The register won't take much power. I trust the relays are solid-state ones and not coils that you are directly driving from the 595 chip?



Also see How do you use SPI on an Arduino?