Analog I/O
Lesson 69: Using analogReadResolution()
Learn how analogReadResolution() sets ADC bit depth, how it affects analogRead() values, and how to choose the right resolution for sensors.
Progress indicator
Lesson 69 of 72
Learning Objectives
- Understand what analogReadResolution() changes in the ADC.
- Learn valid resolution ranges and return value ranges.
- Map readings correctly after changing resolution.
- Know when higher or lower resolution is appropriate.
- Avoid mistakes with mapping and reference assumptions.
Concept Explanation
What is analogReadResolution()
analogReadResolution(bits) sets the ADC bit depth. The return range becomes0..(2^bits - 1).
analogReadResolution() Syntax
analogReadResolution(12); // ESP32 12-bit -> 0..4095
analogReadResolution(10); // 10-bit -> 0..1023ADC Bit Depth Concept
More bits give finer steps but may expose more noise. Fewer bits give coarser steps but faster, simpler mapping.
Resolution vs Accuracy
Resolution is step size; accuracy depends on reference stability, input impedance, and noise. Changing resolution does not automatically improve accuracy.
When to Use analogReadResolution()
- Match ADC range to the precision your sensor needs.
- Simplify mapping if you prefer 0–1023 math (10-bit).
- Increase resolution for finer control when noise is manageable.
Example Code
Set 12-bit ADC, read a sensor, map to PWM, and view values in Serial Monitor.
const int LED_PIN = 2;
const int SENSOR_PIN = A0;
int sensorValue = 0;
int pwmValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
analogReadResolution(12); // set ADC to 12-bit (0..4095)
}
void loop() {
sensorValue = analogRead(SENSOR_PIN);
pwmValue = map(sensorValue, 0, 4095, 0, 255);
analogWrite(LED_PIN, pwmValue);
Serial.print("sensor=");
Serial.print(sensorValue);
Serial.print(", pwm=");
Serial.println(pwmValue);
delay(300);
}Example Code Explanation
analogReadResolution(12)sets range to 0..4095.analogRead(SENSOR_PIN)returns values in that range.map(... 0, 4095, 0, 255)scales correctly for PWM.- Serial prints show raw reading and mapped PWM for debugging.
delay(300)makes changes easy to see.
What Happens Inside
- ADC bit depth is configured.
- analogRead returns values in the new numeric range.
- Mapping translates the new range into your control range (PWM).
- LED brightness follows the scaled value.
Common Mistakes with analogReadResolution()
- Changing resolution but not updating map() in/out ranges.
- Assuming higher resolution always improves accuracy.
- Ignoring sensor/reference noise and not averaging samples.
Best Practices for analogReadResolution()
- Pick a resolution that matches sensor precision and noise level.
- Update mapping ranges after changing resolution.
- Average multiple samples for smoother readings.
Try it now
Open the simulator, change the sensor slider, and see how resolution affects readings.