SN74HC165N Multiplexing

Good evening, I'm following the example ( Arduino Playground - HomePage ) for demultiplexing 8 buttons with a 165 chip.
Works like a charm except for a little weird issue:

Pin States:
Pin-0: LOW
Pin-1: LOW
Pin-2: LOW
Pin-3: LOW
Pin-4: LOW
Pin-5: LOW
Pin-6: LOW
Pin-7: HIGH

The last bit is always inverted, If I press that button then it goes to LOW.
Tried switching the poles and replacing the button but nothing changes, so I guess it is something in the code but can't figure what. :frowning:

Let's see a picture of your wiring.

I agree with LarryD. It will be an electrical thing.

Meanwhile, my page about this chip:

Here it is, the 8 parallel inputs are wired exactly as the A one

Can you write pin numbers on those please? And state which Arduino pins the "digital" lines go to.

Thank you, I just added that information

What about SH, CL, CL/INH, QH?

I am trying to check it is wired correctly.

Can you post your exact sketch please? For example, do you have this?

#define NUMBER_OF_SHIFT_CHIPS   2

If so, do you have 2 chips?

Just one chip so I set that one to 1, basically I modified only this line and those for arduino pins
Also added missing pins in the image

/*
 * SN74HC165N_shift_reg
 *
 * Program to shift in the bit values from a SN74HC165N 8-bit
 * parallel-in/serial-out shift register.
 *
 * This sketch demonstrates reading in 16 digital states from a
 * pair of daisy-chained SN74HC165N shift registers while using
 * only 4 digital pins on the Arduino.
 *
 * You can daisy-chain these chips by connecting the serial-out
 * (Q7 pin) on one shift register to the serial-in (Ds pin) of
 * the other.
 * 
 * Of course you can daisy chain as many as you like while still
 * using only 4 Arduino pins (though you would have to process
 * them 4 at a time into separate unsigned long variables).
 * 
*/

/* How many shift register chips are daisy-chained.
*/
#define NUMBER_OF_SHIFT_CHIPS  1

/* Width of data (how many ext lines).
*/
#define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8

/* Width of pulse to trigger the shift register to read and latch.
*/
#define PULSE_WIDTH_USEC   5

/* Optional delay between shift register reads.
*/
#define POLL_DELAY_MSEC   1

/* You will need to change the "int" to "long" If the
 * NUMBER_OF_SHIFT_CHIPS is higher than 2.
*/
#define BYTES_VAL_T unsigned int

int ploadPin        = 12;  // Connects to Parallel load pin the 165
int clockEnablePin  = 10;  // Connects to Clock Enable pin the 165
int dataPin         = 11; // Connects to the Q7 pin the 165
int clockPin        = 10; // Connects to the Clock pin the 165

BYTES_VAL_T pinValues;
BYTES_VAL_T oldPinValues;

/* This function is essentially a "shift-in" routine reading the
 * serial Data from the shift register chips and representing
 * the state of those pins in an unsigned integer (or long).
*/
BYTES_VAL_T read_shift_regs()
{
    long bitVal;
    BYTES_VAL_T bytesVal = 0;

    /* Trigger a parallel Load to latch the state of the data lines,
    */
    digitalWrite(clockEnablePin, HIGH);
    digitalWrite(ploadPin, LOW);
    delayMicroseconds(PULSE_WIDTH_USEC);
    digitalWrite(ploadPin, HIGH);
    digitalWrite(clockEnablePin, LOW);

    /* Loop to read each bit value from the serial out line
     * of the SN74HC165N.
    */
    for(int i = 0; i < DATA_WIDTH; i++)
    {
        bitVal = digitalRead(dataPin);

        /* Set the corresponding bit in bytesVal.
        */
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

        /* Pulse the Clock (rising edge shifts the next bit).
        */
        digitalWrite(clockPin, HIGH);
        delayMicroseconds(PULSE_WIDTH_USEC);
        digitalWrite(clockPin, LOW);
    }

    return(bytesVal);
}

