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

  1. ADC samples the sensor input.
  2. ADC compares sample level using selected reference mode behavior.
  3. Sample is converted to digital count (for ESP32 often 0-4095).
  4. Your voltage math scales that count using chosen assumptions.

Reference Voltage Concept

General conversion idea:

normalized = raw / maxCount
estimatedVoltage = normalized * assumedFullScaleVoltage

If 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

ModeIdeaNotes
DEFAULTUse default board ADC reference behaviorBest starting point for beginners
INTERNALUse internal reference option if supportedBoard-specific availability
EXTERNALUse external reference input sourceNeeds 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

  1. analogReference(DEFAULT) sets default ADC reference mode.
  2. raw = analogRead(SENSOR_PIN) captures ADC count.
  3. normalized maps count into 0.0 to 1.0 range.
  4. estimatedVoltage scales normalized value by 3.3 assumption.
  5. LED threshold demonstrates analog-driven digital reaction.
  6. Serial prints raw, normalized, and voltage estimate for debugging.

What Happens Inside

  1. Reference mode is configured in setup before loop sampling.
  2. ADC conversion engine samples input and generates raw count.
  3. Program-side math transforms raw count to scaled values.
  4. Control and serial output consume transformed values.
Raw ADCNormalizedEstimated V (3.3V assumption)
00.0000.000 V
20480.500~1.650 V
40951.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.

Run in Simulator