shiftIn() returns 0

I'm trying to make use out of the SN74HC595N chip i had laying around and wanted to make a command line type of stuff, everything works until i try to get data from the chip.
I expected it to tell me the number i put (in this case, its 50) but it just returns 0

Here is the code:

void loop() {
  /* Serial reader, you may ignore this or uncomment if you are debugging.
  String output; 
  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      output += c; //makes the string readString
    } 
  }*/
  if (output.startsWith("STORE")) {
    int arg = output.substring(6).toInt() * 2; // To make it display correctly in binary, i multiply it by 2 
                                               //because in the LEDs it is half of the number.
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, arg);
    digitalWrite(latchPin, HIGH);
    Serial.println("DONE");
  }else if (output.startsWith("GET")) {
    int result = (int)shiftIn(dataPin, clockPin, MSBFIRST) / 2; // divide by 2 because we just multiplied by 2
    Serial.println(result);
  }
}

74HC595 is a serial to parallel shift register… not much good for shifting ‘in’.

You might like to look at the 74xx165 series for input.

There are also other bidirectional chips, but this should sort you out.

1 Like

oh, sorry about that im new to this, what's the right approach to read from this chip?

In short, you don’t READ from the 74xx595 chip under 99% of situations

Go buy and read about the 74xx165 and dig in again.

1 Like

The 595 is a Serial In / Parallel Out shift register. You can shift data OUT of the Arduino IN to the data pin of the 595 and when you raise the latch line, the bits will appear on the 8 outputs of the register, in parallel. It is only good for outputs.

The 165 is a Parallel In / Serial Out shift register. When you lower the latch line, the data on the 8 parallel pins get latched in the register in parallel. Then you can shift data IN to the Arduino OUT of the data pin of the 165. it is only good for inputs.

On 595 a serial input pin (14) is used for a 'shiftOut', but a serial out pin (9) is used for a 'shiftIn'. You need to use different pin on boths sommands. Keep in mind that when you perform a 'shiftIn' from 595, some other value will probably be loaded.

595 pin description
pin 14 serial data input
pin 9 serial data output

And i am not sure for uses of 'latchPin' - in your 'shiftIn' is not used.

You’re exactly right… the ‘carry bit’ shifted out is intended for daisy-chained ‘595 s.
If you care to read that back in while shifting, you’ll see the data that you shifted out earlier… a bit ring buffer if you will.

There’s no practical reason you’d do this, because you should know what was originally shifted out to the 595/serial-in anyway.

Where I have seen this used is when 595s and 165s are daisy chained to illuminate indicators -and- to simultaneously read switches or other pin states..
The dara stream is shifted through the length of the whole chain, and while you ignore the 595 output bits, you can read and write long strings of inputs from the 165s, and outputs with 3/4 wires. (tricks required for 3 wires!)

Oh, well i saw several examples of shiftIn being used with the 74HC59 but thanks for the help, i will consider getting a 74xx165.

Thank you so much, this is exactly what i was looking for. (the reason i didn't find this simple answer is because of the arduino documentation not giving a name for every pin)

@idonthaveanarduinoboar

If you really don't have an Arduino board, try wokwi.com the Arduino Simulator.

It has both the 74HC595 and the 74HC165 parts available to wire up and experiment upon.

a7

Oh i forgot my name, i do have an arduino board but i made this account when i didnt, but thank you for this website, i will consider using it to help with my future projects.

I do that too, sometimes. :expressionless:

a7