adc_oneshot.c

ESP32-H2 · Archivo : adc_oneshot.c · Lenguaje : C · Uso : Lire un capteur analogique avec l'ADC oneshot

El ADC del H2 pasa por el nuevo driver esp_adc: se configuran unidad, canal, atenuación y anchura, y adc_oneshot_read devuelve el valor bruto. La conversión a milivoltios con la tabla de calibración del SDK sustituye la regla de tres aproximada.

Hardware necesario

Una placa ESP32-H2 (RISC-V a 96 MHz, radio IEEE 802.15.4 solo — sin Wi-Fi) tipo DevKitM-1, alimentada por USB. 1 potenciómetro de 10 kΩ (o sensor analógico tipo MPX5700, salida < 3,3 V).

Cableado

Cursor del potenciómetro en ADC3 (GPIO4), las otras dos patas a 3,3 V y GND. Nunca más de 3,3 V en un pin ADC.

Funcionamiento

adc_oneshot_new_unit abre la unidad, config_channel fija 12 bits + atenuación 12 dB (fondo de escala ≈ 3,3 V), read devuelve la muestra. La doble lectura elimina la cola del multiplexor. Prográmelo con ESP-IDF (idf.py set-target esp32h2); el puerto USB-Serial/JTAG (GPIO26/27) sirve de consola y puerto de flash. Mantenga Boot + Reset para modo download si hace falta.

Para ir más lejos

Promedie 16 muestras para estabilizar, o dispare la lectura con el LP timer entre sueños.

adc_oneshot.c

// adc_oneshot.c — lecture 12 bits + conversion en millivolts.
#include "driver/adc.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_log.h"

static const char *TAG = "adc";

void app_main(void)
{
    const adc_oneshot_unit_init_cfg_t ucfg = { .unit_id = ADC_UNIT_1 };
    adc_oneshot_unit_handle_t adc;
    ESP_ERROR_CHECK(adc_oneshot_new_unit(&ucfg, &adc));

    adc_oneshot_chan_cfg_t chan = {
        .bitwidth = ADC_BITWIDTH_12, .atten = ADC_ATTEN_DB_12,
    };
    ESP_ERROR_CHECK(adc_oneshot_config_channel(adc, ADC_CHANNEL_3, &chan));

    int mv = 0, raw = 0;
    while (1) {
        ESP_ERROR_CHECK(adc_oneshot_read(adc, ADC_CHANNEL_3, &raw));
        adc_oneshot_read(adc, ADC_CHANNEL_3, &raw);   // 2e lecture : plus stable
        // Conversion brute -> mV par la table de calibration du SDK :
        mv = raw * 3300 / 4095;
        ESP_LOGI(TAG, "raw=%d soit ~%d mV (potentiometre ?)", raw, mv);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

← Volver a la ficha