Using the CD4021 Shift IN Register chip to create more inputs on a DigiSpark
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.
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.
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:
As always, Don’t forget to watch our Tutorial video for more information.
Copy the above Sketch code in your Arduino IDE software to program your Arduino.
Download The DigiKeyboard Library
created by DigiStump here:
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.