Blog / News ArdSimX Articles/Guides Project Archive SimVim Panel Baron-58 sim

Buttons and toggle switches

Direct input

A simple input control devices such as buttons and toggle switches can be connected to Arduino either directly or using input extension board or "key matrix" grid.

With direct connection every switch occupies one Arduino pin, so, with one "Mega" board you can have about 50 switches, with Uno 10-12 or more with analog inputs that can be used as digital inputs too.

For all libraries presented on this site any toggle switch or button should have one its output pin connected to an Arduino pin and the other to the Ground.

Each digital pin of Arduino that is used as input is auto-assigned in these libraries as input with internal pullup resistor. You don't need to use standard Arduino "pinMode()" function. No any external resistors are needed!

Note: When Pin #13 used as input it should have an external pull down resistor, because it has a built-in on-board LED with resistor attached to it.

If you use Ethernet connection this pin cannot be used in some boards (Uno, Nano etc.). When you use Arduino with Ethernet Shield/Module you should know that some pins cannot be used for input/output because they are reserved for communication between Arduino and Ethernet Shield.

Adding more digital inputs

When you need to connect some kind of multi-position linear or rotary switch you can use digital pins for each position (see the picture at right) this can take a lot of digital inputs (e.g. 10 pins for 2-engine starter/magneto).

You can use analog pin as additional digital input for Multi-position switch.

Another option is using additional extension board - see Input extention board.

Even with one Mega board, you can have about 50 directly connected switches and buttons. To extend the number of inputs you can simply add more Arduino boards with ArdSim library (up to 9) to your system.

If adding more Arduino boards is not an option, you can use a "key-matrix" inputs. This connection method is a little more complicated than direct connection and needs additional soldering and components (lots of diodes).

Key-matrix input interface

The commonly used method to increase the number of inputs for any microcontroller is the input/output multiplexing technique known as "key-matrix". With this method several inputs ("rows") and several outputs ("columns") of the controller form a grid (matrix) . In each intersection (node) of this grid you can place a button that connects one row with one column when pressed.

All Arduino row pins (red) are configured as inputs with internal pull-up resistors. The column (blue) pins are configured as outputs.

The program reads all input lines one by one when the first output line is set to active state, then repeats this action for each output line and detects changes on each intersection.

To prevent "ghosting and masking effects" when several buttons / switches are pressed simultaneously, a diode should be connected to each toggle and button.

Pro: N input lines and M output lines forms an NxM grid and you can have more inputs than with direct connection using the same number of controller pins.

Cons: Since it is multiplexing method, the larger the matrix dimension the more processing time will be needed to scan all inputs, and that can affect some output devices.

Advice: Choose the minimally adequate matrix size that you will need. For example, don't assign a 10x10 matrix if you will use no more than 30 switches (5x6 will be enough).

In case you use one Arduino board for switches and buttons inputs only, the matrix size is not critical.

In the picture you can see an example of a key matrix with 5 rows and 6 columns. For each of the 30 nodes (circled) in this example one button or switch can be used. Diodes connected in series with button are needed to prevent interference between nodes.

As an option you can use enough pins as direct inputs and use remaining ones as key-matrix to extend inputs up to the required number. For example, you can have an 8x8 matrix for 64 inputs using 16 pins of Arduino Mega and the other 32 pins as direct inputs for buttons, switches and encoders.

All additional components that you'll need are lots of diodes (you have to connect one diode for each button and switch to prevent interference between nodes and Arduino damage!).

Diodes can be soldered directly to each button/switch. Numeration for ArdSim configuration is shown on the diagram (matrix 6x5):

Wiring options for matrix inputs

Matrix 3x3 example:Switch with diode:Option for diode connections:

One of the suitable way of wiring is using the flat ribbon cable. You will need the cable with N+M conductors for an NxM matrix and several clamping connectors. I.e. If you have 64 matrix inputs (8x8) take a cable with at least 16 conductors, or more if you need to extend other wires such as Gnd and +5v.

You can use flat cable only for "column" lines:

Or, you can use some kind of extension board with headers and diodes. In this case couples of wires will be stretched to each button and can lead to wiring mess.

© Copyright 2012-2016 - SimVim Design