Reader small image

You're reading from  Arduino Development Cookbook

Product typeBook
Published inApr 2015
Publisher
ISBN-139781783982943
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Cornel M Amariei
Cornel M Amariei
author image
Cornel M Amariei

Cornel Amariei is a Romanian inventor and entrepreneur in the fields of Robotics and 3D printing. He has been working with the Arduino platform since its early days in 2007. His past experience involves large cargo gamma ray scanning robotics, ATM security systems, and blind assisting devices. In his spare time, he is a performing musician playing multiple instruments—predominately the guitar. He is also a swimmer, water polo player, and photographer. Over the years, he has built hundreds of Arduino projects, ranging from flying Quadcopters to levitating magnets and underwater robots. Currently, he splits his time between doing his undergraduate studies in electric engineering and computer science at Jacobs University in Bremen, Germany, and his start-ups and research and development job.
Read more about Cornel M Amariei

Right arrow

Chapter 3. Working with Buttons

In this chapter, we will cover the following recipes:

  • Connecting a button

  • Button with no resistor

  • The toggle. switch

  • Button to serial

  • Button debouncing

  • 1,000 buttons, 1 pin

  • 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.

To keep the example simple, we will connect a button to the Arduino, and whenever we press and hold it, the internal Arduino LED will light up. But first, we need to talk a little about buttons. There are a few common configurations found in everyday electronics. We can categorize buttons/switches based on three main characteristics:

  • Momentary and maintained buttons

  • Open or closed buttons

  • Poles and throws

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 with no resistor


It is simple to connect a button to the Arduino. You need the button, some wires, and a resistor. But what if we no longer need the resistor and want to still be able to use the button with no false readings?

The resistor is mandatory for proper operation of a button, and everybody will insist on using it. However, there is a little secret embedded in each Arduino pin. Each pin already has a pull-up resistor that we can enable with just one small change in our code.

Getting ready

For this recipe, you will need just two components:

  • An Arduino board connected to a computer via USB

  • 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.

Schematic

Here is one implementation on the 12th digital pin. Other digital pins can also be used.

Here is a simple way of wiring the button:

For most buttons with standard through-hole terminals, we can directly input the...

The toggle switch


A toggle switch can be very useful for various projects. It can hold one or more constant states. For example, we can have a few of them and configure a certain system to work a certain way based on the configuration. This is done all the time on computer motherboards and other electronic devices.

A two-state toggle switch is just like a standard push button; only, it remains in the state we put it in. An on-off switch is a two-state toggle switch. It becomes more useful when we have a three-state toggle switch as in this recipe. It has two usable states and an off state.

In this recipe, we will use a basic toggle switch to light up two LEDs. When the toggle switch is in one end position, only one LED will be switched on. If it is in the other end position, the other LED will be switched on. Finally, if the toggle switch is in the center, both LEDs will be switched off.

Getting ready

The following are the ingredients required to execute this recipe:

  • An Arduino board connected...

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:

  • An Arduino board connected to a computer via USB

  • A button

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...

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!

Another very important application of this is reading a button only once. When we press the button, we keep it pressed for a few milliseconds. In this time, the Arduino can read it hundreds, even thousands of times. It detects a few hundred times instead of once that we pushed the button. This is the primary use of debouncing in the Arduino world.

Getting...

1,000 buttons to 1 pin


One button, one pin—that is the way things are usually done on Arduino boards. But it is so limiting. There are some tricks that let you connect more than one button to a pin. Actually, it is even possible to connect 1,000 buttons to just 1 pin. We will explore this possibility in this recipe.

Getting ready

The following are the ingredients required for this recipe:

  • An Arduino board connected to a computer via USB

  • A breadboard and jumper wires

  • Three buttons

  • Four resistors of equal value: 1K ohm works well

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 pin—here, pin A0—on 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...

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:

  • An Arduino board connected to a computer via USB

  • A breadboard and jumper wires

  • 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 4051—in this case, pins y0, y1, y2, and y3.

  4. Connect...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Arduino Development Cookbook
Published in: Apr 2015Publisher: ISBN-13: 9781783982943
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Cornel M Amariei

Cornel Amariei is a Romanian inventor and entrepreneur in the fields of Robotics and 3D printing. He has been working with the Arduino platform since its early days in 2007. His past experience involves large cargo gamma ray scanning robotics, ATM security systems, and blind assisting devices. In his spare time, he is a performing musician playing multiple instruments—predominately the guitar. He is also a swimmer, water polo player, and photographer. Over the years, he has built hundreds of Arduino projects, ranging from flying Quadcopters to levitating magnets and underwater robots. Currently, he splits his time between doing his undergraduate studies in electric engineering and computer science at Jacobs University in Bremen, Germany, and his start-ups and research and development job.
Read more about Cornel M Amariei