Simple Digital and Analog Input

5.0 Introduction

The Arduino’s ability to sense digital and analog inputs allows it to respond to you and to the world around you. This chapter introduces techniques you can use to monitor and respond to these inputs: detect when a switch is pressed, read input from a numeric keypad, and read a range of voltage values.

This chapter covers the Arduino pins that can sense digital and analog inputs. Digital input pins sense the presence and absence of voltage on a pin. Analog input pins measure a range of voltages on a pin.

Figure 5-1 shows the arrangement of pins on the Arduino Uno. This pin arrangement is used by many Arduino-compatible boards, including the Adafruit Metro line and SparkFun. See this list of the official boards, which links to connection information for each. If your board is not on that list, check your board supplier’s website for connection information.

The Arduino function to detect digital input is digitalRead, and it tells your sketch if a voltage on a pin is HIGH or LOW. HIGH is between 3 and 5 volts for boards such as the Uno (between 2 and 3.3 volts on ARM-based boards and any other 3.3V boards), LOW is 0 volts. The Arduino function to configure a pin for reading input is pinMode(pin, INPUT).

On a board with the Uno-style pin layout (including the Arduino Leonardo, several of the Adafruit Metro boards, and SparkFun RedBoard), there are 14 digital pins (numbered 0 to 13) as shown at the top of Figure 5-1. On the Uno and ...

Get Arduino Cookbook, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.