Need help with Arduino and Shift Registers

I have successfully setup the Arduino to drive a shift register (74HC595) by following the documentation and samples available. Visually I could see the LEDs lit up accordingly based on the value I send to the shift register. In addition, I have modified the sample to be able to receive input value serially and send the same value to the shift register, and it does work accordingly as well.

However, I wanted to know if this is the norm or am I missing something… Everytime I connect a serial connection to Arduino (from java program or opening the Arduino Serial Monitor), the shift register sets all bits to HIGH (all LEDs lit up). Is this normal or is there a way to suppress this or am I missing something?

Below is the sketch I have uploaded to Arduino –

String str = "";
int datapin = 4; 
int clockpin = 6;
int latchpin = 7;
byte data = 0;


void setup() {
  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);  
  pinMode(latchpin, OUTPUT);  
  Serial.begin(115200);
}
 
void loop()
{
  // if serial port is available, read incoming bytes
  if (Serial.available() > 0) {
    str = Serial.readString();

      Serial.print("DEBUG RECV: ");
      Serial.print(str);
      Serial.println(" <---END OF RECV");
 
      Serial.print("--Arduino received: ");
      Serial.println(str);

      String scmd = str.substring(0,2);
      Serial.print("Command: " + scmd);
    
      if (scmd == "SR"){
        String sstr = str.substring(2);
        data = sstr.toInt();
        Serial.print("Shifting out: ");
        Serial.println(data);
        
        
        shiftOut(datapin, clockpin, MSBFIRST, data);
        digitalWrite(latchpin, HIGH);
        digitalWrite(latchpin, LOW);
      }
  }
}

Help is highly appreciated. Thanks!

Right, first things first.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

Albeit it is not too complex at present, but it may well get more so.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

Next, we need a perfectly focused photograph of your set-up with all wires clearly visible as they terminate on the modules and show that as a link in the text, not an attachment.

The best way to do this is to take it outside in full daylight but not direct sun and use a digital camera at least a metre away from directly above (or very slightly offset to ensure all the connections are able to be distinguished) using the (actual) zoom to just include all parts of the assembly.

Finally, understand that you have not by any means adequately described the components to which you refer. You may have them in front of you and thus understand what they are, we do not. So you need to post web-links to each part (except the Arduino, with which we are entirely familiar of course :grinning: ). Also explain more clearly what the overall purpose of this system is.

You can add another connection to the output enable pin, with a pullup resistor to 5V. When the arduino is reset by the things you descrive, OE will be pulled high, the outputs will be disabled, and stay off until your sketch loads in good data and enables the output.

This is due to the control lines to the shift register being allowed to float when the Arduino is reset.

Put 47 k pull-downs on the clock and latch pins.

Thanks for the suggestions and apologies for the late reply.

I did read somewhere that 74HC595 will give unexpected results during initialization and this can only be suppressed by a pull down resistor.

@Paul__B: Will 10k work instead of 47k?

I have tested with 10k pull-down for the moment as this is what I currently have, and it works.

sky8libra:
I did read somewhere that 74HC595 will give unexpected results during initialisation and this can only be suppressed by a pull down resistor.

It is all about floating inputs.

One suspects you are breadboarding this and have flying jumper wires all over the place. If connected to nothing - which is to say, an Arduino pin that is uni-initialised, they will pick up the mains frequency from your house wiring - 50 or 60 Hz, so during that reset phase they are furiously clocking rubbish into the chip.

sky8libra:
@Paul__B: Will 10k work instead of 47k?

Fine. I just specified something with a minimal current draw when the circuit is up and running. On a final PCB or stripboard where the connections are more direct, 100k should be fine.

sky8libra:
I have tested with 10k pull-down for the moment as this is what I currently have, and it works.

I thought it would. :grinning: