Analog I/O
Lesson 70: Using analogReference()
Learn how analogReference() affects ADC measurement scaling and when to use default or external reference modes.
Progress indicator
Lesson 70 of 70
Learning Objectives
- Understand what analogReference() controls in analog measurement flow.
- Learn how reference assumptions affect voltage conversion accuracy.
- Differentiate default and external reference concepts clearly.
- Understand common mode names and board-dependent behavior.
- Avoid common scaling and setup-order mistakes in ADC projects.
Concept Explanation
What is analogReference()
analogReference() tells the ADC which reference mode to use for mapping analog voltage to a digital number.
ADC values are always relative to reference behavior, so reference choice directly affects calculated voltage.
analogReference() Syntax
analogReference(DEFAULT);
analogReference(INTERNAL);
analogReference(EXTERNAL);Supported names depend on board family and core implementation.
How analogReference() Works
- ADC samples the sensor input.
- ADC compares sample level using selected reference mode behavior.
- Sample is converted to digital count (for ESP32 often 0-4095).
- Your voltage math scales that count using chosen assumptions.
Reference Voltage Concept
General conversion idea:
normalized = raw / maxCount
estimatedVoltage = normalized * assumedFullScaleVoltageIf assumed full-scale voltage does not match real ADC behavior, calculated voltage will be off.
Default vs External Reference
DEFAULT: board default reference behavior.EXTERNAL: external reference source on supported boards/pins.- External mode requires correct hardware design and board support verification.
analogReference() Modes
| Mode | Idea | Notes |
|---|---|---|
DEFAULT | Use default board ADC reference behavior | Best starting point for beginners |
INTERNAL | Use internal reference option if supported | Board-specific availability |
EXTERNAL | Use external reference input source | Needs valid external wiring and docs check |
When to Use analogReference()
- When sensor scaling assumptions need explicit reference behavior.
- When your board documentation recommends mode configuration.
- When tuning analog measurement consistency across projects.
Example Code
This sketch sets default reference mode, reads ADC value, and estimates voltage for beginner visualization.
const int SENSOR_PIN = A0;
const int LED_PIN = 2;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
// Use board default ADC reference mode.
analogReference(DEFAULT);
}
void loop() {
int raw = analogRead(SENSOR_PIN);
float normalized = raw / 4095.0;
float estimatedVoltage = normalized * 3.3;
if (raw > 2048) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
Serial.print("raw=");
Serial.print(raw);
Serial.print(", normalized=");
Serial.print(normalized, 3);
Serial.print(", estimatedVoltage=");
Serial.println(estimatedVoltage, 3);
delay(600);
}Example Code Explanation
analogReference(DEFAULT)sets default ADC reference mode.raw = analogRead(SENSOR_PIN)captures ADC count.normalizedmaps count into 0.0 to 1.0 range.estimatedVoltagescales normalized value by 3.3 assumption.- LED threshold demonstrates analog-driven digital reaction.
- Serial prints raw, normalized, and voltage estimate for debugging.
What Happens Inside
- Reference mode is configured in setup before loop sampling.
- ADC conversion engine samples input and generates raw count.
- Program-side math transforms raw count to scaled values.
- Control and serial output consume transformed values.
| Raw ADC | Normalized | Estimated V (3.3V assumption) |
|---|---|---|
| 0 | 0.000 | 0.000 V |
| 2048 | 0.500 | ~1.650 V |
| 4095 | 1.000 | ~3.300 V |
Common Mistakes with analogReference()
- Using unsupported reference mode for the board.
- Using wrong voltage assumption in conversion formula.
- Configuring mode too late after many reads.
- Skipping calibration when measurement precision is required.
Best Practices for analogReference()
- Set reference mode in
setup()before repeated analogRead(). - Check board documentation for supported modes and caveats.
- Validate formula assumptions with measured real voltage.
- Keep serial debug values while tuning analog thresholds.
Try it now
Open the simulator workspace and inspect raw ADC values and reference-based scaling.