Shift In Help [SOLVED]

Hello Everyone,

I am trying to figure out how to use Shift In. I am using a CD4021B chip on a Mega2560. I have everything hooked up and it appears to be 'somewhat' working. The problem is that one of my 8 buttons will not trigger a HIGH reading on the chip.

The button in question is connected to Pin 8 on the chip. When I press it, nothing happens. My byte reads "00000000". When I press the button next to it, Pin 7, my byte reads "10000001". All the rest of the buttons seem to work correctly except for the last button.

My board is wired exactly like the example on the shiftIn tutorial.

In this project, I would like to read from the shift in register and write the bits into the "masterArr" array.

My Code:

//shiftOut Pins
int outClockPin = 45;
int outLatchPin = 46;
int outDataPin = 47;
//shiftIn Pins
int inLatchPin = 42;
int inDataPin = 43;
int inClockPin = 44;

byte masterArr[8] = {0, 0, 0, 0, 0, 1, 1, 1};

// shiftIn Var
byte shiftInData = 159;

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

  //shiftOut setup
  pinMode(outClockPin, OUTPUT);
  pinMode(outLatchPin, OUTPUT);
  pinMode(outDataPin, OUTPUT);
  //shiftIn setup
  pinMode(inLatchPin, OUTPUT);
  pinMode(inClockPin, OUTPUT);
  pinMode(inDataPin, INPUT);
}

void loop() {
  getShift();
  sendShift();
  delay(2000);
}

void sendShift(){
  byte alpha;
  for(int j = 0; j < 8; j++) {
    bitWrite(alpha, j, masterArr[j]);
  }
  Serial.print("out to shift = ");
  Serial.println(alpha);
  Serial.println("............................");

  digitalWrite(outLatchPin, 0);
  shiftOut(outDataPin, outClockPin, LSBFIRST, alpha);
  digitalWrite(outLatchPin, 1);
}

void getShift(){
  digitalWrite(inLatchPin,1);
  delay(20);  
  digitalWrite(inLatchPin,0);

  shiftInData = shiftIn(inDataPin, inClockPin, MSBFIRST);

  for(int i = 0; i <= 7; i++){
    int x = bitRead(shiftInData, i);
    masterArr[i] = x;
    Serial.println(x);
  }
  

  Serial.print("in from shift = ");
  Serial.println(shiftInData, BIN);
  Serial.println("............................");
}

Thank you in advance for any help you can give!

Here are some images of the wiring. I followed the example as best I could and double-checked the wiring against it a few times. Not sure how I can get better images of the wiring - it's a bit of a rat's nest...
https://drive.google.com/open?id=1C-sQMoyTAhn-6tdHQEd1XR9T1aE-wTNV
https://drive.google.com/open?id=1hXIQcsVVrokTjFoC23FhasJV_4a5VwI_
https://drive.google.com/open?id=19onCubxwpxuj0Hk0aLIWlajnBwY2Aquu

Show us a good image of the actual wiring.

bkirkby:
The button in question is connected to Pin 8 on the chip. When I press it, nothing happens. My byte reads "00000000". When I press the button next to it, Pin 7, my byte reads "10000001". All the rest of the buttons seem to work correctly except for the last button.

My board is wired exactly like the example on the shiftIn tutorial.

Then SOMETHING is wrong because the shiftIn() tutorial shows no button connected to Pin 8 on the shift register. Pin 8 is the Vss (GROUND) pin.
The buttons in the schematic are connected to pins 1, 15, 14, 13, 4, 5, 6, and 7.

Then SOMETHING is wrong because the shiftIn() tutorial shows no button connected to Pin 8 on the shift register. Pin 8 is the Vss (GROUND) pin.

My apologies for not clarifying. In the tutorial, there is a reference to "P8" and also "PI-8" which would be Pin 1 on the chip(starting from top, left corner in birds-eye view - with the dimple being at the top of the chip).

Did you include the pull down resistors?

No wiring image = poor help.

Here is a fritzing of my wiring for my CD4021BE. Ignore the part number on the chip in the image. The system is running 5v.

IMAGE

This is not your actual wiring.

Not going to ask any more, good luck with this and future projects.

larryd:
Did you include the pull down resistors?

No wiring image = poor help.

I did include pull down resistors and I did include links to 3 images that show the wiring.

larryd:

This is not your actual wiring.

Not going to ask any more, good luck with this and future projects.

larryd - there are 3 links in the top post to three images of my actual wiring. I realize they are links and not actual images, but if you click on them, they do go to images.

