Arduino User Input Devices
This is the first lesson in the Arduino User Input devices. You will learn which devices work as inputs for the Arduino, how to connect and use them.
A button is a simple on-off switch. There are many kinds of buttons, distinguished by the mechanism used to close or open a circuit, but essentially all buttons belong to one of two families: those that keep the connection in either an open or a closed state, and those that return to their original (default) state.
Here are some examples of switches.
Clock-wise from the top:
- A toggle switch. It stays open or closed after pushing it.
- An on/off switch that also remains open or closed.
- A momentary button that remains closed while pressure is applied to it, then returns to the open position.
A keyboard key or a door bell button are both momentary buttons. Momentary buttons are also known as "biased" because they have a tendency to return to their original position. A light switch, on the other hand, stays at the position it was pushed to, so it is often called "un-biased".
Let's create a simple circuit to demonstrate a button.
We don't really need the Arduino for this. A battery, an LED, a resistor and the button itself would suffice.
But, using the Arduino is simple enough, and it's already got an LED in pin 13 we can use anyway, and no need to worry about a battery pack.
Plus, we can use the monitor to actually see a message when the button is pressed.
So, we'll setup this circuit (below).
Once assembled, the project will look like this (below as well):
The sketch is simply taking a reading from digital pin 2, where one of the pins of the button is connected, and writing a value to digital pin 13, where the LED is connected.
I could have written this script to be even smaller, but I will leave that for you to do as an exercise.
And that's how you can use a momentary button with the Arduino!
"Arduino User Input Devices" series
Ready for some serious Arduino learning?
Start right now with Arduino Step by Step Getting Started
This is our most popular Arduino course, packed with high-quality video, mini-projects, and everything you need to learn Arduino from the ground up.
Jump to another article in this series.