Analog input & PWM · Lesson
11 min read
Reading analog values
Potentiometers, voltage dividers and the 10-bit ADC.
What You Will Learn
- Wire a potentiometer as a voltage divider
- Convert readings with map()
- Calculate the voltage from a reading
A potentiometer is a resistor with a sliding contact (the wiper). With its ends on 5 V and GND, the wiper outputs any voltage between them.
analogRead(A0) converts 0–5 V into 0–1023. To get volts back: volts = reading × 5.0 / 1023.
Arduino C++
int raw = analogRead(A0); float volts = raw * 5.0 / 1023; int percent = map(raw, 0, 1023, 0, 100);
Potentiometer dimmerarduino-uno
Note: Drag the knob slider and watch raw and brightness in the Serial tab.
map() uses whole numbers. map(512, 0, 1023, 0, 255) gives 127.
Check Your Understanding
Test what you learned in this lesson