Arduino for Beginners · Digital Inputs and Outputs
digitalRead() and a button; pull-up/pull-down resistor
Digital Inputs and Outputs
Introduction
How Arduino reads the outside world: the digitalRead(pin) function returns HIGH or LOW from a pin configured as an input. This lesson dissects wiring a momentary push button (tactile switch) and the key problem of a floating input, which yields random readings. We show two solutions: a pull-down resistor (default LOW, press = HIGH) and a pull-up resistor (default HIGH, press = LOW, inverted logic), the typical external resistor value of 10 kilohms, and the INPUT_PULLUP mode, which enables the ATmega328P built-in pull-up resistor (~20-50 kilohms) and lets you drop the external part. Finally we assemble a button read in the loop() with the result stored in a variable.