Create more inputs on your Arduino using the CD4021 Shift IN Register

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

OVERVIEW

Sometimes while building a project, you run out of inputs on your Arduino to connect multiple switches. 

For example the DigiSpark is great for small enclosures, but it doesn’t offer much in ways of inputs…

By using a simple Shift Register like the CD4021, you can add up to 8 additional inputs, using only 3 pins on your Arduino. 

You can even connect multiple of these Shift Register together to have more inputs if needed while still only using 3 pins.

 

In this tutorial we will use a DigiSpark with a CD4021 Shift Register and read 4 tact switches.

Depending on the switch pressed, the DigiSpark will send some keyboard strokes back to the PC.

Making a sort of Button Box, where we can assign each tact switches keyboard strokes combination.



SCHEMATIC

CD4021 Arduino

Here are the connection needed for this tutorial:

 

The GND and 5V (out) of the DigiSpark is connected to the breadboard power rails.
Pin 0-1-2 of the DigiSpark are connected to pin 3-10-9 of the CD4021.

 

One leg of the Tact Switches is connected to the 5V rail.
The other leg of the Tact Switches are connected to pin 4-5-6-7 of the CD4021.
Pins 4-5-6-7 also have some 10K resistors connected to ground for switch pullups.

CD4021 shift register



THE CODE

To have the DigiSpark act as a Keyboard, we will be using a library called “DigiKeyboard” created by DigiStump.

You can find more information about the ShiftIn function used to read information from the CD4021 here:

https://www.arduino.cc/en/Reference/ShiftIn

 

As always, Don’t forget to watch our Tutorial video for more information.

#include "DigiKeyboard.h"  // Include Library for Keyboard Emulation

//Define pins
int dataPin = 0;   // Pin 0 of DigiSpark connected to Pin 3 of CD4021
int clockPin = 1;  // Pin 1 of DigiSpark connected to Pin 10 of CD4021
int latchPin = 2;  // Pin 2 of DigiSpark connected to Pin 9 of CD4021

//Define variable
byte RegisterValue = 0;  // Used to hold data from DC4021

void setup() {

//define pins used to connect to the CD4021 Shift Register
  pinMode(dataPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 

}

void loop() {

  //Set latch pin to 1 to get recent data into the CD4021
  digitalWrite(latchPin,1);
  
  delayMicroseconds(20);
  
  //Set latch pin to 0 to get data from the CD4021
  digitalWrite(latchPin,0);

  //Get CD4021 register data in byte variable
  RegisterValue = shiftIn(dataPin, clockPin, MSBFIRST);


  if (RegisterValue == B1000) {
    DigiKeyboard.print("Button 1 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }

  if (RegisterValue == B100) {
    DigiKeyboard.print("Button 2 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }

  if (RegisterValue == B10) {
    DigiKeyboard.print("Button 3 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }

  if (RegisterValue == B10000) {
    DigiKeyboard.print("Button 4 pressed  -->");
    DigiKeyboard.println(RegisterValue, BIN);
  }

delay(250);

}


TUTORIAL VIDEO



DOWNLOAD

Copy the above Sketch code in your Arduino IDE software to program your Arduino.

 

Used Libraries:

Download The DigiKeyboard Library

created by DigiStump here:

https://github.com/digistump/DigisparkArduinoIntegration/tree/master/libraries/DigisparkKeyboard

 

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder, and make sure to restart the Arduino IDE (Close and Reopen) software so it detect this newly installed library.

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up