Sub topic 13
Resolution and voltage on other boards
This chapter covers how the ADC resolution and reference voltage differ between Arduino boards, and how to change the potentiometer sketches so they work on any of them. It assumes you have read the earlier chapters. You should know how analogRead() converts a voltage to a count, how to convert that count to volts, and how map() rescales a range.
What changes between boards
Earlier chapters used the Uno's numbers: a 10-bit ADC (analog-to-digital converter), readings from 0 to 1023, and a 5 V reference. Two things in the sketches depend on those numbers:
- The maximum reading, 1023.
- The full-scale voltage, 5.0 V.
On other boards either value can be different. The table lists common families. Check the documentation for your exact board, because variants within a family differ.
| Board family | Microcontroller | Native ADC | Default `analogRead()` range | Logic and ADC voltage |
|---|---|---|---|---|
| Uno R3, Nano, Mega 2560, Leonardo | AVR ATmega | 10-bit | 0 to 1023 | 5 V |
| Uno R4 Minima and WiFi | Renesas RA4M1 | up to 14-bit | 0 to 1023 | 5 V |
| Zero, MKR family | SAMD21 | 12-bit | 0 to 1023 | 3.3 V |
| Due | SAM3X8E | 12-bit | 0 to 1023 | 3.3 V |
| Nano 33 BLE | nRF52840 | 12-bit | 0 to 1023 | 3.3 V |
| ESP32 boards | ESP32 | 12-bit | 0 to 4095 | 3.3 V |
Most boards with a better-than-10-bit ADC keep a 10-bit default so that old sketches behave the same. You opt in to the higher resolution with analogReadResolution(). ESP32 boards using the Espressif core are an exception, because they default to 12 bits.
Step size depends on both numbers
The smallest voltage change the ADC can report is one step. It is the reference voltage divided by the number of steps. A 10-bit converter has 1024 steps, a 12-bit converter has 4096, and a 14-bit converter has 16384.
The sketches in this tutorial divide by 1023, the largest reading, so the top reading maps exactly to the reference. The AVR datasheet describes the conversion as raw = Vin × 1024 / Vref, so the true step is Vref / 1024. The difference is under 0.1% and does not matter for a potentiometer, but you may see either form in other code.
| Configuration | Step size |
|---|---|
| 10-bit, 5 V reference | about 4.9 mV |
| 10-bit, 3.3 V reference | about 3.2 mV |
| 12-bit, 3.3 V reference | about 0.81 mV |
| 14-bit, 5 V reference | about 0.31 mV |
| 10-bit, AVR 1.1 V internal reference | about 1.1 mV |
Higher resolution does not make a reading more accurate. A 12-bit ADC can report finer steps, but the noise from the earlier chapter still moves the value around. On a noisy board the lowest two bits of a 12-bit reading may be mostly jitter. The averaging and deadband techniques from earlier chapters matter more at high resolution. A deadband of 4 counts on a 10-bit scale is about 16 counts on a 12-bit scale.
The knob fraction does not change
A potentiometer wired as a voltage divider is ratiometric. Its output is a fixed fraction of whatever voltage powers it. If the ADC uses the same supply as its reference, the reading is also that fraction of full scale. Both voltages cancel.
With the knob at the midpoint, the reading is about half of full scale on any board:
- 512 of 1023 on a 10-bit ADC.
- 2048 of 4095 on a 12-bit ADC.
This is why map(raw, 0, 1023, 0, 255) produces the right brightness only when the maximum is really 1023. On a 12-bit board the same code reaches full brightness at one quarter of the knob's travel, because readings from 1023 to 4095 all map above 255. The fix is to use the real maximum.
Ratiometric measurement also explains why the default AVR reference, the board's supply voltage, can be acceptable even when USB power sags to 4.7 V. The raw reading of the pot stays correct. Only the conversion to volts is wrong, because the code assumed 5.0 V.
Wiring on 3.3 V boards
The pot must be powered from the board's own logic voltage.
Check three things before wiring:
- Which pin is the board's voltage output (
3V3,3.3V, or5V). - That the analog pin number you use exists on the board.
- That nothing else in the circuit, such as a 5 V sensor, can put 5 V on the analog pin.
A sketch that adapts to the board
The sketch below keeps the board-specific numbers in three constants at the top, then derives everything else from them. It reads the pot, converts the reading to volts, and expresses it as a percentage of travel.
const int POT_PIN = A0;
const int ADC_BITS = 12; // 10 on AVR boards and the R4 default
const float VREF = 3.3; // 5.0 on Uno R3, Nano, Mega, Leonardo, R4
const long ADC_MAX = (1L << ADC_BITS) - 1;
void setup() {
Serial.begin(9600);
#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) || \
defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_ARCH_MBED) || \
defined(ARDUINO_ARCH_ESP32)
analogReadResolution(ADC_BITS);
#endif
}
void loop() {
int raw = analogRead(POT_PIN);
float volts = (float)raw * VREF / ADC_MAX;
int percent = map(raw, 0, ADC_MAX, 0, 100);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" volts=");
Serial.print(volts, 3);
Serial.print(" percent=");
Serial.println(percent);
delay(100);
}How it works:
ADC_MAXis computed from the bit count, so changingADC_BITSupdates every calculation.1L << 12is 4096, and subtracting 1 gives 4095.- The
#ifblock callsanalogReadResolution()only on cores that provide it. AVR boards such as the Uno R3 have no such function and always return 10-bit values, so leaveADC_BITSat 10 there. If you set a different value on a core that lacks the function, the math will assume the wrong maximum. - On ESP32 boards, which already default to 12 bits, the call just makes the setting explicit.
To drive the LED from the earlier chapter on a 12-bit board, replace the hard-coded 1023 with the same constant:
int brightness = map(raw, 0, ADC_MAX, 0, 255);
analogWrite(LED_PIN, brightness);The output range of analogWrite() stays 0 to 255 by default on all of these boards. Some cores also offer analogWriteResolution(), which changes it, so check your core before assuming.
Changing the reference on AVR boards
AVR boards let you choose the reference with analogReference(). The default uses the supply voltage. The INTERNAL option selects a built-in reference, which is 1.1 V on the Uno R3 and Nano. The Mega 2560 uses INTERNAL1V1 and INTERNAL2V56 instead. The EXTERNAL option uses a voltage you apply to the AREF pin.
A lower reference gives finer steps for small signals, because 1.1 V is spread over the same 1024 values. It also means any input above the reference simply reads 1023. A pot powered from 5 V would saturate at about 22% of its travel with a 1.1 V reference.
After switching references, the first few readings may be inaccurate. Read and discard one or two values before trusting the result. The Uno R4 and other newer cores have their own analogReference() options with different names and voltages, so look up your core before using it.
Gotchas on specific boards
- ESP32 linearity: The ESP32 ADC is not perfectly linear. With default settings, readings near 0 V and near 3.3 V are compressed, so the extremes of the knob may read as 0 or 4095 over a small range of travel. Calibration functions and attenuation settings exist, but for a simple dimmer the effect is small.
- ESP32 pin limits: On the original ESP32, the ADC2 pins cannot be used for
analogRead()while Wi-Fi is active. Use ADC1 pins when your sketch also uses Wi-Fi. - Pin names:
A0is defined on most boards, but its physical position and the number of analog pins differ. Use the symbolic name rather than a number. - Mixed voltages: Reading a 5 V sensor on a 3.3 V board needs a divider or level shifter, not just a different constant in the sketch.
A checklist for porting
The last step is worth doing once on any new board. Measure the voltage on the pot's wiper with a multimeter and compare it with the sketch's volts value. A mismatch usually means a wrong VREF, a wrong ADC_BITS, or a reference that differs from the supply.
Exercise
A sketch written for an Uno R3 uses map(raw, 0, 1023, 0, 255) to dim an LED. You move it unchanged to an ESP32 board whose analogRead() returns 0 to 4095. At roughly what fraction of the knob's travel does the LED reach full brightness, and what is the fix?
Show answer
Full brightness is reached when raw reaches 1023, which is about 25% of travel (1023 / 4095). Beyond that, map() produces values above 255, which analogWrite() treats as full on. The fix is to use the real maximum: map(raw, 0, 4095, 0, 255), or better, map(raw, 0, ADC_MAX, 0, 255) with ADC_MAX computed from the resolution.
Exercise
On a 3.3 V board with a 12-bit ADC and a 3.3 V reference, the sketch prints raw=3102. What voltage does the pot's wiper have, and what is the size of one step?
Show answer
Volts = 3102 × 3.3 / 4095 ≈ 2.50 V. One step is 3.3 / 4095 ≈ 0.81 mV (the datasheet form 3.3 / 4096 gives the same value to two digits).
Exercise
You use analogReference(INTERNAL) on an Uno R3 (1.1 V reference) while the pot is still powered from 5 V. What does the reading do as you turn the knob from one end to the other, and why?
Show answer
The reading rises with the knob until the wiper reaches 1.1 V, about 22% of travel, then stays at 1023 for the rest. Any input at or above the reference reads as full scale, so the upper part of the range is lost.