🛠 Lire un capteur analogique sur A0 (ADC 12 bits)
Objectives
Read an analog sensor (potentiometer or light sensor) on A0 using the ESP32-S3's 12-bit ADC (0-4095 for 0-3.3 V), convert the value to voltage, and understand the ADC2/Wi-Fi pitfall.
Hardware
- 1 × Arduino Nano ESP32
- 1 × 10 kΩ potentiometer (pin 1 → 3.3 V, pin 2 → A0, pin 3 → GND)
- Warning: the entire board operates at 3.3 V; no signal must exceed 3.3 V on any pin (and the board has no 5 V pin)
Complete code
// A0..A7 : 8 entrées analogiques, ADC 12 bits, 3,3 V max (docs-db)
void setup {
Serial.begin(115200);
}
void loop {
int raw = analogRead(A0); // 0..4095
float mv = raw * (3300.0 / 4095.0); // tension en millivolts
Serial.printf("A0 = %d (%.0f mV)\n", raw, mv);
delay(200);
}
Expected verification
When turning the potentiometer, the raw value follows 0 → 4095 and the voltage 0 → approximately 3300 mV. At rest, the values remain stable (noise of only a few units).
Common errors
- ADC2/Wi-Fi pitfall: on ESP32, ADC2 channels become unusable (reading 0 or erratic values) as soon as Wi-Fi is active. >
- Powering the sensor with 5 V: exceeds 3.3 V on the ADC; moreover, the board has no 5 V pin.
- Expecting 10 bits like on an AVR: here it is 12 bits (0-4095).
- Wires too long without a common ground with the board: measurements jump constantly.
Other tutorials for this board
Code examples for this board
connexion-wifi.inoArduinopoint-acces.inoArduinoble-capteur.inoArduinoneoled-rgb.inoArduinoadc-12bits.inoArduinopreferences.inoArduino