Parallel in shift register with Nano

I have been trying to use 74HC165 to cascade 9 shift registers but then I turn on one switch it sometimes enable random bit in serial monitor or sometimes doesn't even recognise if switch was pressed

Yes that will happen. This is because you have no supply decoupling on those chips. Each chip requires a 0.1uF ceramic capacitor connected between power and ground. As well as something like a 100uF capacitor between power and ground at the start of the chain.

Full explanation here:-
Power Supply Decoupling Tutorial

Also what synchronises your pushing the switch with you starting to gather the information from your shift registers? You can't have them running all the time.

1 Like
#define NUMBER_OF_SHIFT_CHIPS 9 // Set the number of 74HC165 chips
#define DATA_WIDTH (NUMBER_OF_SHIFT_CHIPS * 8)

int LoadPin = 7;
int EnablePin = 4;
int DataPin = 5;
int ClockPin = 6;

// Array to hold the values from shift registers, two longs can store up to 64 bits, so we need three for up to 72 bits
unsigned long pinValues[3];  
unsigned long oldPinValues[3];

void setup()
{
    Serial.begin(9600);

    pinMode(LoadPin, OUTPUT);
    pinMode(EnablePin, OUTPUT);
    pinMode(ClockPin, OUTPUT);
    pinMode(DataPin, INPUT);

    digitalWrite(ClockPin, LOW);
    digitalWrite(LoadPin, HIGH);
    digitalWrite(EnablePin, HIGH);  // Ensure the chip is enabled only when reading

    read_shift_regs(pinValues);
    print_byte();
    memcpy(oldPinValues, pinValues, sizeof(pinValues));
}

void loop()
{
    read_shift_regs(pinValues);

    if (memcmp(pinValues, oldPinValues, sizeof(pinValues)) != 0)
    {
        print_byte();
        memcpy(oldPinValues, pinValues, sizeof(pinValues));
    }
}

void read_shift_regs(unsigned long values[3]) {
    memset(values, 0, sizeof(unsigned long) * 3);  // Clear previous values

    digitalWrite(EnablePin, HIGH);
    digitalWrite(LoadPin, LOW);
    delayMicroseconds(5);
    digitalWrite(LoadPin, HIGH);
    digitalWrite(EnablePin, LOW);

    // Read serial data from shift register
    for (int i = 0; i < DATA_WIDTH; i++) {
        int arrayIndex = (i / 32);  // Calculate the array index
        int bitPosition = (DATA_WIDTH - 1 - i) % 32; // Calculate the bit position

        // Pulse clock to shift data
        digitalWrite(ClockPin, HIGH);
        delayMicroseconds(5);
        digitalWrite(ClockPin, LOW);

        // Read bit from DataPin and place it in the correct position
        if (digitalRead(DataPin)) {
            values[arrayIndex] |= (1UL << bitPosition);
        }
    }
}


void print_byte() {
    Serial.println("Shift Register Values:");
    for (int i = 0; i < DATA_WIDTH; i++) {
        int arrayIndex = i / 32;
        int bitPosition = i % 32;
        Serial.print((pinValues[arrayIndex] >> (31 - bitPosition)) & 1, BIN);
        Serial.print("  ");
    }
    Serial.println("\n");
}

Even after capacitor decoupling same issue persisting.

You can shift into several port pins and make it less prone to noise after you properly decouple it. Follow @Grumpy_Mike link.

using multiple pins for each shift register wont solve the original problem

And you know this because?

I have a code that i build to use with that ic but i never had the chance to properly use it .I used it with daisy chain connection if you want i can provide you my work

It would be great help I will test it out

Because I tried it for different pins and this problem doesn't arises also this problem is there for Daisy chained registers

ShiftIN :smile:
I used to pins in ascending or descending order i clearly don't remember . Check which pin you see high and set the pin number to check .Ascending and descending order that i mean was i don't remember from which pin 1 starts either from 1st IC or the last IC in the daisy chain

Look at my answer I did not say it would solve the problem. I also stated to try that after you does your suggestion which will solve the problem.

That comment was directed towards @mayuresh_2110 post #8

1 Like

Which pin is this:
image

It's QH bar

OK, thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.