Blog
Arduino pins: digital vs analog, explained simply
Alessandro Panait·
An Arduino talks to the world through its pins. Getting the pin type right is the first thing that trips up beginners.
Digital pins: on or off
A digital pin has two states: HIGH (usually 5V) or LOW (0V). Use them for anything that is binary:
- Reading: a button pressed or not, a limit switch, a line sensor that says black or white.
- Writing: turning an LED or a relay on and off.
pinMode(2, INPUT);
int pressed = digitalRead(2); // 0 or 1
digitalWrite(13, HIGH); // LED onAnalog pins: a range of values
Analog input pins (marked A0, A1, ...) read a voltage between 0 and 5V and turn it into a number from 0 to 1023. Use them for sensors that give a smooth value:
- a light sensor, a potentiometer, many distance sensors.
int value = analogRead(A0); // 0..1023PWM: fake analog output
Arduino cannot output a true in-between voltage, but pins marked with ~ can fake it with PWM (rapid on/off pulses). That is how you dim an LED or set motor speed. We cover this in the PWM lesson.
The mistake to avoid
Reading a button with analogRead or a light sensor with digitalRead is the classic beginner bug. Match the pin type to the signal: binary goes digital, smooth goes analog.