How do i setup 16 button inputs using two 8x1 multiplexers on arduino leonardo

I have never built anything Arduino related stuff. I barely know how electronics work but i can solder pretty well and knows the basics.
I have been wanting to build MIDI controller using arduino for quiet awhile now. So i decided to build one using Arduino Leonardo. Which has USB-HID.

Now. i want to have 4 buttons for banks, 16 buttons for inputs. So i got two 8x1 multiplexers and connected them to the Arduino like this.


I first connected them like this. and after i saw the second image i connected the rest of multiplexer pins to ground.

This image is for 1 multiplexer and for analog signals. mine is digital.

I don't know if it will work or not. any advices? tips?

I need help

Yes, it'll work, but it's sub-optimal. You can connect the address lines of the two multiplexers together, so they use only 3 pins instead of 6.

For the code, you could use the Control Surface library, for example. It has support for multiplexers built-in: Control Surface: Getting Started
(That particular example uses CCPotentiometers, but you can also use CCButtons or NoteButtons, depending on your use case.)

Pins of the multiplexers can be used transparently for changing banks as well.

Pieter

You appear to be “barking up the wrong tree”!

Your first diagram is a bit confusing, but the 74151 is obsolete and not particularly appropriate! You have not shown how you were going to connect the switches.

To select one of 16 switches, a practical way would be to connect two 74HC4051s almost as shown, but you connect the three select pins and the “Z” of both to the same pins and connect the enable pins to two different other pins so you can enable either one or the other.

Your buttons or switches you connect from the “Y” pins to ground and you connect a 4k7 pull-up from the common “Z” pin to your supply. You select each button in turn using the multiplexers, and use digitalRead to read the button.

Noob question. Do i need resistors for hooking up the buttons?

I explained that you need one pull-up resistor in the "Z" pin of the multiplexer. That will pull up for whichever button is selected at that time. Clever, eh? :grinning:

Paul__B:
I explained that you need one pull-up resistor in the "Z" pin of the multiplexer. That will pull up for whichever button is selected at that time. Clever, eh? :grinning:

In most cases, the internal pull-up resistors of the Arduino will work fine.

In which case you need to explain the need to set pinMode of INPUT_PULLUP.

But with buttons which may be "Tact" switches which develop problems over time and/ or may be connected by longer external wiring, a lower value pull-up (the internal is approximately 47k) is not a bad idea. :roll_eyes: