utilizing a 74HC4051 to multiplex inputs

for my next project (which is going to be a neat little pedal-controlled sample-based instrument – more about that later), i will need to know the states of lots of buttons, but utilizing as little as possible of arduino’s pins. using shift registers for this purpose came directly to my mind, but then I had a look at the schematics of the pocket piano arduino shield from critter and guitari. they used  74HC4051 8bit-multiplexers to read the buttons which seemed to me to be a safer way to get the button states right. besides, if you need to multiplex more than 8 inputs, you can save input pins by using multiplexers: for each shift register you attach to the arduino, you need two pins (clock and input), for the first multiplexer you always need 3 pins (output to select the outputted bit), but for every additional multiplexer you need only one more input pin!

today i tried the multiplexers and made up a small code snippet, which scans though all inputs of the 74HC4051s and outputs the numbers of the active inputs via serial. with a little bit twiddling it got quite short:

uint8_t multout[] = {4,3,2};  //connected to the Input Pins 11,10,9 on the 74HC4051
uint8_t multin = 14;      //connected to the Common Output Pin 3 on the 74HC4051

void setup() {
 Serial.begin(9600);     //fire up a Serial connection at 9600baud
 for (int i = 0; i<3; i++) pinMode (multout[i], OUTPUT);
 pinMode (multin, INPUT);
}

void loop(){
 for (uint8_t i = 0; i < 8; i++){                          //for all 8 Inputs on the 74HC4051
 for (uint8_t k = 0; k < 3; k++){                        //set the 3 Inputs
 digitalWrite(multout[k], (i & (1 << k)) >> k);          //to select a specific Input (little bit twiddling)
 delayMicroseconds(1);                                   //wait 1us to be sure, the Multiplexer has switched
 }
 if (digitalRead(multin) == 1) Serial.println(int(i));   //if an Input is HIGH, print his number via Serial
 }
}

the wiring is very simple: the buttons are all attached to Vcc and the input-pins of the 74HC4051 (pins 12-15,1,2,4,5) with a pull-down resistor of 10k. the input selectors of the multiplexer (pins 9-11) are connected to arduino pins 2-4 and the output of the multiplexer (Pin 3) is attached to arduinos pin 14 (a.k.a. analog in 0). the enable-input (pin6, active low) of the multiplexer is connected to ground, so it is always on.
with this little code snippet, it should be easy to use the 74HC4051 for most input needs.

About these ads

One thought on “utilizing a 74HC4051 to multiplex inputs

  1. Hello, just found this bit of code and it seems like you know what you’re doing. me not so much. if you have the time to answer some questions of mine that would be awesome. please feel free to email personally – or comment here.

    from what you have above – how does one write the code for individual pins on the 74hc? namely, how are they defined so that one can use the data from one pin to do a specific task.

    Do you know if it is possible to have a spst switch on one pin (of the 74hc) and when closed send a (midi) message to the computer, as well as turn on a led (attached to another pin on the 74hc) and keep it on. then when you hit the switch again the led turns off. in other words – the computer sees the switch close, creates a midi note, but also turns off (or on, accordingly) a led.

    what would the code look like for something like that?

    Thanks very much!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s