Shift in issues with 74HC165

Hi all,
I’m building a piggyback board that extends input and out put pins on arduino uno. Got my 74HC565s working nicely. code might be a bit clunky but i like it that way - i can see whats going on.
I’m trying to get some code to work that I found in this forum, i’ve modified it a bit but its not giing me an expected output in the serial monitor.
I’ve pulled the inputs to the 165 to ground and have buttons that send +5v to the inputs of the 165.

when any of the buttons are pressed the serial monitor changes from 00000000 to 11111111. i’d have expected the serial monitor to put 1’s depending n which input to the 165 went high.

any thoughts???

Many thanks

(P.S. fantastic forum!)

int inclearPin = 11; // enablepin  74HC165pin 15
int indataPin = 9;  //74HC165 pin 7
int inclockPin = 10; //74HC165 pin 2 
int inloadPin = 12; // toggling this tells the 165 to read the value into its memory for reading 74HC165pin1

int temp = 0;

void setup() {
  //start serial

  pinMode(inclearPin, OUTPUT); 
  digitalWrite(inclearPin, 0); // enable input, you could also tie this pin to GND
  pinMode(indataPin, INPUT);
  pinMode(inclockPin, OUTPUT);  
  pinMode(inloadPin, OUTPUT);


void loop() {
  digitalWrite(inloadPin, 0); // read into register (tells the 165 to take a snapshot of its input pins)
  digitalWrite(inloadPin, 1); // done reading into register, ready for us to read    

  for(int i=0; i<=7; i++){ // read each of the 165's 8 inputs (or its snapshot of it rather)
  // tell the 165 to send the inputs pin state
  digitalWrite(inclockPin, 0);
  // read the current output
  temp = digitalRead(indataPin); // read the state
  // tell the 165 we are done reading
  digitalWrite(inclockPin, 1);

  Serial.print (temp);  
  Serial.println ("");
  Serial.println ("--------");
any thoughts???

You may want to read the datasheet.

Some code for you to try:

unsigned char hc165_read(void) {
  unsigned char mask = 0x80;
  unsigned char tmp = 0;

  do {
    digitalWrite(SCK, 0); //clear sck
    if (digitalRead(SOUT)) tmp |= mask;
    tmp |= 0;
    digital Write(SCK, 1); //set sck
    mask = mask >> 1;
  } while (mask);
  return tmp;

And you should get the loading of the chip right as well.

Hi dhenri,

I can't see what the problem is with the OP s code ( if he just wants to check switches with a line of 1s and 0s )

I am also a newbie, and cant see what he missed in the data sheet .

I have used this chip to generate a video overlay and seem to recall I just clocked out one bit at a time...

Thanks for the replies, I have read the datasheet, ( this was my interpretation of where I thought the pins should be in the comments at the top.
I’ll take it apart and put it back together and try out your code too.

Not sure i really understand your code example? does the 'do' section sit inside or outside the loop in my code?