Always post your images here as some PDAs cannot access your images.

Please confirm the input to the I.C. goes from 0 to 5 volts when the corresponding switch is closed.

Add a decoupling .1uF ceramic capacitor from the I.C.'s power pins to ground.

The input to the I.C. does go to 5v when the button is pressed. During some debugging, I even connected it directly to the 5v rail on the breadboard to make sure.

Just to be sure what you meant, I added the capacitor between Pin 16(VDD) and ground. Do I need to add a capacitor anywhere else?

Thanks for your help!!

LarryD - This is a total newbie question, but how where you able to get the images to show directly in this forum?

Posting images:

https://forum.arduino.cc/index.php?topic=519037.0

For each I.C. in your cct., add one .1uF from VDD to GND (as close to the pins as possible).

Please confirm the input to the I.C. goes from 0 to 5 volts when the corresponding switch is closed (for each switch).

All the inputs into the I.C. do go from 0 to 5v. In my current project, I have the CD4021BE as well as a 74HC595. When I push any of the buttons other than the one connected to Pin 1 on the I.C., the corresponding LED connected to the 74HC595 lights up.

I know this is not an issue with the 74HC595 because I can send it a byte of "11111111" and every single LED will light up.

I don't think this is a wiring issue due to the fact that when I push the button connected to Pin 1, I do not see a "1" show up at that spot in my Serial monitor, which is reading all bits in the byte from the CD4021. When I push any of the other buttons connected to the CD4021, I do get a "1" in the byte readout. With the exception of the button connected to pin 15, which when pressed, gives me a byte read out of "10000001" when it should be giving me a readout of "00000010" instead.

This is why I believe it is a software issue, not a hardware issue.

It seems as if the first, or last, bit I read from the CD4021 is always a "0" - or that the order that the bits are calculated is shifted by one. i.e. "0,1,2,4,8,16,32,64" instead of "1,2,4,8,16,32,64,128".

Have you tried the example code from the tutorial to see if your wiring responds as it should?

Added a line to your code, refer to the arrow in the code <------<<<<

I do not have a CD4021 to test this code change, what happens when you try?

//shiftOut Pins
int outClockPin = 45;
int outLatchPin = 46;
int outDataPin = 47;
//shiftIn Pins
int inLatchPin = 42;
int inDataPin = 43;
int inClockPin = 44;

byte masterArr[8] = {0, 0, 0, 0, 0, 1, 1, 1};

// shiftIn Var
byte shiftInData = 159;

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

  //shiftOut setup
  pinMode(outClockPin, OUTPUT);
  pinMode(outLatchPin, OUTPUT);
  pinMode(outDataPin, OUTPUT);
  //shiftIn setup
  pinMode(inLatchPin, OUTPUT);
  pinMode(inClockPin, OUTPUT);
  pinMode(inDataPin, INPUT);
}

void loop() {
  getShift();
  sendShift();
  delay(2000);
}

void sendShift(){
  byte alpha;
  for(int j = 0; j < 8; j++) {
    bitWrite(alpha, j, masterArr[j]);
  }
  Serial.print("out to shift = ");
  Serial.println(alpha);
  Serial.println("............................");

  digitalWrite(outLatchPin, 0);
  shiftOut(outDataPin, outClockPin, LSBFIRST, alpha);
  digitalWrite(outLatchPin, 1);
}

void getShift(){

  digitalWrite(inLatchPin, 1);
  delay(20);
  digitalWrite(inClockPin, HIGH);   //Set clock high to catch first bit.  <-----<<<<< line added
  digitalWrite(inLatchPin, 0);

  //digitalWrite(inLatchPin,1);
  //delay(20);  
  //digitalWrite(inLatchPin,0);

  shiftInData = shiftIn(inDataPin, inClockPin, MSBFIRST);

  for(int i = 0; i <= 7; i++){
    int x = bitRead(shiftInData, i);
    masterArr[i] = x;
    Serial.println(x);
  }
  

  Serial.print("in from shift = ");
  Serial.println(shiftInData, BIN);
  Serial.println("............................");
}

BTW
Alternatives:
A 74HC165 parallel to serial
and
a 74HC595 serial to parallel

Use SPI with above.

larryd:
Added a line to your code, refer to the arrow in the code <------<<<<

larryD! Your fix worked!! Thank you so much for the help!!!

I understand what the code is taking the clock high, but I can't figure out why that solved the problem. What led you to this fix?

Thanks again!