🛠 Console série et lecture d'un capteur analogique

ESP32-S2 · debutant · Schéma : IMG-esp32-s2-debutant-3-esp32-s2-console-serie-capteur-analogique.svg — Potentiomètre dont le curseur est relié à l'entrée ADC1 GPIO3 d'une ESP32-S2, les deux autres pattes au 3V3 et au GND, câble USB vers l'ordinateur pour la console série.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials