CD4021 not shifting all 8 bits

Hiya,
I was working on getting the ShiftIn tutorial to work from this page: http://www.arduino.cc/en/Tutorial/ShiftIn
My electronics are set up exactly as the tutorial says (i've attached a picture as well).

Everything seems to work, except for the PI-8 pin input doesn't come through the serial. Pins 1-7 work as expected -- Pin 8 doesn't register anything through the serial.
As for general troubleshooting, I took the input from one of the other switches and attached it to the switch being used for Pin 8 and it does work -- so it doesn't appear to be the wiring or the switch itself.

I can't figure out what I'm missing -- any assistance would be greatly appreciated.
You can find a pic of my setup and the code I've been using to debug with below:

int latchPin = 9;
int dataPin = 8;
int clockPin = 10;

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

  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

}

void loop() {
  digitalWrite(latchPin,1);
  digitalWrite(latchPin,0);
  Serial.println(shiftIn(dataPin, clockPin, MSBFIRST), BIN);
}

For one thing, are you certain about the IC's pin_8 and pin_16?
Pin_8 should go to GND and pin_16 should go to 5V.
Looks like IC pin_8 is connected 5V

Hi, I should have clarified the pin numbers.
When I say pin 8 above, I am referring to PI-8 on the TI datasheet for my chip, found here: http://arduino.cc/en/uploads/Tutorial/TI_CD4021.pdf

In terms of the actual pinning numbers the pin that isn't responding properly is actual pin 1 -- referred to as PI-8 on the datasheet.
Thanks!

I had to toy with the contrast, putting your pic through a little processing, and now I can see that you have the power pins right.

Haha. Thank you, iPhone. (Sorry about that).
Any other ideas on why this pin isn't showing up in the serial input?

Serial.println(shiftIn(dataPin, clockPin, MSBFIRST), BIN);

Since it's a "tutorial" then it ought to be right. Right? (:

The first register's output should be present on "Q8" when it gets latched.
But if it got latched, clocked (where all the bits advance/shift over) and then read, it seems that the first bit would be lost.

Somebody else, last week, was writing about just this same thing, losing a bit/input like you are.

If I had one around I'd write my own routine, raising the clock_pin as necessary and digitalRead Q8 each time, and "serial monitor" the result.

Hmm...so you think the issue is that I'm not able to "catch" the last bit before it gets shifted out?
It's a good thought, I'll play with some things when I get home.

The actual tutorial uses a manmade shiftIn function (seen at the bottom here: http://arduino.cc/en/Tutorial/ShftIn11). I made the assumption that this tutorial was created prior to 'shiftIn' being included in the core arduino libs. I'll try to use this function as well to see what I can find out.

Thanks!

JCB,
I got hold of a 4021.
I wrote the following demo and verified same.
It prints out the status of the register, via "Serial Monitor", as a binary number.
[I don't know how to do that with 'leading zeros'.]
Works good for me!
Note, I used different pins and aliases, nothing cryptic.
Let me know how you get on.

//
//        Demo4021
//         
//        A   --1    16--   +
//        x   --2    15--   B
//        SO  --3    14--   C
//        E   --4    13--   D
//        F   --5    12--   x
//        G   --6    11--   +
//        H   --7    10--   clk
//      gnd   --8     9--   load

int loadpin = 10;
int clockpin = 11;
int SOpin = 12;
int registerContent = 0;
int bitContent = 0;

void setup ()
{
  pinMode (loadpin, OUTPUT);
  pinMode (clockpin, OUTPUT);
  pinMode (SOpin, INPUT);
  digitalWrite (loadpin, LOW);
  digitalWrite (clockpin, HIGH);
  Serial.begin(9600);
}

void loop ()
{
  for (int idx = 0; idx < 8; idx++)
  {
    if (idx == 0)
    {
      pulseload();
    }
    bitContent = digitalRead (SOpin);
    if (bitContent == 1)
    {
      bitWrite (registerContent,idx,1);
    }
    else
    {
      bitWrite (registerContent,idx,0);
    }
    pulseclock();
  }
  Serial.print (registerContent, BIN);
  Serial.print (byte(10));
  registerContent = 0;
  delay (1000);
}

void pulseload ()
{
  digitalWrite (loadpin, HIGH);
  delay (1);
  digitalWrite (loadpin, LOW);
}

void pulseclock ()
{
  digitalWrite (clockpin, LOW);
  delay (1);
  digitalWrite (clockpin, HIGH);
}
1 Like

Wow! Thanks for your help.
I know you probably hear this every day, but my house was struck by lightning a few days ago, so I've been dealing with the aftermath and haven't had time to sit down with this project.

I hope to find some time this weekend to work with this again. Especially since you spent the time to get some working code together.
Thanks again for all your efforts, I'll let you know how I make out.

Cheers!

FINALLY had a chance to jump back into this project this weekend. Sure enough, your code worked. I went back and also tested the shiftIn function that was in the examples and that worked as well.
So, it seems entirely possibly that the shiftIn function that ships with Arduino core has a bug in it (clocking too quick before the last bit gets shifted).

Thanks again for all your help.
Jason

hello everybody,
I have the same problem. first bit is not read. I am using a 4021B. Can you help me please?
thanks in advance, Marco