Shift register initializes with all 1's?

I’m trying to control a couple of relays using a shift register. But during the setup(), the all the relays connected to the shift register will click… This will cause a short circuit if I connect the relays to the final circuit…

byte shiftBits = 0;

void registerWrite(int pin, int state) {
	bitWrite(shiftBits, pin, state);
	shiftOut(data, clock, MSBFIRST, shiftBits);
	digitalWrite(latch, HIGH);
	digitalWrite(latch, LOW);
}

void setup() {
	pinMode(latch, OUTPUT);
	pinMode(clock, OUTPUT);
	pinMode(data, OUTPUT);
	Serial.begin(115200); // BTBee
	audioPlayer.begin(9600); // MP3 player thingy
	mp3_set_serial(audioPlayer);
	delay(500);
	mp3_set_volume(audioSettings[0]);
	delay(500);
	mp3_set_EQ(5); // 0-5, Normal/Pop/Rock/Jazz/Classic/Bass
	
	//for (int i = 0; i < buttonArraySize; i++) {
		//registerWrite(i, LOW);
	//}
	delay(1000);
	sendAck("res");
	mp3_play(voice(0));
}

I did clean the shift register in the setup to make sure that it was empty (in case of an unexpected reset of the arduino), I thought this could be the problem, but when I comment out that part, the click in all the relays still happens…

But in any case, the relays shouldn’t do anything when I power it on for the first time right?

What shift register? Link to the data sheet.

If it's the typical 74HC595, how do you have the OE and STCP pins wired?

Could you rewire the relays so that they are active low?

Yes, that's the one... I should've said that in my post...

The SHCP is connected to pin 3 (clockPin), STCP to pin 4 (latchPin) and OE to pin 2 (dataPin)...

SparkyRih:
Yes, that's the one... I should've said that in my post...

The SHCP is connected to pin 3 (clockPin), STCP to pin 4 (latchPin) and OE to pin 2 (dataPin)...

OE is connected to Data? What about DS and MR?

Sorry, OE is connected to GND, the other one is connected to pin 2...
MR is connected to the 5v supply...

Nobody?

Connect OE to an Arduino pin and make it HIGH; best is probably to use an external pull-up resistor. The external pull-up resistor will keep the OE pin HIGH while the Arduino is booting / initializing.

Outputs of the 595 will be tri-state and no current will flow. Next shift zeroes into the 595 and after that set the OE pin LOW.

Not tested, just try it.

sterretje:
Connect OE to an Arduino pin and make it HIGH; best is probably to use an external pull-up resistor. The external pull-up resistor will keep the OE pin HIGH while the Arduino is booting / initializing.

Outputs of the 595 will be tri-state and no current will flow. Next shift zeroes into the 595 and after that set the OE pin LOW.

Not tested, just try it.

After reading a couple of articles I completely understand how the 595 works, so I also came up with that solution, but I didn't try it yet...

I tried connecting the A0 of another Arduino to one of the shift register pins, but I can't find any pulse on boot up (it always fluctuates between 30-31, never goes any higher or lower)? When I purposely set the specific shift register pin to high, the second Arduino does recognize the pulse, but it never finds one during setup()...

All Arduino pins are configured as input when the micro boots. Connecting an Arduino input pin and a 595 input pin still results in a floating input (as if nothing was connected; same problem as with buttons).

The safe way is to use an external pull-up (as explained, that guarantees that the pin is HIGH when the micro resets).

Next in setup()

void setup()
{
  // make sure it is high when we switch the Arduino pin to output
  digitalWrite(A0, HIGH);
  // switch to output
  pinMode(A0, OUTPUT);

  // write 0s to the shift register
  ...
  ...

  // enable 595 outputs
  digitalWrite(A0, LOW);



  // other init
  ...
  ...
}

Just showing the idea.

A pull-down resistor on the latch pin seems to solve the entire problem…

I can bootup and reset the Arduino however I want, the relays won’t be activated unexpectedly anymore… But they do work when I want them to work of course :slight_smile:

I need some more testing of course, but I think this will be safe…