In this tutorial I cover the following topics
 Buttons, buttons... to many buttons
 The resistor ladder
 Multiplexing
 Ready to use multiplexer
 Expanders
Although most of microcontrollers have many input/output pins, there are no problems to spend all of them and find yourselves in a situation when there would be no option to attach enything else. In this part I will show what you can do in case of such a situation. Other words, I will show how to "multiply" our pins.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 
#define PIN_0 5 #define PIN_1 4 #define PIN_DELETE 3 #define PIN_ENTER 2 #define PIN_LED 7 #define MAX_CODE_LEN 32 #define CODE "10011" char codeArray[MAX_CODE_LEN+1]; byte codeLen = 0; void setup() { pinMode(PIN_0, INPUT); pinMode(PIN_1, INPUT); pinMode(PIN_DELETE, INPUT); pinMode(PIN_ENTER, INPUT); pinMode(PIN_LED, OUTPUT); Serial.begin(9600); digitalWrite(PIN_LED, LOW); for (int i=0; i<=MAX_CODE_LEN; i++){ codeArray[i] = '\0'; } Serial.println("Ready..."); } void loop() { if(digitalRead(PIN_0) == HIGH){ delay(20); while(digitalRead(PIN_0) == HIGH); delay(20); codeArray[codeLen] = '0'; codeArray[codeLen+1] = '\0'; codeLen += 1; if (codeLen == MAX_CODE_LEN) { codeLen = 1; } Serial.println(codeArray); } else if(digitalRead(PIN_1) == HIGH){ delay(20); while(digitalRead(PIN_1) == HIGH); delay(20); codeArray[codeLen] = '1'; codeArray[codeLen+1] = '\0'; codeLen += 1; if (codeLen == MAX_CODE_LEN) { codeLen = 1; } Serial.println(codeArray); } else if(digitalRead(PIN_DELETE) == HIGH){ delay(20); while(digitalRead(PIN_DELETE) == HIGH); delay(20); /* Funny bug if codeLen is declared as byte (to save memory for example). In such case codeLen = 1 results in codeLen = 255 which is not lower than 0. codeLen = 1; if (codeLen < 0) { codeLen = 0; } */ if (codeLen > 0) { codeLen = 1; } codeArray[codeLen] = '\0'; Serial.println(codeArray); } else if(digitalRead(PIN_ENTER) == HIGH){ delay(20); while(digitalRead(PIN_ENTER) == HIGH); delay(20); if(strcmp(codeArray, CODE) == 0) { digitalWrite(PIN_LED, HIGH); Serial.println("Access granted..."); delay(2000); digitalWrite(PIN_LED, LOW); // Don't forget to clear whole buffer to prevent memmory leaks. for (int i=0; i<=MAX_CODE_LEN; i++){ codeArray[i] = '\0'; } codeLen = 0; Serial.println("Ready..."); } Serial.println(codeArray); } } 
Recal voltage divider idea discussed in Voltage divider tutorial. General schema looks like it is showned on the following image
where the formula used to calculate $V_{out}$ takes a form
$$V_{out} = \frac{V_{in}R_{2}}{R_{1}+R_{2}}$$
$$V_{out} = \frac{V_{in}\cdot 0}{R_{1} + 0} = 0$$  
$$V_{out} = \frac{V_{in}R_{2}}{R_{1}+R_{2}} = \frac{V_{in}R_{2_{1}}}{R_{1}+R_{2_{1}}} = \frac{V_{in}}{\frac{R_{1}}{R_{2_{1}}}+1}$$  
$$V_{out} = \frac{V_{in}R_{2}}{R_{1}+R_{2}} = \frac{V_{in}(R_{2_{1}} + R_{2_{2}})}{R_{1}+R_{2_{1}}+R_{2_{2}}}= \frac{V_{in}}{\frac{R_{1}}{R_{2_{1}}+R_{2_{2}}}+1}$$  
$$V_{out} = \frac{V_{in}R_{2}}{R_{1}+R_{2}} = \frac{V_{in}(R_{2_{1}} + R_{2_{2}} + R_{2_{3}})}{R_{1}+R_{2_{1}}+R_{2_{2}}+R_{2_{3}}}= \frac{V_{in}}{\frac{R_{1}}{R_{2_{1}}+R_{2_{2}}+R_{2_{3}}}+1}$$ 
Examples given in previous subsection show few similar but different dividers. You can combine all of them into on schema.
How it works is explained in a set of images below.
This way you have had created a variable voltage divider called a multiple voltage divider
or more often a resistor ladder
.
Now it's time to make a real test.
 Create circuit given in the schema below
 Attach multimeter to our circuit
 Use the following code to programm Arduino
123456789101112131415161718192021222324#define VOLTAGE A0int sensorValue = 0;int i = 0;void printVolts(int val) {float voltage = val * (5.0 / 1023.0);Serial.print(" ADC value: ");Serial.print(val);Serial.print(", volts: ");Serial.println(voltage);}void setup() {Serial.begin(9600);}void loop() {sensorValue = analogRead(VOLTAGE);Serial.print(i);i++;printVolts(sensorValue);delay(1000);} 
Write down results: voltage displayed on multimeter and analog value readed by Arduino and changed into numerical values by analogdigital converter (ADC in short). In this case you got
Switch
pressedResistance value of variable
divider part $R_{2}$
(for constant $R_{1}=10kO$)Voltage used measured as integer
from ADCconverted to volts
from ADC valuemeasured by
multimetertheoretical
value1 0kO 0 0 0.0 0.00 0.0 2 $R_{2}$ = 1kO 0.98kO 95 0.46 0.47 0.45 3 $R_{2}+R_{2}$ = 1kO+1kO=2kO 1.97kO 180 0.89 0.83 0.83 4 $R_{2}+R_{2}+R_{2}$ = 1kO+1kO+1kO=3kO 2.95kO 238 1.16 1.16 1.15
This allows us to interprete each reading as a different button being pressed with a few lines of code  see listing below. Obviously rather than telling the microcontroler to look for the exact values you expect the analog pin to read, you set a range of values that can be interpreted as belonging to a specific button. I do this by calculating the points halfway between each expected ADC reading and set these as as the boundaries of our ranges (see also image)
Switch  Range  
from  to  midpoint  
1  0  95  47 
2  95  180  137 
3  180  238  209 
4  238  1023  630 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
#define VOLTAGE A0 int sensorValue = 0; int i = 0; int getButtonNumber(int val) { if (val <= 47) return 1; else if (val <= 137) return 2; else if (val <= 209) return 3; else if (val <= 630) return 4; else if (val <= 1023) return 0; } void setup() { Serial.begin(9600); } void loop() { sensorValue = analogRead(VOLTAGE); Serial.print(i); i++; Serial.print(" "); Serial.println(getButtonNumber(sensorValue)); delay(1000); } 
The downside is that this can only detect one button press at a time, the button with the lowest resistor value is returned, and the output varies when different voltages are used to power it requiring an update to our software.
So far, so good but this soltion has two drawbacks which makes it working only for a project with a small number of buttons.
 Every time you change the number of buttons, you have to measure what have changed  you have to measure ADC values for newly added button(s). Neext you have to update our source code.
 What is worse, voltage resolution is not constant. The more buttons you have, the shortest voltage ranges you will have.
Adding another switches, each with a 1kO resistor, you obtain results presented in the table below. To make it more visible, also results for 21, 31 and 41 buttons obtained with 10kO resistors instead 10 times 1kO are included.Switch Range from to midpoint Midpoint
distance1 0 95 47  2 95 180 137 90 3 180 238 209 66 4 238 294 266 57 5 294 342 318 52 6 342 384 363 45 7 384 424 404 41 8 424 458 441 37 9 458 491 474 33 10 491 518 504 30 11 518 21 682 31 766 41 816 1023 919
and depicted in the image
The situation could be even worse if you use 1kO pullup resistor
Switch Range from to midpoint Midpoint
distance1 15 518 266  2 518 687 602 336 3 687 769 728 126 4 769 820 794 66 5 820 853 836 42 6 853 877 865 29 7 877 895 886 21 8 895 909 902 16 9 909 920 914 12 10 920 930 925 11 11 930 21 974 31 990 41 998 1023 1010
Variable and short voltage ranges may lead to errors both in code and in real devices as a result of noise and components tollerance. Anyway, magic numbers are always bad solution and should be avoided whenever possible. If you should somehow predict ADC value based on ADC resolution and number of buttons then you could implement better solution.
So, lets try find out resistors values to have equidistance measured in voltage, based on the following specification:
 $V_{in}=5V$,
 four buttons,
 pullup resistor $R_{1}=1kO$,
 voltage distance $V_{d}=0.5$, e.g. $V_{S1}=0$ (voltage output for switch $S1$), $V_{S2}=V_{d}$ (voltage output for switch $S2$), $V_{S3}=2V_{d}$, $V_{S3}=3V_{d}$.
You know the input voltage, output voltage, and resistor $R_{1}$, but not $R_{2}$ which should be calculated. Starting from previously given formula
$$V_{out} = \frac{V_{in}R_{2}}{R_{1}+R_{2}}$$
rearranging its components
$$V_{out}(R_{1}+R_{2}) = V_{in}R_{2}$$
$$V_{out}R_{1}+V_{out}R_{2} = V_{in}R_{2}$$
$$V_{out}R_{1} = V_{in}R_{2}V_{out}R_{2}$$
$$V_{out}R_{1} = R_{2}(V_{in}V_{out})$$
you finaly obtain formula for missing resistance $R_{2}$
$$R_{2} = \frac{R_{1}V_{out}}{V_{in}V_{out}}$$
Based on this formula and notation from the following schema you can calculate desired resistances
 Calculations for $R_{21}$  in this case $V_{out}$ should be equal to 0.5V
$$
R_{2} = R_{21} = \frac{R_{1}\cdot V_{out}}{V_{in}V_{out}} = \frac{1.0 \cdot 0.5}{5.0  0.5} = \frac{0.5}{4.5} = 0.11
$$
To get resistance close to theoretical value of 0.11kO which is 110 Ohm, you will use one 100 Ohm resistor and one 10 Ohm so $R_{21}$ would be equal to 110 Ohm.  Calculations for $R_{22}$  in this case $V_{out}$ should be equal to 1.0V
$$
R_{2} = R_{21} + R_{22}
$$
On the other hand you know that
$$R_{2} = \frac{R_{1}V_{out}}{V_{in}V_{out}}$$
so in consequence
$$R_{21} + R_{22} = \frac{R_{1}V_{out}}{V_{in}V_{out}}$$
and then
$$R_{22} = \frac{R_{1}V_{out}}{V_{in}V_{out}}  R_{21}$$
$$R_{22} = \frac{1.0 \cdot 1.0}{5.01.0}  0.11 = \frac{1}{4}  0.11 = 0.25  0.11 = 0.14$$
To get resistance close to theoretical value of 0.14kO which is 140 Ohm, you will use one 150 Ohm resistor so $R_{21}$ would be equal to 150 Ohm.  Calculations for $R_{23}$  in this case $V_{out}$ should be equal to 1.5V
$$
R_{2} = R_{21} + R_{22} + R_{23}
$$
On the other hand you know that
$$R_{2} = \frac{R_{1}V_{out}}{V_{in}V_{out}}$$
so in consequence
$$R_{21} + R_{22} + R_{23} = \frac{R_{1}V_{out}}{V_{in}V_{out}}$$
and then
$$R_{23} = \frac{R_{1}V_{out}}{V_{in}V_{out}}  R_{21}  R_{22}$$
$$R_{23} = \frac{1.0 \cdot 1.5}{5.01.5}  0.11  0.15 = \frac{1.5}{3.5}  0.26 = 0.428  0.26 = 0.168$$
To get resistance close to theoretical value of 0.168kO which is 168 Ohm, you will use one 120 Ohm resistor and one 51 Ohm so $R_{23}$ would be equal to 171 Ohm.
Now you can implement more universal method for detecting the pin which was pressed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
#define VOLTAGE A0 #define VOLTAGE_RANGE 100 #define BUTTONS 4 int sensorValue = 0; int i = 0; int getButtonNumber(int val) { if (val > VOLTAGE_RANGE * BUTTONS  VOLTAGE_RANGE/2) { return 0; } else { return (val + VOLTAGE_RANGE/2)/VOLTAGE_RANGE + 1; } } void setup() { Serial.begin(9600); } void loop() { sensorValue = analogRead(VOLTAGE); Serial.print(i); i++; Serial.print(" "); Serial.println(getButtonNumber(sensorValue)); delay(1000); } 
and measure real voltage and ADC values as it is presented in the table below
Switch pressed 
Resistance value of variable divider part $R_{2}$ (for constant $R_{1}=1kO$) 
Resistance distance 
Voltage  
used  measured  calculated  used  as integer from ADC 
converted to volts from ADC value 
measured by multimeter 
theoretical value 

0          1023  5.00  4.95  5.00 
1  0k$\Omega$  0  0  0  0  0.0  0.02  0.0 
2  $R_{21}$ = 110 $\Omega$  113 $\Omega$  $R_{21}=110 \Omega$  $R_{21}=110 \Omega$  100  0.5  0.51  0.5 
3  $R_{21}+R_{22}$ = 260 $\Omega$  260 $\Omega$  $R_{22}=140 \Omega$  $R_{22}=150 \Omega$  208  1.0  1.04  1.0 
4  $R_{21}+R_{22}+R_{23}$ = 431 $\Omega$  430 $\Omega$  $R_{23}=168 \Omega$  $R_{23}=171 \Omega$  306  1.5  1.51  1.5 
You may ask: What if I press more than one button at once? For example: two or three? Nothing. Really nothing. You don't believe me? Let's make a test in Tinkercad.
Start with pressing one button at a time:
Now, press two buttons and more:
In this case result is the same as you would press third button. If you continue this experiment you will not get different results in a sense that what you get agrees with corresponding one button case:
Multiplexing
is the generic term used to describe the operation of sending one or more analogue or digital signals over a common transmission line at different times or speeds. The device you use to do that is called a multiplexer
and demultiplexer
. An electronic multiplexer (also mux
or data selector
) makes it possible for several signals to share one device or resource, for example, one A/D converter or one communication line, instead of having one device per input signal. It selects between several analog or digital input signals and forwards it to a single output line.
You should be familiar with this concept, as every day you use it (or rather it is used by some devices) to transmit your data through an network:
Generally, the selection of each input line in a multiplexer is controlled by an additional set of inputs called control lines
and according to the binary condition of these control inputs, one appropriate data input is connected directly to the output. Normally, a multiplexer has an even number of $2^{n}$ input lines and a corresponding number of $n$ control inputs. An electronic multiplexer can be considered as a multipleinput, singleoutput switch. The schematic symbol for a multiplexer is an isosceles trapezoid with the longer parallel side containing the input pins and the short parallel side containing the output pin. The schematic below shows a 2to1 multiplexer (also denoted as 2:1 muliplexer or 2:1 mux) on the left and an equivalent switch on the right.
Multiplexers can be either digital circuits made from high speed logic gates used to switch digital or binary data or they can be analogue types using transistors, MOSFET’s or relays to switch one of the voltage or current inputs through to a single output.
To start out easy, you’ll create a multiplexer taking two inputs and a single selector line as it was depicted above. With inputs $X_{0}$ and $X_{1}$ and select line $S$, if $S$ is 0, the $X_{0}$ input will be the output $Y$. If S is 1, the $X_{1}$ will be the output $Y$.
To find a digital circuit imlementing this functionality, first you have to create corresponding truth table:
Inputs  Output  

$S$  $X_{0}$  $X_{1}$  $Y$ 
0  0  0  0 
0  0  1  0 
0  1  0  1 
0  1  1  1 
1  0  0  0 
1  0  1  1 
1  1  0  0 
1  1  1  1 
Having this table, you can derive a corresponding Boolean formula:
$$
\begin{align}
Y =
& \overline{S} \cdot X_0 \cdot \overline{X_1} + \overline{S} \cdot X_0 \cdot X_1 + S \cdot \overline{X_0} \cdot X_1 + S \cdot X_0 \cdot X_1 = \\
& \overline{S} \cdot X_0 \cdot (\overline{X_1} + X_1) + S \cdot X_1 \cdot (\overline{X_0} + X_0) = \\
& \overline{S} \cdot X_0 + S \cdot X_1\\
\end{align}
$$
The boolean formula for the 2to1 multiplexer looks like this:
$$Y= \overline{S} \cdot X_{0} + S \cdot X_{1}$$
or the same as programming logic expression
1 
Y = (~S && X0)  (S && X1) 
or in the pseudocode style
1 
Y = OR( AND(NOT(S), X0), AND(S, X1)) 
Based on the given above formulas, you can make a corresponding logic circuit
Of course it can be completed also with other set of gates, for example NAND gates. From the previous material, Completeness of NAND gate set, you know that using only NAND gates you can have equivalents of all three basic gates: AND, OR and NOT as it is showned again below.
Using these ,,replacemants'' to the schema with basic set of gates you obtain
This schema could be simplify. Let's introduce some additional symbols
Now you can write
$$
Y = \overline{E \cdot F} = \overline{\overline{C \cdot C} \cdot \overline{D \cdot D}}
$$
Because in Boolean algebra the following is true
$$
X \cdot X = X
$$
you can write
$$
Y = \overline{\overline{C} \cdot \overline{D}} = \overline{\overline{\overline{A \cdot A}} \cdot \overline{\overline{B \cdot B}}}
$$
Again from the law given above you obtain
$$
Y = \overline{\overline{\overline{A}} \cdot \overline{\overline{B}}}
$$
and applying the following rule
$$
\overline{\overline{X}} = X
$$
you finally get
$$
Y = \overline{A \cdot B}
$$
Other words, none of the gate with output $C$, $D$, $E$ or $F$ is needed, so final chema od multiplexer build only with NAND gates looks like below
You can combine two lower order multiplexers like 2:1 or 4:1 to get higher order multiplexer like 8:1. Now, for example let us try to implement a 4:1 multiplexer using a 2:1 multiplexer. To construct a 4:1 multiplexer using a 2:1 multiplexer, you will have to combine three 2:1 multiplexer together. As it is showned on the image below
The end result should give us 4 input pins ($X_{0}$, $X_{1}$, $X_{2}$ and $X_{3}$), 2 control/select pins ($S_{0}$ and $S_{1}$) and one output pin ($Y$).
Conversely to multiplexer, a
demultiplexer
(or demux
) and pass through to one of multiple output lines, using a select line to choose which output the input goes to.
To find a digital circuit imlementing this functionality, first you have to create corresponding truth table:
Inputs  Outputs  

$S$  $X$  $Y_{0}$  $Y_{1}$ 
0  0  0  0 
0  1  1  0 
1  0  0  0 
1  1  0  1 
Having this table, you can derive a corresponding Boolean formula which in case of the 1to2 demultiplexer is really simple and looks like this:
$$Y_{0}= \overline{S} \cdot X$$
$$Y_{1}= S \cdot X$$
or the same as programming logic expression
1 2 
Y0 = ~S && X Y1 = S && X 
or in the pseudocode style
1 2 
Y0 = AND(NOT(S), X) Y1 = AND(S, X) 
Based on the given above formulas, you can make a corresponding logic circuit
Of course it can be completed also with other set of gates, for example NAND gates
There are a lot of different ready to use (de)multiplexer.
 The basic trio of analog multiplexers stems from the venerable 4000series CMOS logic chips: the 4051 is a single eightway multiplexer, the 4052 has two fourway multiplexers, and the 4053 has three twoway switches.
 The 74HC405x series chips with higher switching speed or lowvoltage analog multiplexers meant for batterypowered use: 74LV405x.
 The highspeed and lowresistance 3251, 3252, and 3253 series chips: CBT3251, FST3252, SN74CBT3253, etc.
In most cases multiplexer can work also as a demultiplexer, so further I will use the term multiplexer in both meanings.
In this part I will show how to work with
CD4051B
and CD4052B
chip (CD4051B, CD4052B, CD4053B datasheet ).
CD4051 is an eight (8) channel multiplexer and has three control input named as A
, B
and C
. These inputs connect only 1 out of 8 channels to the output in order to obtain the desired output. Channel I/O terminals became outputs and common O/I become input terminals when CD 4051 is used as a demultiplexer. Pinous for all chips from this family are depicted on the image below and for CD4051 chip is summarized in the following table
Pin 
Meaning 
A 
selector line coding first bit of binary channel number 
B 
selector line coding second bit of binary channel number 
C 
selector line coding third bit of binary channel number 
07 
independent inputs/outputs channels 
Vee 
negative supply voltage (connected to ground (GND) in our case) 
Vss 
ground (GND, 0V) 
Vdd 
positive supply voltage (from 5 to 20 volts) 
COM AUT/IN 
common input/output 
INH 
enable input active on LOW (connected to ground (GND) in our case) 
I start with very basic and unusual example and show how multiplexer can be used as a static hardware switch.
Left: Basic schema to test CD4051 chip. Right: Close to real view of a circuit schema given on the right. 