/* Dump the list of zones along with their current status.
*/
void display_pin_values()
{
    Serial.print("Pin States:\r\n");

    for(int i = 0; i < DATA_WIDTH; i++)
    {
        Serial.print("  Pin-");
        Serial.print(i);
        Serial.print(": ");

        if((pinValues >> i) & 1)
            Serial.print("HIGH");
        else
            Serial.print("LOW");

        Serial.print("\r\n");
    }

    Serial.print("\r\n");
}

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

    /* Initialize our digital pins...
    */
    pinMode(ploadPin, OUTPUT);
    pinMode(clockEnablePin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, INPUT);

    digitalWrite(clockPin, LOW);
    digitalWrite(ploadPin, HIGH);

    /* Read in and display the pin states at startup.
    */
    pinValues = read_shift_regs();
    display_pin_values();
    oldPinValues = pinValues;
}

void loop()
{
    /* Read the state of all zones.
    */
    pinValues = read_shift_regs();

    /* If there was a chage in state, display which ones changed.
    */
    if(pinValues != oldPinValues)
    {
        Serial.print("*Pin value change detected*\r\n");
        display_pin_values();
        oldPinValues = pinValues;
    }

    delay(POLL_DELAY_MSEC);
}

Why is CL/INH tied to CL? I had CL/INH grounded permanently.

Can you try my sketch please?

You will need slight wiring changes as shown in the sketch comments.

// Chip pin 1 (/PL)  goes to LATCH (D9)
// Chip pin 2 (CP)   goes to SCK   (D13)
// Chip pin 9 (Q7)   goes to MISO  (D12)

Or you can use the bit-banged version and adjust the pin constants:

// Chip pin 1 (/PL)  goes to LATCH (D8)
// Chip pin 2 (CP)   goes to SCK   (D7)
// Chip pin 9 (Q7)   goes to MISO  (D6)

#include <bitBangedSPI.h>

bitBangedSPI bbSPI (bitBangedSPI::NO_PIN, 6, 7);  // MOSI, MISO, SCK

const byte LATCH = 8;

In your case with your current wiring it would be:

bitBangedSPI bbSPI (bitBangedSPI::NO_PIN, 11, 10);  // MOSI, MISO, SCK

const byte LATCH = 12;

The bit-banged library is at: http://www.gammon.com.au/Arduino/bitBangedSPI.zip

Thank you, I'll try it as soon as I can, my board stopped working ATM :frowning:
http://forum.arduino.cc/index.php?topic=271681.msg1914857

If it's a Mega, better stick to my bit-banged version unless you adjust the pins. The hardware SPI pins on the Mega are different from the Uno.

Same result with your sketch Nick, so I guess the problem is somewhere in the electronic, I'll double check it.

Can't find anything wrong in the wiring.
I will try replacing the chip when I can get another one.

Added a 2nd daisy chained 165, 6 of the 8 pins there are HIGH by default.
I don't get it :0

By default do you mean with nothing connected? Without anything connected to a pin you can get any value but most likely you will get a high.

Can you post a photo showing your wiring, not more than 1000 pixels wide.

Grumpy_Mike:
By default do you mean with nothing connected? Without anything connected to a pin you can get any value but most likely you will get a high.

No, I mean with the buttons not being pressed.
All the pins have a button/switch with a pull-up resistor.

Grumpy_Mike:
Can you post a photo showing your wiring, not more than 1000 pixels wide.

I really doubt I can get something useful with the 320p of my phone.

No, I mean with the buttons not being pressed.
All the pins have a button/switch with a pull-up resistor.

Then in that case the default input is high. You have to worry about the two that are not.

I really doubt I can get something useful with the 320p of my phone.

It is hard to help in that case.

The way you drew your wiring they would be high by default. I'm not sure what else you are expecting.

You have 8 x 4.7k resistors, right? And each one is connected between the pin and 5V? And the switch is connected between the pin and Gnd? And the switches work? Try measuring the voltage on each pin and confirm whether or not it is what the code reports.

Yes what code reports is right, the pins HIGH have 5V and LOW ones nothing.

Grumpy_Mike:
Then in that case the default input is high.

I see, I suppose I should use pull down resistors instead.