🛠 Première entrée analogique sur ADC1

ESP32 (original, ESP32-WROOM-32 class) · debutant · Schéma : IMG-esp32-debutant-3-entree-analogique-adc1.svg — Potentiomètre relié à GPIO34 (ADC1) sur ESP32, tension convertie en valeur 0-4095 affichée en console

Objectives

Read a voltage using the ESP32's analog-to-digital converter (ADC). We will deliberately use a channel from ADC1 (GPIO32 to GPIO39): ADC2 is unusable as soon as Wi-Fi is active — a classic pitfall that we will detail in the corresponding advanced tutorial. You will also discover the GPIO34-39 constraint: these pins are pure inputs, never outputs.

Hardware

Complete code

const uint8_t POT_PIN = 34;   // ADC1_CH6, entree seule

void setup {
  Serial.begin(115200);
  analogReadResolution(12);            // 0..4095
  analogSetPinAttenuation(POT_PIN, ADC_11db); // plage ~0 a 3,3 V
  analogRead(POT_PIN);                 // 1er echantillon ignore
}

void loop {
  uint32_t brut = analogRead(POT_PIN);
  float mv = brut * 3300.0f / 4095.0f;
  Serial.printf("ADC1 GPIO34 : %u -> %.0f mV\n", brut, mv);
  delay(100);
}

Expected verification

When turning the potentiometer, the console displays a continuous value from 0 to 4095 and the corresponding voltage from 0 to approximately 3300 mV. At rest (finger near the wiper), the value may fluctuate by a few units: this is the normal noise of a 12-bit ADC without filtering.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials