Question / Problem with ShiftIn register


For my project I tried to built a ShiftIn register with 8 pushbuttons. I use the 4021BE modul.

I took the following code from the tutorial:

//define where your pins are
int latchPin = A2; //gruen
int dataPin = A3; // gelb
int clockPin = A4; // blau

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
byte switchVar1 = 72;  //01001000

//define an array that corresponds to values for each 
//of the shift register's pins
char note2sing[] = {
  'C', 'd', 'e', 'f', 'g', 'a', 'b', 'c'}; 

void setup() {
  //start serial

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);


void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  //set it to 1 to collect parallel data, wait
  //set it to 0 to transmit data serially  

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar1 = shiftIn(dataPin, clockPin);

  //Print out the results.
  //leading 0's at the top of the byte 
  //(7, 6, 5, etc) will be dropped before 
  //the first pin that has a high input
  Serial.println(switchVar1, BIN);

  //This for-loop steps through the byte
  //bit by bit which holds the shift register data 
  //and if it was high (1) then it prints
  //the corresponding location in the array
  for (int n=0; n<=7; n++)
    //so, when n is 3, it compares the bits
    //in switchVar1 and the binary number 00001000
    //which will only return true if there is a 
    //1 in that bit (ie that pin) from the shift
    if (switchVar1 & (1 << n) ){
      //print the value of the array location

//white space
//delay so all these print satements can keep up. 


//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
    digitalWrite(myClockPin, 0);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;

    //Debuging print statements
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  //debuging print statements whitespace
  //Serial.println(myDataIn, BIN);
  return myDataIn;

Whe I run the code, the following is shown on the serial monitor:


Pressing nothing leads to 0. I suppose this is the correct behavior.

But regardless wich of the putton I press, I always get


I am not sure wether I understood the code but I expected it to show a different letter, depending on which of the eight buttons I was just pressing.

Any help?


I agree with you - the fact that you constantly get "11111111" is not correct.

Most probable is that you have wired something wrongly. So, check it one more time :roll_eyes: Actually, do check it, not just cursory, but check each wire and their pin numbers and tick it off on a hardcopy of your diagram. A couple of times it has happened that although I was willing to swear and write in blood that the circuit was correct ... it wasn't. :blush:


you were right ...
The connection between Pin 8 and GND was missing.
Now it works as wanted: You press one button and one character shows up. You press two buttons ... two characters ...

Thanks for your help.



I am having almost the same problem, as you had, Schwabinger. I checked the wiring a few times, and I am sure that it is correct. I think maybe I am having incorrect shift register, HCF4014BE?
I copied the program from Arduino page, but nothing seems to be working correct. Or I don`t understand program.
I need help, so if you be so kind and help mi a bit, please.
Hope you get this massage