Sub topic 1
Digital Signals: What HIGH and LOW Mean
This chapter assumes you can write a basic Arduino sketch with setup() and loop(), use a breadboard and jumper wires, and apply Ohm's law to simple resistor circuits.
Prerequisites
You should know the setup() and loop() structure of a sketch, how to wire a breadboard, and the basics of voltage, current, and resistance. No prior experience with digital inputs is needed.
Digital signals: what HIGH and LOW mean
A digital pin reads its input as one of two levels. HIGH means the pin sees a voltage close to the board's logic supply, which is 5 V on a classic Uno. LOW means the pin sees a voltage close to 0 V, or ground (GND). Voltages between these levels are not valid digital states, so the chip cannot reliably classify them.
Setting a pin as input and calling digitalRead
Call pinMode() once in setup() to tell the chip that a pin receives signals. Then call digitalRead() in loop() to get the current level.
const int buttonPin = 2;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
int level = digitalRead(buttonPin);
Serial.println(level);
delay(200);
}How a push button connects to a pin
A push button is a switch that joins two terminals while pressed. Connect one terminal to the input pin and the other to GND. Pressing the button ties the pin to ground, so it reads LOW. Releasing the button opens the circuit.
Why an unconnected input reads random values
When the button is open, the pin is not tied to either level. It is called a floating input. The input circuitry picks up stray electrical noise from nearby wires, so digitalRead() can switch between HIGH and LOW without any change from the button. The next chapter adds a pull-down or pull-up resistor to give the pin a defined level.
Exercise
The button is open and the pin is floating. What can digitalRead() return?
Show answer
It can return HIGH or LOW at random, because a floating input has no defined voltage.