Analog I/O
Lesson 68: Using analogRead() for Analog Input
Learn how analogRead() samples analog sensors, what ADC resolution means, and how to map readings into useful values.
Progress indicator
Lesson 68 of 72
Learning Objectives
- Understand what analogRead() does and when to use it.
- Learn ADC resolution and return range for ESP32/Arduino.
- Map analogRead() values into PWM or other scaled ranges.
- Avoid common mistakes with input noise and pin modes.
- Observe readings in Serial Monitor for debugging.
Concept Explanation
What is analogRead()
analogRead(pin) measures the voltage on an analog-capable pin and returns a digital value that represents that voltage relative to the ADC reference.
analogRead() Syntax
int reading = analogRead(A0);How analogRead() Works
- ADC samples the pin voltage.
- Voltage is converted to a numeric value in the current resolution range.
- Result is returned as an integer (e.g., 0–4095 on 12-bit ESP32).
The numeric range depends on ADC resolution (see analogReadResolution()) and reference voltage.
ADC Resolution Concept
On many Arduino boards, analogRead returns 0–1023 (10-bit). On ESP32 with 12-bit resolution, range is 0–4095. Higher resolution gives finer steps but may add noise sensitivity.
analogRead() Return Range
- Default Uno: 0–1023 (10-bit).
- ESP32 typical: 0–4095 (12-bit) unless changed with analogReadResolution().
When to Use analogRead()
- Reading sensors that output analog voltages (pots, LDRs, many analog sensors).
- Measuring variable inputs to drive PWM, thresholds, or control logic.
- Sampling sensor values to log or map into other ranges.
Example Code
Read an analog sensor, map it to PWM, and see LED brightness follow the input.
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);
}
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
analogRead(SENSOR_PIN)captures the sensor voltage as a number.map()scales ADC range to PWM (0–255) for LED brightness.analogWrite()drives LED duty cycle based on mapped value.- Serial prints both raw and mapped values for debugging.
delay(300)slows updates so changes are visible.
What Happens Inside
- ADC samples pin voltage.
- Value returned in resolution range (0..4095 on ESP32 default).
- Mapped to PWM range for output control.
- LED shows brightness proportional to sensor level.
Common Mistakes with analogRead()
- Forgetting the ADC range and assuming 0–1023 on all boards.
- Skipping input stabilization (no delay or averaging) with noisy sensors.
- Mapping with wrong min/max values after changing resolution.
Best Practices for analogRead()
- Check your board’s default ADC resolution and reference.
- Use map() or constrain() to fit the reading into control ranges.
- Log readings in Serial Monitor while tuning thresholds.
Try it now
Open the simulator and see how analogRead() values map to LED brightness.