Wednesday, October 26, 2016

Create more inputs using the CD4021 Shift 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.

SCHEMATIC
                       0
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.
Pin 8 and 16 of the CD4021 are connected to GND and 5V.

                                        
PARTS USED
1.

                                                          DigiSpark ATtiny85

2.

                                                         Full size Breadboard

3.

                                                Jumper Wires male to female

4.

                                                     Jumper Wires male to male

5.

                                                       Resistors 1/4w – 5pcs

6.

                                                   Tact switch | Push button

7.

                                         Tact switch | Push button Red – 5pcs

CODE
#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);
}
IMPORTANT FILE TO DOWNLOAD
keyboard

No comments:

Post a Comment