মূল বিষয়বস্তুতে যান
Learn/Analog input & PWM/Reading analog values
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
Open in Lab
UNOATmega328P: The 8-bit AVR microcontroller that runs your sketch (16 MHz, 32 KB flash, 2 KB RAM).ATmega328PUSB-B: USB connection for uploading sketches and for the Serial Monitor. Also powers the board (5 V).DC jack: Barrel jack for 7–12 V from a battery pack or adapter (centre positive).ATmega16U2: Converts USB to serial for uploads and Serial.print().RESET: Restarts your sketch from the beginning (setup() runs again).L (D13): Built-in LED connected to pin 13 (LED_BUILTIN).ON: Power indicator.5 V regulator: Turns 7–12 V from VIN/jack into a steady 5 V.16 MHz: Clock source: the microcontroller runs 16 million cycles per second.ICSP: In-circuit programming header (SPI). Used to burn a bootloader; not needed for normal uploads.50%220 Ω+
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

1On a Uno, what reading do you get for 2.5 V on A0?

2Which pin of a potentiometer gives the variable voltage?