Arduino 02: Digital INPUTs

The Ultimate Getting Started Guide

Last time we looked at how to use Digital Outputs in our projects, today I will be going over digital inputs. At a basic level, digital inputs allow us to add buttons and switches to our projects. I’ll also cover “if” statements and button debouncing. Let’s get started!

What is a Digital Input?

A digital input on the Arduino Uno makes use of one of the 14 digital pins (the same ones we used earlier as digital outputs) to take a digital voltage reading. Each of these 14 pins can be used as an input. A digital input can have two possible logical states, HIGH (5V) or LOW (0V). As such, digital inputs can be used to detect simply binary on/off (high/low) situations in our circuit. This makes them great for detecting whether a button is pressed or whether a switch is turned on.

Practical Uses

Digital inputs are useful when implementing buttons and switches that will interact with the logic in our program. We can use them to trigger functions that we create. In today’s example, we will be using a digital input to interface with a simple button, which we will then use to execute code that will turn on a light. Buttons can be used to make a variety of things happen in a project by triggering code. In a home automation setting, digital inputs and buttons (end-stop switches and magnetic switches) can be used to detect whether a door or window is open or whether a drawer is open/closed.

You could also use digital inputs in any situation where you want to detect whether a circuit is outputting a 5V (HIGH) logic signal or a 0V (LOW) logic signal. Although simple, digital inputs can have a variety of surprisingly interesting uses. 

Parts List

Computer (PC/Mac/Linux)

Arduino IDE (click to download)

Arduino Uno (or alternative)

USB Cable

1x 3mm or 5mm LED (any kind)

1x Breadboard

Jumper cables

1x Assorted set of resistors (10k for button + 220 Ohm for LED)

1x Push Button (momentary switch 2 pin or 4 pin)

The Circuit

Assemble your circuit following the interactive diagram below. I recommend watching the video for exact instructions and electrical calculations.

The Code

If Statements

An “if statement” is a conditional statement that runs a piece of code if a set of conditions is met. When a condition inside the parenthesis of an “if statement” is true, the code encapsulated within the curly brackets is executed. It is common to place an “else if” or “else” statement immediately after an “if” statement. If an “if statement” turns out to be false, the program will run the code contained in the “else” block. This will only happen if the condition test in the “if” statement turns out to be false. “else if” statements run only if the “if” statement is false and “if” the condition in the “else if” is true.

int ledpin=13;

int button=4;

void setup() {
// put your setup code here, to run once:
pinMode(ledpin, OUTPUT); //set the ledpin (pin 13) as an output
pinMode(button, INPUT); //set the button pin (pin 4) as an input
}

void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(button)==HIGH){
digitalWrite(ledpin,HIGH);
}
else{
digitalWrite(ledpin,LOW);
}

}

Button Debouncing

Button debouncing is not a function nor conditional operator, it is a technique used to “debounce” a momentary switch or button. When a button is pressed, the Arduino takes this input and makes a change to our variable that stores our button state. Due to the rapid rate at which the Arduino measures or “polls” our button along with the fact that our button may send our erroneous states between HIGH and LOW as it is being pressed, means that our input may not be accurate. This can manifest itself as the button being pressed twice, several times or not at all, a pretty annoying phenomenon. Thankfully its fairly easy to fix this “bouncing” between logic states. The easiest fix is to add a delay of anywhere from 50ms to 150ms (approximately) which gives us enough time to lift our finger off the button before any additional changes occur in the code logic. Unfortunately, this is a rather primitive solution that doesn’t always work and adds unnecessary delays to our code. I’ll show you one possible and fairly common fix to the problem.

My favorite way to debounce buttons with the Arduino is based on an example from an Arduino book I read back when I was just getting started with the platform. It is based on the principle that we can compare the current state of a button to its previous state to determine in our logic whether the button press was intentional and intended to change the state of a variable.

 

 

/* In the Arduino IDE “File > Examples> Debounce. Please note this is not my code, all credit goes to David A. Mellis, Limor Fried, Mike Walters, Arturo Guadalupi who created this Arduino Example Sketch

http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won’t change. They’re used here to set pin numbers:
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledState);
}

void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you’ve waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() – lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it’s been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

// set the LED:
digitalWrite(ledPin, ledState);

// save the reading. Next time through the loop, it’ll be the lastButtonState:
lastButtonState = reading;
}

Tutorial / Build Guide

Check out the video for a step by step introduction to Arduino and installation tutorial. It is important to have everything set up correctly as the following tutorials will build on this one.

Conclusion

Congratulations! You now know how to build basic circuits using the Arduino’s Digital Input pins and functions. We’ve also demystified “if” statements and put them to good use.

In our next tutorial, I’ll be covering Serial communication and how we can have our Arduino communicate with our computer! Stay tuned!

Downloadable Resources

Download Sketch

Download Circuit Diagram

More Great Arduino Tutorials

Rapid Prototyping with 3D Printers

Rapid Prototyping with 3D Printers

  With the advent of 3D printers anyone can create parts or products from the comfort of their home. In my latest electronics project, WOOF IOT, I used Fusion 360 and my Ender 3 Pro printer to design a case for my Internet of Things Device. What is Rapid...

Introduction to Arduino

Introduction to Arduino

When I first started with Arduino back in early high school I was amazed by the versatility and power of the platform. Although there are many electronics platforms available today, Arduino is one of the best and a great place to start learning. In my article I will...

Hello World! Introducing myself

Hello World! Introducing myself

Hello World! Thank you for visiting my little corner of the internet. As the name suggests, I’m Filip, an International Business Administration bachelor student at the Rotterdam School of Management. Restless and curious, I enjoy...