Strange data from two SN74HC165N (daisy chained and get their input from CNY70)

Hi everyone,

I am struggeling with the following setup:

My Arduino Uno gets input from two "SN74HC165N" chips. With those two chips I can read up to 16 inputs. However I do use only 14 of the 16 available inputs (the remaining 2 are connected to ground). I power my Arduino with external power supply.

Each input of both SN74HC165N is connected to an CNY70 optical sensor. The setup itself works fine. Each CNY70 is connected to 5V, GND and two resistors (100 Ohm for the infrared emitter and 22k Ohm for the phototransistor)

But here s what is happening: Some of the inputs (its always the same pairs) seem to interact with each other. That means if lets say the 1st input gets fired automatically the 5th gets fired as well. Even if I swap both SN74HC165N the problem persists with the same inputs.

I checked a lot of things but I do not find any short-circuits.

Has anybody ever observed the same thing? Any ideas.

Thanks for your ideas! Musterstrasse

Is the external power supply Gnd connected to Arduino Gnd?

Post your schematic & code.

Do you have good pull-ups on the phototransistors? If not they will have a very high impedance sometimes (if dark) and their signals will be very susceptible to noise pick-up from nearby wiring.

Are your chips decoupled?

Hi CrossRoads , MarkT,

Thanks for your feedback. Here is some more detail:

In my schematic you see that I use 14 inputs out of the 16 available inputs. The strange thing is that 1) some of those inputs interact with each other (i.e. if the first input fires the second also shows HIGH signal). I have 2 of those "pairs" in my schematic. 2) one single input out of those 14 inputs is always giving my a HIGH signal.

Is the external power supply Gnd connected to Arduino Gnd?

I am using an external power adapter to power my Arduino (Voltcraft; 12-27W; 12V, 550mA). It goes directly into Arduino so there is no need to make an additional connection to GND I believe. The board itself where the components are soldered to is connected to 5V and GND directly from Arduino.

My schematic is attached and here is my sketch code (source: http://playground.arduino.cc/Code/ShiftRegSN74HC165N#.UxJDshZshFI)

/*
 * 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   2

/* 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        = 8;  // Connects to Parallel load pin the 165
int clockEnablePin  = 9;  // Connects to Clock Enable pin the 165
int dataPin         = 11; // Connects to the Q7 pin the 165
int clockPin        = 12; // 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()
{
    byte 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)
    {
        display_pin_values();
        oldPinValues = pinValues;
    }

    delay(POLL_DELAY_MSEC);
}

Do you have good pull-ups on the phototransistors?

I use 22k Ohm resistors for the phototransitors.

Are your chips decoupled?

No, I was thinking that could run two of those chips without decoupling capacitors.

Just now I found a post that reports a similar issue http://forum.arduino.cc/index.php?topic=220167.0

Do you have any ideas why I could try?

Thank you very much in advance! Musterstrasse

Schematic.png|1327x1404

Hi Max.

Do not assume you don't need decoupling. Always assume you do need that, that's the way to do things. It's also what is pointed out in that other thread.

And please. Tidy up those schematics, it's all over the place.

QH/ is an output, disconnect them from Gnd. Definitely add 0.1uF decoupling caps to the power supply pins. That looks like Eagle - right click the part, select Invoke, select PWR so you can see where the power pin is. I can't tell from that drawing how the data is making it back to the uC. Too many over lapping signals.