Maker Pro
Maker Pro

How do I multiplex lots of switches/LEDs with my Arduino

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
I am looking at building a Project (https://www.electronicspoint.com/threads/assorted-gubbins-on-toy-tank.277654/ ) and need to control about 13 LEDs, using maybe 12 Buttons.

I do not have that many I/O pins on the Arduino Micro that I am planning on using (especially once the other parts I need are connected)

I have been pointed in the direction of "Multiplexing", but need to check that I understand how it works.

So:
To manage the 12 Input Buttons, rather than using 12 separate pins, I build a 3x4 matrix of buttons, and write a loop to cycle through each of the 4 Columns, checking which of the 3 buttons is pressed. This uses 7 Pins (4 Column-selector, 3 input).
I run this loop, assigning values to IsButtonXPressed variables, and then move on to the next section of code (do stuff when buttons are pressed).
The code then comes back to the loop, and checks all the buttons again.

I also need to do similar on the Output, putting the LEDs in a grid, and controlling row/columns to light them up.

Am I on the right lines here?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
yeah, pretty much so.

However it might be worth looking at the 74HC595 for driving the LEDs. With this you can control an almost unlimited number of LEDs with just 3 pins.

The equivalent for inputs is the 74HC165.

There are also plenty of other chips out there that can do this sort of thing.

You'll find that there will be a tradeoff between simplicity, number of pins, number of inputs/outputs and cost (amongst other things). It's up to you to decide what the right answer is for you.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Thanks.
They certainly do look handy! And probably easier to cram into my limited space than my (inexpertly-built) matrix! :)
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
So, I can either connect 8 of my buttons to the 74HC165, and the other 4 direct to pins, or use 2x 74HC165 and work on them all together. Sounds more coherent to do the latter.
Only one button will be being pushed at once.

For LEDs, I do similar. 2x 74HC595. I will need multiple LEDs lit at once. I'm not sure how this works. Do I send a "pattern" to the 595, and it lights those LEDs, or do I send sequential 1 LED signals, and they "flicker" (too fast to notice)?
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
For the outputs, you send a bit stream representing all of the LEDs then send a latch command. The LEDs that had a 1 in the bit stream will be on. You need do nothing else until you change anything, then you do it all over again. No flickering.

Bob
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Cool.
I think I know where I am going with the code needed then. Will test basic code when my Arduino arrives, and then send off for some 74HCxxx chips.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
I need to figure what I2C is!

For now, i think the 74HC165/595 will do what I need.
 

Jouellet

Feb 2, 2015
86
Joined
Feb 2, 2015
Messages
86
8 I/O points expansion module

You could configure 4 points as Inputs and 4 points as Outputs

You write 0/1 thru I2C to turn outputs on/off
And read thru I2C
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I2C is a communications protocol which allows bidirectional data transfer.

In practice this means you (typically) use a specific set of pins and call library routines to read and write data to/from the external device(s).
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
I'm struggling to source a 74HC165 from my favoured suppliers.
Would a 74HCT164 do the same job?

Or is there a good place to buy a 165?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Try sourcing a 74HC166

Who is your supplier? Try Element 14, Mouser, or RS.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
I have been using bitsbox.co.uk.
I am only ordering small amounts ( <£20) at a time.

I know I can chain shift-registers together, but would be interested in a 16-bit IC, to keep both code and hardware tidy (I am limited on space).
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Me again! :)

I've got a bag full of 595s and 166s.

I've built a circuit using an OUTPUT shift-register (595). All works fine.
But I need to expand, and use an INPUT shift-register for my rack of switches/buttons. (also a 2nd OUTPUT shift-register, but that looks OK ...)

Every tutorial/example uses Arduino Pin 8 as latch, for both.
My OUTPUT is currently:
Code:
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

I am concerned that if I connect my INPUT latch to Pin 8, I will cause issues on the OUTPUT when reading the INPUT.

Is this a problem? Can I use a different pin?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
You can latch both at the same time if your process is to load the outputs and then read the inputs at the same time.

It is probably simpler to use a different pin for latching inputs than used for latching outputs.

There is a cunning method of using a single pin to both read and write, and a single pin for latching, but unless you're really short of pins, make things as was as possible for yourself.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Thanks.
I can just use a different pin. :)

I might be interested in the cunning trick, but not for this project! By offloading my Inputs and Outputs onto shift-registers, I should have plenty of pins left on the Arduino.
 
Top