🛠 Console série et lecture d'un capteur analogique
Objectives
Master serial communication on the ESP32-S2 and analog-to-digital conversion: read a potentiometer on an ADC1 channel and send the values to the serial monitor.
Hardware
- ESP32-S2 board and USB cable
- 1 potentiometer (e.g., 10 kΩ)
- 3 Dupont cables
Complete code
// Lecture ADC1 + console série sur ESP32-S2
const uint8_t CAPTEUR_PIN = 3; // ADC1_CH0 sur la plupart des cartes S2
void setup {
Serial.begin(115200);
analogSetPinAttenuation(CAPTEUR_PIN, ADC_11db); // plage ~0-3,1 V
delay(1000);
Serial.println("Console serie ESP32-S2 prete");
}
void loop {
uint16_t brut = analogRead(CAPTEUR_PIN);
float tension = brut * 3.3f / 4095.0f; // ADC 12 bits
Serial.printf("brut=%u tension=%.2f V\n", brut, tension);
delay(250);
}
Expected verification
The serial monitor displays four times per second a raw value between 0 and 4095 and the corresponding voltage. Turning the potentiometer makes both values vary continuously from 0 to approximately 3.3 V.
Common errors
- Using an ADC2 pin: on ESP32 boards, ADC2 is unavailable when WiFi is active; prefer ADC1.
- Forgetting
analogSetPinAttenuation: without attenuation, voltages close to 3.3 V are clipped. - Inconsistent serial baud rate: the monitor must be set to 115200 baud.
- Confusing the two USB ports on the board (UART and native USB): if nothing is displayed, try the other port.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino