Arduino Development Cookbook - Sample Chapter
Arduino Development Cookbook - Sample Chapter
ee
Arduino Development Cookbook comprises clear and step-by-step recipes that give you the toolbox of
techniques to construct any Arduino project, from the simple to the advanced. Each chapter gives you
more essential building blocks for Arduino development, from learning about programming buttons through
to operating motors, managing sensors, and controlling displays. Throughout, you'll find tips and tricks to
help you troubleshoot your development problems and push your Arduino project to the next level!
The single-chip computer board Arduino is small in size but vast in scope, capable of being used for
electronic projects from robotics through to home automation. The most popular embedded platform in the
world, Arduino users range from school children to industry experts, all incorporating it into their designs.
Sa
pl
e
and problems
Cornel Amariei
$ 44.99 US
29.99 UK
P U B L I S H I N G
Arduino Development
Cookbook
Over 50 hands-on recipes to quickly build and understand Arduino
projects, from the simplest to the most extraordinary
P U B L I S H I N G
Cornel Amariei
Connecting a button
Button to serial
Button debouncing
Button multiplexing
Introduction
Buttons are the basis of human interaction with the Arduino. We press a button, and
something happens. They are simple components, as they only have two states: opened
or closed. When a button is closed, current can pass though it. When it's opened, no current
can pass. Some buttons are closed when we push them, some when they are released.
In this chapter, we will explore various button configurations and see how to tackle common
problems with these. Let's jump in!
Connecting a button
One of the basic interactions you can have with the Arduino is pushing a button, which causes
another action. Here, we will see how to connect and use a button.
41
Momentary buttons are active as long as they are pressed, while maintained buttons keep
the state we let them in. Keyboards have momentary buttons while the typical light switch is
a maintained button.
Momentary buttons can either be opened or closed. This reflects the connection state when
not pressed. A closed momentary switch will conduct current while not pressed and interrupt
the current when pressed. An opened button will do the opposite.
Lastly, there are poles and throws. The following figure explains the main two types:
Single Pole Single Throw (SPST) switches have a closed state in which they conduct current,
and an opened state in which they do not conduct current. Most momentary buttons are SPST.
42
Chapter 3
Single Pole Double Throw (SPDT) switches route the current from the common pin to one of the
two outputs. They are typically maintained; one example of this is the common light switch.
The common button we'll be using in this chapter is a push button. It's a small momentary
opened switch. It typically comes in a 4-pin case:
The pins inside the two red ellipses are shorted together. When we press the button,
all four pins are connected.
Getting ready
These are the ingredients needed to execute this recipe:
A push button, which can be found at any electronics store, such as the
online shops of Sparkfun, Radioshack, Adafruit, and Pololu
How to do it
The following are the steps to connect a button:
1. Connect the Arduino GND and 5V to separate long strips on the breadboard.
2. Mount the push button on the breadboard and connect one terminal to the 5V
long strip and the other to a digital pin on the Arduinoin this example, pin 2.
3. Mount the resistor between the chosen digital pin and the GND strip. This is
called a pull-down setup. More on this later.
43
Schematic
This is one possible implementation on the second digital pin. Other digital pins can also
be used.
44
Chapter 3
Code
The following code will read if the button has been pressed and will control the built-in LED:
// Declare the pins for the Button and the LED
int buttonPin = 2;
int LED = 13;
void setup() {
// Define pin #2 as input
pinMode(buttonPin, INPUT);
// Define pin #13 as output, for the LED
pinMode(LED, OUTPUT);
}
void loop() {
// Read the value of the input. It can either be 1 or 0.
int buttonValue = digitalRead(buttonPin);
if (buttonValue == HIGH) {
// If button pushed, turn LED on
digitalWrite(LED,HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
}
How it works
The purpose of the button is to drive the digital pin to which it's connected to either HIGH or
LOW. In theory, this should be very simple: just connect one end of the button to the pin and
the other to 5V. When not pressed, the voltage will be LOW; otherwise it will be 5V, HIGH.
However, there is a problem. When the button is not pressed, the input will not be LOW but
instead a different state called floating. In this state, the pin can be either LOW or HIGH
depending on interference with other components, pins, and even atmospheric conditions!
That's where the resistor comes in. It is called a pull-down resistor as it pulls the voltage
down to GND when the button is not pressed. This is a very safe method when the resistor
value is high enough. Any value over 1K will work just fine, but 10K ohm is recommended.
45
Code breakdown
The code takes the value from the button. If the button is pressed, it will start the built-in
LED. Otherwise, it will turn it off.
Here, we declare the pin to which the button is connected as pin 2, and the built-in LED on
pin 13:
int buttonPin = 2;
int LED = 13;
In the setup() function, we set the button pin as a digital input and the LED pin as an
output:
void setup() {
pinMode(buttonPin, INPUT);
pinMode(LED, OUTPUT);
}
The important part comes in the loop() function. The first step is to declare a variable that
will equal the value of the button state. This is obtained using the digitalRead() function:
int buttonValue = digitalRead(buttonPin);
Lastly, depending on the button state, we initiate another action. In this case, we just light
up the LED or turn it off:
if (buttonValue == HIGH){
digitalWrite(LED,HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
There's more
In this example, we've seen how to connect the button with a pull-down resistor. However,
this is not the only way. It can also be connected using a pull-up resistor.
Pull-up configuration
In a pull-up configuration, the resistor will pull up the voltage to 5V when the button is not
pressed. To implement it, connect one terminal of the button to the digital pin and the other
one to GND. Now, connect the resistor between the digital pin and 5V.
46
Chapter 3
This configuration will return inverted values. When pressed, the button will give LOW, not
HIGH, as it will draw the pin down to GND, 0 V. However, it brings no advantages over the
pull-down configuration.
Multiple buttons
What if we want to implement multiple buttons? We only need to use one digital pin
configured as input for each button we use. Also, each button needs its independent
pull-down or pull-up resistor.
See also
For other topics regarding buttons, check the following important recipes in this chapter:
Getting ready
For this recipe, you will need just two components:
A push button
How to do it
There is just one simple step in this recipe:
1. Connect the Arduino GND to a terminal on the button and connect the chosen
digital pin to the other terminal.
47
Schematic
Here is one implementation on the 12th digital pin. Other digital pins can also be used.
For most buttons with standard through-hole terminals, we can directly input the pins into the
terminals on the Arduino.
48
Chapter 3
Code
The following code will read if the button has been pressed and will control the built-in LED:
// Declare the pins for the Button and the LED
int buttonPin = 12;
int LED = 13;
void setup() {
// Define pin #12 as input and activate the internal pull-up
resistor
pinMode(buttonPin, INPUT_PULLUP);
// Define pin #13 as output, for the LED
pinMode(LED, OUTPUT);
}
void loop(){
// Read the value of the input. It can either be 1 or 0
int buttonValue = digitalRead(buttonPin);
if (buttonValue == LOW){
// If button pushed, turn LED on
digitalWrite(LED,HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
}
How it works
When we press the button, the value of the Arduino pin should be either LOW or HIGH. In this
configuration, when we press the button, the pin is connected directly to GND, resulting in
LOW. However, when it is not pressed, the pin will have no value; it will be in a floating state.
To avoid this, an internal pull-up resistor is connected between each pin and 5V. When we
activate the resistor, it will keep the pin at HIGH until we press the button, thus connecting
the pin to GND.
49
Code breakdown
The code takes the value from the button. If the button is pressed, it will start the built-in
LED. Otherwise it will turn it off.
Here, we declare the pin to which the button is connected as pin 12, and the built-in LED
as pin 13:
int buttonPin = 12;
int LED = 13;
In the setup() function, we set the button pin as a digital input and we activate the internal
pull-up resistor using the INPUT_PULLUP macro. The LED pin is declared as an output:
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
In the loop() function, we continuously read the value of the button using the
digitalRead() function, and we store it in a newly declared variable called buttonValue:
int buttonValue = digitalRead(buttonPin);
Lastly, depending on the button state, we initiate another action. In this case, we just light up
the LED or turn it off:
if (buttonValue == LOW){
// If button pushed, turn LED on
digitalWrite(LED,HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
There's more
It is easy to connect a button to the Arduino without any resistors. What if we need
more buttons?
Multiple buttons
Each button requires its own digital pin and resistor. The Arduino already has one pull-up
resistor in each digital and analog pin, so in the end, all that is needed is one pin for each
individual button. The other terminal of the buttons is tied together to GND.
50
Chapter 3
See also
For other topics regarding buttons, check the following important recipes in this chapter:
Getting ready
The following are the ingredients required to execute this recipe:
At least one toggle switch, which we can always take out of an old electric
toy or buy from Sparkfun, Digikey, and so on
How to do it
Follow these steps in order to connect the toggle switch to the LEDs:
1. Connect the Arduino GND to a long strip on the breadboard.
2. Mount the toggle switch and connect the middle terminal to the long GND strip
on the breadboard.
51
Schematic
This is one possible implementation. Other digital pins can also be used.
52
Chapter 3
Code
The following code will check the status of the toggle switch and will drive the LEDs accordingly:
int buttonPin1 = 2;
int buttonPin2 = 3;
int LED1 = 5;
int LED2 = 6;
void setup() {
// Define the two LED pins as outputs
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// Define the two buttons as inputs with the internal pull-up
resistor activated
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop(){
// Read the value of the inputs. It can be either 0 or 1
// 0 if toggled in that direction and 1 otherwise
int buttonValue1 = digitalRead(buttonPin1);
int buttonValue2 = digitalRead(buttonPin2);
How it works
When the toggle switch is toggled to one of the two end positions, it will connect one of the
Arduino pins to GND. The pin that is not connected to GND will stay HIGH due to the internal
pull-up resistor in the Arduino pin. If the toggle switch is in the central position, no pin will be
connected to GND and both will be HIGH.
Code breakdown
The code takes the value from the two pins connected to the toggle switch. If one of them
goes LOW, it will turn on one of the LEDs.
Here, we declare the pins to which the toggle switch is connected as pins 2 and 3. The LEDs
are defined on pins 5 and 6:
int buttonPin1 = 2;
int buttonPin2 = 3;
int LED1 = 5;
int LED2 = 6;
In the setup() function, we set the pins for the LEDs as outputs and the two pins going to
the toggle switch as inputs. Also, we activate the internal pull-up resistor so that it does not
need an external one:
void setup() {
// Define the two LED pins as outputs
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// Define the two buttons as inputs with the internal pull-up
resistor activated
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
In the loop() function, we continuously read the values of the two pins going to the toggle
switch, and we store them in the variables buttonValue1 and buttonValue2:
int buttonValue1 = digitalRead(buttonPin1);
int buttonValue2 = digitalRead(buttonPin2);
54
Chapter 3
Lastly, depending on the toggle switch state, we initiate another action:
if (buttonValue1 == HIGH && buttonValue2 == HIGH){
// Switch toggled to the middle. Turn LEDs off
} else if (buttonValue1 == LOW){
// Button is toggled to the second pin, one LED ON
} else {
// Button is toggled to the third pin, the other LED ON
}
There's more
Toggle switches can be very useful when used together. A DIP switch is very interesting, as it
usually has multiple small toggle switches. Each time we add a toggle switch, we double the
number of configurations. With four two-state switches, we can have up to 16 configurations.
This is useful when we have a system that needs a lot of configurations. Rather than
uploading code again and again, we can use the toggle switches to choose what to do.
See also
Use the following links to find some common switches you can buy:
https://www.sparkfun.com/products/9276
https://www.sparkfun.com/products/8034
Button to serial
If we want to easily track how a button acts, serial communication is the best and simplest
way. All we need to do is to read the status of the button and print it to the serial connection.
Testing whether a button is working can be solved by using an LED. However, if we need to
check two buttons or better understand what's happening when the button is pressed, serial
communication is much safer and may even be simpler.
Getting ready
The following are the ingredients required to execute this recipe:
A button
55
How to do it
This recipe uses the Button with no resistor recipe's hardware implementation. Please
implement the same schematic as in that recipe. We will have different code here,
which will output the values on the serial connection.
Code
The following code will print the button status on the serial connection:
int buttonPin = 2;
void setup() {
// Define pin #2 as input
pinMode(buttonPin, INPUT_PULLUP);
// Establish the Serial connection with a baud rate of 9600
Serial.begin(9600);
}
void loop(){
// Read the value of the input. It can either be 1 or 0.
int buttonValue = digitalRead(buttonPin);
// Send the button value to the serial connection
Serial.println(buttonValue);
// Delays the execution to allow time for the serial transmission
delay(25);
}
If the button is connected to another digital pin, simply change the value
of buttonPin to the digital pin that has been used.
How it works
When the button is pressed, it can either return a value of 1 or 0. Because we activated the
internal pull-up resistor inside the Arduino pin, the values will be safe, and no floating condition
will be obtained. After we read the value of the pin, we send it to the serial connection.
Code breakdown
The code takes the value from the button connected to digital pin 2 and writes it to the serial.
56
Chapter 3
In the setup() function, we set the button pin as an input and activate the internal pull-up
resistor. Then, we start the Serial connection with a speed of 9,600 bits per second:
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
In the loop() function, we continuously read the value of the connected button:
int buttonValue = digitalRead(buttonPin);
Then, we print the value using the Serial.println() command. We can also use Serial.
print(); however, println will write the value and go to a new line afterwards. This looks
much better and it is easier to understand:
Serial.println(buttonValue);
At the end, we need a delay to allow the data to be transmitted. The delay can be short
since we only send one value; however, it is mandatory to have it. Otherwise, the serial
will constantly overflow and no good values will reach the computer:
delay(25);
There's more
To print more than one button, we can use the Serial.print() function to write each
button state in line and then use the Serial.println() function to go to the next line.
Here is a simple implementation:
Serial.print(buttonValue1); // Print first value
Serial.print(" "); // Leave an empty space between
Serial.println(buttonValue2); // Print the second value
Button debouncing
A button is a simple device; when we push it, it gives a value, otherwise it gives another value.
Unfortunately, it is not always like that. When we push or release a button, for a very small
amount of time the button bounces between pushed or not. This is due to mechanical errors
and wear and tear in the button.
Even if it is a small amount of time, the Arduino is quick, and when we press the button, it may
read values that are quickly bouncing between pressed and not pressed. In most cases, this is
not a problem; but in many cases, this happens and it can take hours to detect what is going
wrong. Better be safe than sorry!
57
Getting ready
The following are the ingredients required for this recipe:
A button
How to do it
This recipe uses the hardware implementation in the Button with no resistor recipe. Please
implement the same schematic as in that recipe. We will have a different code here, which
will output the debounced values on the serial connection.
Code
The following code will read the status of the button and print it over the serial connection:
// Declare the pin for the button
int buttonPin = 2;
// Variable for keeping the previous button state
int previousButtonValue = HIGH;
long lastDebounce = 0; // Last time the button was pressed
long debounceTime = 50; // Debounce delay
void setup() {
// Define pin #2 as input and activate the internal pull-up
resistor
pinMode(buttonPin, INPUT_PULLUP);
// Establish the Serial connection with a baud rate of 115200
Serial.begin(115200);
}
void loop(){
// Read the value of the input. It can either be 1 or 0
int buttonValue = digitalRead(buttonPin);
if (buttonValue != previousButtonValue && millis() lastDebounce >= debounceTime){
58
Chapter 3
// Reading is useable, print it
Serial.println(buttonValue);
// Reset the debouncing timer
lastDebounce = millis();
// Change to the latest button state
previousButtonValue = buttonValue;
}
// Allow some delay for the Serial data to be transmitted
delay(10);
}
How it works
To avoid reading the button multiple times and detecting false readings, there are two
important steps. First, we only read changes in the button state. If the button has the
same value as at the last reading, we ignore it. Secondand here is the important stepif
the button has been pressed or released, we don't evaluate its value for the following few
milliseconds. This will make sure that no rapid oscillations in the button state are read.
It is possible to implement this with a simple delay() function; however, delay() stops
the Arduino board from executing anything else.
Code breakdown
The code takes the value from the button connected on digital pin 2 and uses debouncing
logic to assure proper output.
We need to declare a few variables. The previousButtonValue variable keeps track of the
previous state of the button. The lastDebounce variable is important; it stores the particular
time at which the button was pressed earlier. The debounceTime variable is the amount
of time in milliseconds between each reading. It is important for these two variables to be
declared as long type, because the numbers get pretty big quite fast.
int previousButtonValue = HIGH;
long lastDebounce = 0; // Last time the button was pressed
int debounceTime = 50; // Debounce delay
59
In the setup() function, we set the button pin as an input and we activate the internal pullup resistor. Then, we start the serial connection with a speed of 115,200 bits per second:
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);
}
In the loop() function, we continuously read the value of the connected button:
int buttonValue = digitalRead(buttonPin);
Now we need to apply the debouncing part of the code. It consists of an IF clause with
two conditions:
The first condition checks whether the new reading is different from the last one.
We don't want to detect a button push a hundred times when we press once.
The second condition checks whether enough time has passed since the last reading.
This makes sure the value doesn't bounce between states.
The time is declared in the debounceTime variable. A good value is around 50 milliseconds.
It can be lower, but we will only need it to be lower if we want to press the button more than
20 times a second.
if (buttonValue != previousButtonValue && millis() lastDebounce
>= debounceTime)
60
Chapter 3
At the end, we need a short delay to allow the data to be transmitted:
delay(10);
See also
To clearly understand what contact bouncing is, visit http://en.wikipedia.org/wiki/
Debounce#Contact_bounce.
Getting ready
The following are the ingredients required for this recipe:
Three buttons
How to do it
We implement a simple configuration using only three buttons on the same pin. Here are
the steps:
1. Connect the Arduino GND to a long strip on the breadboard. Also connect the
Arduino 5V to a long strip.
2. Connect one of the resistors from the GND strip to an analog pinhere, pin A0on
the Arduino.
3. Connect three resistors in series starting at the 5V strip.
4. At each junction of two resistors, connect one button. Also connect the third button
at the end of the resistor series.
5. Connect the other terminals of the buttons together and to the A0 analog pin on
the Arduino.
61
Schematic
This is one possible implementation. Other analog pins can also be used.
62
Chapter 3
Code
The following code will read the analog pin on the Arduino board and detect which button
is pressed:
// Declare the Analog pin on the Arduino board
int buttonPin = A0;
void setup() {
// Establish the Serial connection with a baud rate of 9600
Serial.begin(9600);
}
void loop(){
// Read the value of the input. It can vary from 0 - 1023
int buttonValue = analogRead(buttonPin);
if (buttonValue < 200){
// A value under 200 represents no button pushed
Serial.println("0");
} else if (buttonValue >= 200 && buttonValue < 300){
// A value between 200 - 300 represents the third button
Serial.println("S3");
} else if (buttonValue >= 300 && buttonValue < 400){
// A value between 300 - 400 represents the second button
Serial.println("S2");
} else if (buttonValue >= 400){
// A value greater than 400 represents the first button
Serial.println("S1");
}
If the buttons are connected to another analog pin, simply change the
buttonPin variable to the analog pin that has been used.
63
How it works
This is all possible due to an electric circuit called the voltage divider. Each time a button is
pressed, a different voltage divider is created. Each button brings a different voltage to the
analog pin on the Arduino. We can read this analog voltage and attribute a specific value to
each button.
Code breakdown
The code takes the value from the analog pin and checks to which button it corresponds.
It prints out on the serial which button has been pushed.
Here, we declare the analog pin that has been used:
int buttonPin = A0;
In the setup function, we start the serial connection with a speed of 9,600 bits per second:
void setup() {
Serial.begin(9600);
}
In the loop() function, we continuously read the value of the analog pin, which can be
from 01023:
int buttonValue = analogRead(buttonPin);
Then, we check which button is pressed. We should be able to attribute one exact value to
each button. However, due to component tolerances and errors, it's much safer to use an
interval. If the expected value is 250 and the button returns 251, the code will not detect the
button. In this example, the intervals are extreme: 0200 for no button, 200300 for the third
button, 300400 for the second button, and over 400 for the first. The best way to find out
the values is to make a simple program print the analog value of the pin on the serial:
if (buttonValue < 200){
// A value under 200 represents no button pushed
Serial.println("0");
} else if (buttonValue >= 200 && buttonValue < 300){
// A value between 200 - 300 represents the third button
Serial.println("S3");
} else if (buttonValue >= 300 && buttonValue < 400){
// A value between 300 - 400 represents the second button
Serial.println("S2");
} else if (buttonValue >= 400){
// A value greater than 400 represents the first button
Serial.println("S1");
}
64
Chapter 3
In the end, always have some delay when working with serial communication to avoid overflow:
delay(25);
There's more
This is a very useful solution when we need multiple buttons but have only a few pins
left. Another advantage is the time needed to read the analog pin. It takes around 0.1
millisecond to read the analog pin, which solves some problems with debouncing.
Here are a few tips on how to easily do more with this configuration.
More buttons
The title says 1,000 but there are only three buttons here. However, the principle is the same.
We can connect as many buttons as we have to a theoretical maximum of 1023. Each button
needs a resistor, so for a configuration of 100 buttons, we will use 100 resistors in series and
at each junction of two resistors, we will mount a button. Again, we will mount the hundredth
button at the end. The Rd resistor that connects the pin to GND is also mandatory.
The values of the resistors are also very important. It is recommended to have a high value
for the Rd resistor: somewhere between 100K1M ohm. The other resistors should be equal
to make things easier: somewhere between 1K10K ohm.
See also
To understand how a voltage divider works, visit http://en.wikipedia.org/wiki/
Voltage_divider.
65
Button multiplexing
Using a multiplexer, it is possible to make the Arduino read over a hundred buttons easily.
A multiplexer/demultiplexer is an integrated circuit that selects one of several inputs and
forwards them to the output. It requires a few control pins to determine which input to
forward to the output.
Getting ready
Following are the ingredients required for this recipe:
Four buttons
A 4051 multiplexer or similar, which we can find at any electronics store and
online at Digikey, Sparkfun, Adafruit, and so on
How to do it
We implement a simple configuration using only four buttons. Here are the steps:
1. Connect the Arduino GND to a long strip on the breadboard. Also connect the
Arduino 5V to a long strip.
2. Mount the four buttons and connect one of their terminals to the long GND strip.
3. Connect the other terminal of each button to an individual input/output pin on the
4051in this case, pins y0, y1, y2, and y3.
4. Connect the E, VEE, and GND pins of the 4051 multiplexer to the long GND strip.
5. Connect the Vcc pin on the 4051 to the 5V strip on the breadboard.
6. Connect S0, S1, and S2 to three digital pins on the Arduinoin this example,
8, 9, and 10.
66
Chapter 3
Schematic
This is one possible implementation. Other pins can also be used.
67
Code
The following code will read the four buttons connected to the multiplexer by switching the
active pin on it:
// Define the input pin on the Arduino and the 3 selection pins
connected to the 4051
int buttonPin = 2;
int A = 10;
int B = 9;
int C = 8;
void setup() {
// Define pin #2 as input with the pull up resistor on
pinMode(buttonPin, INPUT_PULLUP);
// Define the output pins going to the control lines of the
Multiplexer
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
// Establish the Serial connection with a baud rate of 9600
Serial.begin(9600);
}
void loop(){
// We first read port IO0
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
int buttonIO0 = digitalRead(buttonPin);
// Then we read
digitalWrite(A,
digitalWrite(B,
digitalWrite(C,
int buttonIO1 =
port IO1
HIGH);
LOW);
LOW);
digitalRead(buttonPin);
// Then we read
digitalWrite(A,
digitalWrite(B,
digitalWrite(C,
int buttonIO2 =
port IO2
LOW);
HIGH);
LOW);
digitalRead(buttonPin);
Chapter 3
digitalWrite(A,
digitalWrite(B,
digitalWrite(C,
int buttonIO3 =
HIGH);
HIGH);
LOW);
digitalRead(buttonPin);
How it works
The multiplexer/demultiplexer is a useful component, but, a little tricky to understand.
Here we used a demultiplexer configuration. Each demultiplexer has one output and a number
of inputsin our case, eight. Also, it has control linesin our example, three. Each control line
represents a number: power of 2 minus 1. For the 4051, A = 1, B = 2 and C = 4. If we want
to read input IO5, we set A and C to HIGH and S1 to LOW. This means the output will be
connected to A + C = 5 input; therefore, pin IO5.
Basically, a multiplexer gives the power to connect one Arduino pin to one I/O pin on
the multiplexer. Only one pin can be connected at any particular time.
Code breakdown
The code commands the connection on the multiplexer using the three command lines. It uses
one input digital pin to get the value from the buttons and prints it on the serial connection.
Here, we declare the used pins:
int
int
int
int
buttonPin = 2;
A = 10;
B = 9;
C = 8;
69
HIGH);
LOW);
LOW);
digitalRead(buttonPin);
We do this for each button we want to read and then we print the output values on the serial.
There's more
Here we have only four buttons on four pinsnot a very good ratio of pins to buttons.
However, for the same number of pins we can get eight buttons, as there are four free
pins on the multiplexer.
More buttons
Even eight buttons on four pins is not too much. There are 16-channel multiplexers, such as
the 4067 that require four control lines, totaling sixteen buttons on five pins. We can go even
further! We can use more multiplexers, and we only need one new line for each multiplexer to
connect to its output while sharing the control lines. Using a 4067 and all the pins, except 0
and 1, on the Arduino Uno, we can read 224 buttons. On the Arduino Mega, this will result in
800 buttons. The sky is the limit with multiplexers.
See also
For an in-depth explanation on multiplexers, visit http://en.wikipedia.org/wiki/
Multiplexer.
70
www.PacktPub.com
Stay Connected: