ShiftIn Registers - CD4021BE

£ 3.20 - £7.90


Select Units

Here we have the CD4021BE shift-In register. These components are perfect for extending the number of digital input pins on your Arduino, Pic, or any other microcontroller.

Using a parallel to serial shift register allows you collect information from 8 or more switches while only using 3 of the pins on your microcontroller.

  • Manufacturer: Texas Instruments
  • Product Category: Shift Register
  • RoHS: yes
  • Number of Bits per Element: 8
  • Package/Case: DIP-16
  • Maximum Operating Temperature: + 125 C
  • Minimum Operating Temperature: - 55 C
  • Packaging: Tube
  • Function: Parallel or Serial to Serial
  • Mounting Style: Through Hole
  • Operating Supply Voltage: 3 V to 18 V

ShiftIn Registers - CD4021BE

These 8-Stage static shift registers take a fair amount of wiring and and a few extra components, but once you have the circuit all wired up, the extra input pins you immediately have on your Arduino make up for it.

Here is an example of the completed circuit. For a full tutorial and a fantastic explanation please head over to the Arduino site.

  • Shift Register
  • 8 x Switches
  • 8 x Resistors (10 kohm)
  • Breadboard
  • Arduino
  • Jumper Wire

Hello world code example

//  Name    : shiftIn Example 2.1                               //
//  Author  : Carlyn Maw                                        //
//  Date    : 25 Jan, 2007                                      //
//  Version : 1.0                                               //
//  Notes   : Code for using a CD4021B Shift Register    	//
//          :                                                   //

//define where your pins are
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

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

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);
  switchVar2 = 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);
  Serial.println(switchVar2, BIN);

//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;