Digital Input Component: Pushbutton/Switch

Objectives and Overview

This lesson introduces digital input and the Arduino function digitalRead(). Additionally, this lesson includes boilerplate digital input code that you’ll build on.

Lesson Objectives

  • Understand digital input and how it differs from digital output.
  • Explain the purpose of the Arduino digitalRead() function and how it relates to digital input.

Working with Digital Input

Digital Input is the next concept after Digital Output. Before starting, let’s do a quick refresher about the basics of Digital Output.

Digital Output Recap

While working with the LED you used digitalWrite() to send a HIGH or LOW signal to an LED pin. The HIGH signal corresponded to the LED turning ON, whereas the LOW message corresponded to the LED turning OFF. Sending either an ON or an OFF signal is the core aspect of Digital Output.

The most basic example of this ON/OFF output is the LED changing states. Next, you’ll be exploring and experimenting with an ON or OFF input.

Basics of Digital Input

Next, let’s continue with the LED example, but instead of looking at the output you’re going to now consider the input.

Recall that digital has two states. The form that this takes for input would again be ON and OFF. The most basic example of digital input is a light switch. A typical light switch has two states: ON and OFF. This is digital input.

Digital Input with Arduino

Digital output uses the command digitalWrite()Digital input has the corresponding command digitalRead().

digitalRead() works similarly to its complementary pair in that it requires a pin as a parameter. You’ll also need to set pinMode() for input instead of output like you’ve used thus far.

The digital input “Hello World” project is to use a pushbutton to turn an LED on and off. Previously, the LED state was controlled entirely by the code that you sent to the board. This time, a physical button is going to be controlling the state.

Let’s examine some pushbutton code from the Arduino Reference:

int ledPin = 13; // LED connected to digital pin 13 int inPin = 7; // pushbutton connected to digital pin 7 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output pinMode(inPin, INPUT); // sets the digital pin 7 as input } void loop() { val = digitalRead(inPin); // read the input pin digitalWrite(ledPin, val); // sets the LED to the button's value }
Code language: JavaScript (javascript)

Code Breakdown

Now, let’s break this code down by section:

Global Variables:

  • 3 global variables are initialized: ledPininPin, and val. Note that these are all type int
  • inPin is given a value of 7, meaning that digital pin 7 is the pin where the pushbutton will be attached.

void setup() Breakdown:

  • pinMode() is used to set the states of each pin. Note thatledPin is set to OUTPUT and the inPin is set to INPUT

void loop()

  • the val variable is now used to store the value from the button. Remember that inPin is the pin that the pushbutton is connected to and that digitalRead() is used to grab the value.
  • digitalRead(inPin) returns either a HIGH (on) or LOW (off) value based on the state of the button.
  • digitalWrite(ledPin) then sends the output message to the LED based on the button’s state.

The Push Button

Now that you’ve gotten a glimpse at the code for a basic pushbutton project, let’s examine the pushbutton itself.

When the button is pushed it creates a connection between two points on a circuit. In the above example, when the button is pressed down it completes the circuit resulting in the LED turning on.

When the button is released, the circuit is no longer complete and the LED turns off.

Essentially, the button connects to the 5V (POWER) when the button is pressed. This results in a HIGH reading, which then turns ON the LED. Once the button is released, 5V is essentially disconnected, which then turns OFF the LED.

Exploring digitalRead()

At this point, you should know the difference between digital input and digital output, and have honed your digitalWrite() skills. But you haven’t dived into digitalRead() yet!

digitalRead() reads the state of a digital pin. This means that you can use this to check whether or not a certain state is HIGH or LOW.

Here is some basic code from the same Arduino Reference page:

If you’re looking for the complete code, head to the Gist link.

Note that these are snippets from the entire code.

int buttonState = 0; // variable for reading the pushbutton status ... // other code cut out from the snippet void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
Code language: JavaScript (javascript)

In this example, the variable buttonState is declared globally. It has a 1 or a 0 as its value. The initial value is 0.

There is a conditional statement to check the state of the button in the void loop(). This is done by digitalRead(buttonPin) which checks to see if the button state is HIGH or LOW. If the reading is HIGH, then the LED should be ON. If the button state reads LOW, then the LED should be OFF.

The next challenge, the LED Flashlight, involves both digital input and digital output and you’ll use a pushbutton and LEDs.


Resources

Arduino Reference Documentation: This has more details about buttons in Arduino, and is where the example code in this lesson are sourced from.