How to shift binary into a shift register (serial in, parallel out) sequentially

Hi makers!

I'm trying to make a continuity tester at work where I connect the input and output of a harness into a test fixture powered by an Arduino. This tester tests whether the position of the connects are pinned correctly.

From one end of the connector (the output in the diagram), I want to shift in a binary that is in the power of 2. For instance, shifting in 0001 , check for continuity by reading the 75HC165 (parallel in, serial out), then clear the register and then shift in 0010 , and then 0100 and so on. For a correctly pinned harness, the write and read binary subtraction will be zero.

Here is my loop that attempts to perform the above:

void loop() {
  for (int i = 1; i < 4; i++) {
    reset_SIPO_register();
    shift_SIPO(pow(2,i));
    delay(2000);
    shift_SIPO(0x00));
    check_continuity(); // not implemented yet
  }
}

void reset_SIPO_register() {
  digitalWrite(reset_pin, LOW);
  digitalWrite(reset_pin, HIGH);
}

void shift_SIPO(int sequence) {
  digitalWrite(latch_pin, LOW);
  shiftOut(data_pin, clock_pin, MSBFIRST, sequence);
  digitalWrite(latch_pin, HIGH);
}

I am testing this concept on a breadboard with LEDs and the above kind of works. However, the LED is lit up one at a time and they don't turn off, not following the binary.

Why clear it? The set bit that you are using is already in the register. You only need to shift it one more time.

Yup, that is exactly what I thought too, I did it just to see if it will make a difference, but it didn't

For this application you don't even need the "load" pin. Only clock and data.

Right, but removing it won't make a difference,

If this system needs troubleshooting, we need more than the generic diagram you gave us. We need a complete and accurate schematic.

Yup, I'm doing that right now, can you see anything problems in the code?

The use of pow. pow does return a double (depending on the Arduino, that can be a float instead) and floating point numbers are not necessarily exact. Below code demonstrates

void setup()
{
  Serial.begin(115200);
  for (int i = 1; i < 16; i++)
  {
    Serial.print(i);
    Serial.print("\t");
    Serial.print(pow(2, i));
    Serial.print("\t");
    shift_SIPO(pow(2, i));
  }
}

void loop()
{
}

void shift_SIPO(int sequence)
{
  Serial.println(sequence);
}

Result:

1	2.00	2
2	4.00	3
3	8.00	7
4	16.00	15
5	32.00	31
6	64.00	63
7	128.00	127
8	256.00	255
9	512.00	511
10	1024.00	1023
11	2048.00	2047
12	4096.00	4095
13	8192.00	8192
14	16383.99	16383
15	32767.98	32767

The better approach is to use an unsigned variable, set it to 1 and shift that

void setup()
{
  Serial.begin(115200);
  uint16_t x = 1;
  for (int i = 1; i < 16; i++)
  {
    x <<= 1;
    Serial.print(i);
    Serial.print("\t");
    Serial.print(x);
    Serial.print("\t");
    shift_SIPO(x);
  }
}

void loop()
{
}

void shift_SIPO(int sequence)
{
  Serial.println(sequence);
}

Result:

1	2	2
2	4	4
3	8	8
4	16	16
5	32	32
6	64	64
7	128	128
8	256	256
9	512	512
10	1024	1024
11	2048	2048
12	4096	4096
13	8192	8192
14	16384	16384
15	32768	-32768

Note the negative number; reason is that you tell shift_SIPO that the number is an int, not an unsigned int. It should not affect what is written out.

1 Like

Don't forget to put some light (10k?) pull-up or pull-down resistors on your 74HC165 input pins. You don't want to accept a broken wire because the input pin happened to float to the expected value.

1 Like

You didn't post the entire sketch.

1 Like

It would save you from the manipulations involving 'pow()'... although you can also fix that in code now.

I know I am making a subtle point, but since you have shift registers both sending and receiving from the matrix, there is no need to ever do shift operations in software.

please see post again, thank you

Hi makers!

Here is my schematic:

I am still trying to learn about this shift register and I'm trying to shift 11111111 to the register and this should light up all the LEDs. However, only 7 of them lit up and Q0 doesn't seem to have any output voltage, sometimes that troublesome LEDs flickers very dimly and sometimes it doesn't flicker at all.

I have done research and looked into varies posts regarding this and majority of them suggested to add in a decouple capacitor between the Vcc and GND on the breadboard. I have added in a 10uF capacitor and it doesn't seem to do anything....I am lost and don't know where I should look for answers.

Here is my code

void loop() {
   reset_SIPO_register();
   shift_SIPO(0xFF));
  
}

void reset_SIPO_register() {
  digitalWrite(reset_pin, LOW);
  digitalWrite(reset_pin, HIGH);
}

void shift_SIPO(int sequence) {
  digitalWrite(latch_pin, LOW);
  shiftOut(data_pin, clock_pin, LSBFIRST, sequence);
  digitalWrite(latch_pin, HIGH);
}

Can the experts provide clues as to what is happening and where I should go from here?

Thank you!

Swap the LED with a working one to see if it may be the LED?

Yup, did that and it is the same, I've used an ohmmeter to read the voltage on Q0 and it is <1Vdc

Not all of it. Post it all so that we can see, for example, what latch_pin is declared as.
Incidentally, red is an odd choice of colour for a ground wire.

Pin 16 on the shift register is Vcc but following the Wire back to the Arduino, it lands on a GND pin. You have to clean up the schematic.

You can't measured voltage with an ohmmeter, only resistance. You need a volt meter for voltage.

It won't, you need a 0.1uF ceramic capacitor as physically close to the shift register power pins as possible. Plug the wires into the breadboard next to the shift register power pins. You can leave the 10uF capacitor where it is.

If there is a problem in code then it's in the bit you didn't post. I note there doesn't seem to be anything to stop the data being sent thousands of times per second.

[Edit]
I don't know where

  shiftOut(data_pin, clock_pin, LSBFIRST, sequence);

is defined, it's missing from your code and might be the cause of your problem. Is it an inbuilt function somewhere? As you are trying to learn about shift registers I suggest you forget about shiftOut() and write your own function to do the shifting, you'll learn a lot more.

@wingsuncheung2609

I only noticed this after I replied, I found you have another topic on the same subject.

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

I thought those two are different questions, this one is asking why Q0 has no input given I have done everything correctly. The another one was asking about the code, how to shift binary numbers around. thank you

Such questions may be unrelated when a system is working. When a problem exists, multiple facets often manifest themselves in multiple questions. Solving one, solves the other. Also with a non-working project, maximum context is provided by maximum information, which is best achieved by having it all in one thread.