pin 2potMCUATmega328P011void setup() {2 pinMode(2, INPUT);3}4void loop() {5 int v = digitalRead(2);6}LOW = 00 to 1.5 Vno promiseHIGH = 13.0 V and up1.5 V3.0 VV0.0 V
Back: a potentiometer wired to pin 2, the microcontroller and the bit it readsFront: the firmware card and a 0 to 5 V tower with the LOW, no-promise and HIGH zonesMovers: the voltage on the wire and a pointer at the pin's voltageA potentiometer puts a voltage on pin 2 of an ATmega328P running on 5 V, and digitalRead(2) turns it into one bit. 1.5 V or less is ensured to read LOW, 3.0 V or more is ensured to read HIGH, and between them the datasheet makes no promise. A real output's 4.2 V leaves 1.2 V of room for noise; a pin with nothing connected floats and reads at random.

A pin that listens

  1. A GPIO pin (general-purpose input/output) can drive or listen. pinMode(2, INPUT) makes pin 2 listen. An input only senses a voltage: it lets less than 1 µA flow, so it hardly disturbs what it measures.

Reading one bit

Designing for noise

What happens

;; step 1/8 · input
line 2    pinMode(2, INPUT);
pin 2     input, high impedance ("Hi-Z")
current   under 1 µA leaks in or out
INPUT also turns the internal pull-up off

The limits come from the ATmega328P datasheet (DS40002061B, Table 30-1) for VCC from 2.4 V to 5.5 V: VIL at most 0.3 × VCC and VIH at least 0.6 × VCC. The datasheet says "Max" is the highest voltage ensured to read low and "Min." the lowest ensured to read high. Each input passes through a Schmitt trigger (datasheet §14.2.5, Figure 14-2): once it switches, the voltage must move back a little before it switches again. This gap is called hysteresis. The datasheet shows it only as typical curves and guarantees no minimum for ordinary pins. Where a chip switches inside the no-promise zone also varies from chip to chip. The figure's simulated chip switches at 2.2 V with 0.25 V of hysteresis; both are made-up example values, not datasheet figures.