HC165N with Esp8266

Hello everyone!

I have a problem with getting data from HC165N to my esp8266.
Currently I am using this code:

// 74HC165 shift register with ESP8266 (NodeMCU)
// Reading 8 digital inputs using shiftIn

const byte LATCH = D0;   // GPIO16 (Load / PL) - Not great choice, but will work for basic use
const byte DATA  = D6;   // GPIO12 (Q7 / QH)
const byte CLOCK = D5;   // GPIO14 (CP / Clock)

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

  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, INPUT);

  digitalWrite(CLOCK, HIGH);
  digitalWrite(LATCH, HIGH);
}

void loop() {
  byte buttonState;

  // Load parallel inputs into shift register
  digitalWrite(LATCH, LOW);
  delayMicroseconds(5);
  digitalWrite(LATCH, HIGH);

  // Read 8 bits from the register
  buttonState = shiftIn(DATA, CLOCK, MSBFIRST);

  // Print button states (active LOW = 1 when not pressed, 0 when pressed)
  for (int i = 0; i < 8; i++) {
    bool pressed = !(buttonState & (1 << i)); // Invert logic
    Serial.print(pressed ? "1" : "0");         // 1 = pressed
  }

  Serial.println();
  delay(200); // Read every 200 ms
}

I don't think that I made a mistake while wiring, anyways here is the photo



and I should also mention that I want to use only 3 pins on esp8266 to receive data (if it is really possible).
Output (While pressing one of those buttons)

I found a tutorial in which a guy used 4 pins on Arduino, that worked for me also, however it stopped working when I switched pins from Arduino Uno to esp8266(

I hope someone can help me

Floating inputs on a CMOS chip? That is not going to behave as you expect nor will it end well.

1 Like

Should I use the 100nF Capacitor?

Why do you think that would help at all?

1 Like

Do not look at the top part of the breadboard. (There are only some rgb leds and HC595). The HC165N receives either LOW or HIGH values after clicking the button. I mean there is not any floating value.

As far as I understand CMOS chip - contains a semiconductor element that makes chip itself more reliable and efficient

And how you understand that my shift register contains CMOS chip? Sorry if my questions feels dumb🫠 and what should I use instead?

  • Remove extraneous components from the breadboard.
  • Your resistors appear to be 1k not 10k.
  • The schematic doesn’t seem to match the breadboard switch wiring.
  • Did that sketch produce that Serial output ?
  • Use a 100nF ceramic de-coupling capacitor.
  • Supply us with an all encompassing schematic.
  • Is the register being powered by 5v or 3V3 ?
1 Like

I can read the chip identifier: 74HC165. The HC in 74HC165 stands for High Speed CMOS.

Your assertion is not supported by reality.

Let's review.


Pins 3, 4, 5 and 6 on the 74HC165 IC are inputs.

Pins 3, 4, 5 and 6 on the 74HC165 IC are not connected to anything.

Therefore pins 3, 4, 5 and 6 are floating.

CMOS inputs should never be left floating. Doing so invites all manner of problems. As mentioned in the datasheet for the IC:

(1) All unused inputs of the device must be held at VCC or GND to ensure proper device operation. Refer to the TI application report, Implications of Slow or Floating CMOS Inputs, literature number SCBA004.

And with that I will wish you good luck with your project and say good-bye.

1 Like

The first thing I noted is that you power the chip from 5volt.
The ESP is a 3.3volt-logic processor, so power the chip and pull up resistors from the 3.3volt pin of the ESP. @LarryD already noted that.
Leo..

1 Like

Everything is working right now! @Wawa , @van_der_decken and @LarryD Thank you so much :blue_heart:. For those who have faced the same problem (Connecting SN74HC165N with Esp8266):

  1. I cleared my breadboard from unnecessary components.
  2. Measured the resistance of each resistor, it should be 10k oms (everything was fine in my case).
  3. Reconnect shift register pins with microcontroller. Here is the schematics

  4. Powered Buttons and Shift register with 3.3v.
  5. Connected every unused pin at shift register to GND.
  6. Uploaded the code:
// 74HC165 shift register with ESP8266 (NodeMCU)
// Reading 8 digital inputs using shiftIn

const byte LATCH = D0;   // GPIO16 (Load / PL) - Not great choice, but will work for basic use
const byte DATA  = D6;   // GPIO12 (Q7 / QH)
const byte CLOCK = D5;   // GPIO14 (CP / Clock)

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

  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, INPUT);

  digitalWrite(CLOCK, HIGH);
  digitalWrite(LATCH, HIGH);
}

void loop() {
  byte buttonState;

  // Load parallel inputs into shift register
  digitalWrite(LATCH, LOW);
  delayMicroseconds(5);
  digitalWrite(LATCH, HIGH);

  // Read 8 bits from the register
  buttonState = shiftIn(DATA, CLOCK, MSBFIRST);

  // Print button states (active LOW = 1 when not pressed, 0 when pressed)
  for (int i = 0; i < 8; i++) {
    bool pressed = !(buttonState & (1 << i)); // Invert logic
    Serial.print(pressed ? "1" : "0");         // 1 = pressed
  }

  Serial.println();
  delay(200); // Read every 200 ms
}

  1. Output result (pressing second and the last buttons)

Thanks to everyone. I hope this will be helpful other beginners too!