top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

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



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.

 

PARTS USED

CD4021 Shift IN Register


DigiSpark


Tact Switches


These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!

 

CONNECTIONS




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.




 







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:


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:


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.

2,382 views0 comments

Opmerkingen


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page