domingo, 17 de junio de 2018

ESP32 desde Cero - Tutorial 4: ADC Conversor Analógico a Digital


En este video-tutorial, explico cómo utilizar los ADCs (conversor analógico a digital) de 12 bits del ESP32, en cualquiera de los 18 pines disponibles, desde la IDE de Arduino.  


Código Fuente:

void setup() {
  Serial.begin(115200);

  /*
  * Get ADC value for pin
  * */
  //analogRead(36);
  
  /*
  * Set the resolution of analogRead return values. Default is 12 bits (range from 0 to 4096).
  * If between 9 and 12, it will equal the set hardware resolution, else value will be shifted.
  * Range is 1 - 16
  *
  * Note: compatibility with Arduino SAM
  */
  //analogReadResolution(12);

  /*
  * Sets the sample bits and read resolution
  * Default is 12bit (0 - 4095)
  * Range is 9 - 12
  * */
  analogSetWidth(12);

  /*
  * Set number of cycles per sample
  * Default is 8 and seems to do well
  * Range is 1 - 255
  * */
  analogSetCycles(8);

  /*
  * Set number of samples in the range.
  * Default is 1
  * Range is 1 - 255
  * This setting splits the range into
  * "samples" pieces, which could look
  * like the sensitivity has been multiplied
  * that many times
  * */
  analogSetSamples(1);

  /*
  * Set the divider for the ADC clock.
  * Default is 1
  * Range is 1 - 255
  * */
  analogSetClockDiv(1);

  /*
  * Set the attenuation for all channels
  * Default is 11db
  * */
  analogSetAttenuation(ADC_11db); //ADC_0db, ADC_2_5db, ADC_6db, ADC_11db

  /*
  * Set the attenuation for particular pin
  * Default is 11db
  * */
  analogSetPinAttenuation(36, ADC_0db); //ADC_0db, ADC_2_5db, ADC_6db, ADC_11db

  /*
  * Get value for HALL sensor (without LNA)
  * connected to pins 36(SVP) and 39(SVN)
  * */
  //hallRead();

  /*
  * Non-Blocking API (almost)
  *
  * Note: ADC conversion can run only for single pin at a time.
  *       That means that if you want to run ADC on two pins on the same bus,
  *       you need to run them one after another. Probably the best use would be
  *       to start conversion on both buses in parallel.
  * */

  /*
  * Attach pin to ADC (will also clear any other analog mode that could be on)
  * */
  adcAttachPin(36);

  /*
  * Start ADC conversion on attached pin's bus
  * */
  adcStart(36);

  /*
  * Check if conversion on the pin's ADC bus is currently running
  * */
  //adcBusy(uint8_t pin);

  /*
  * Get the result of the conversion (will wait if it have not finished)
  * */
  //adcEnd(uint8_t pin);

  /**
  * When VDD_A is 3.3V:
  *
  * - 0dB attenuaton (ADC_ATTEN_DB_0) gives full-scale voltage 1.1V
  * - 2.5dB attenuation (ADC_ATTEN_DB_2_5) gives full-scale voltage 1.5V
  * - 6dB attenuation (ADC_ATTEN_DB_6) gives full-scale voltage 2.2V
  * - 11dB attenuation (ADC_ATTEN_DB_11) gives full-scale voltage 3.9V (see note below)
  *
  * @note The full-scale voltage is the voltage corresponding to a maximum reading (depending on ADC1 configured
  * bit width, this value is: 4095 for 12-bits, 2047 for 11-bits, 1023 for 10-bits, 511 for 9 bits.)
  *
  * @note At 11dB attenuation the maximum voltage is limited by VDD_A, not the full scale voltage.
  */  
}

void loop() {
  Serial.println(analogRead(36)); //VP
  delay(1000);
}

12 comentarios:

  1. tengo un problema, estoy trabajando con un NodeMCU esp32s y necesito leer 6 entradas analógicas con un sensor flexible, pero no esta leyendo ningun de los pines, dejare el codigo como lo estoy haciendo

    void setup() {
    analogReadResolution(12);
    Serial.begin(115200);
    delay(1000);
    }

    void loop() {

    Serial.println(analogRead(0));
    Serial.println(analogRead(3));
    Serial.println(analogRead(6));
    Serial.println(analogRead(7));
    Serial.println(analogRead(4));
    Serial.println(analogRead(5));
    delay(500);

    }

    ResponderEliminar
    Respuestas
    1. mira el punto 5.11 de la hoja de datos y veras que de los pines que has puesto solo el 0 es una entrada ADC.
      https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf

      Eliminar
  2. HOLA quiero leer los datos de un sensor de la humedad en la tierra y no consigo leer nada

    ResponderEliminar
    Respuestas
    1. Estoy con el mismo problema, en el puerto serial solo muestra 4095, pero si utilizo el arduino uno si funciona.

      Ya lo solucionaste para el ESP32???

      Eliminar
  3. muy buenas noches creo que si le dan un poco de delay(5) para que el
    convertidor tenga tiempo de hacer la conversión

    ResponderEliminar
  4. void loop() {

    Serial.println(analogRead(0));
    delay(5);
    Serial.println(analogRead(3));
    delay(5);
    Serial.println(analogRead(6));
    delay(5);
    Serial.println(analogRead(7));
    delay(5);
    Serial.println(analogRead(4));
    delay(5);
    Serial.println(analogRead(5));
    delay(500);

    }

    ResponderEliminar
  5. lo unico es que ya se tardara un poco mas es decir 535 milisegundos en total

    ResponderEliminar
  6. los SAR tardan un pco , pero si quieres esperar lo justo , usa las instrucciones que usa la libreria:

    adcAttachPin(pinAD);
    adcStart(pinAD);
    while(adcBusy(pinAD));
    Serial.println(adcEnd(pinAD));

    ResponderEliminar
  7. Como le hace para controlar un led con el potenciometro la intensidad de brillo......

    ResponderEliminar
  8. Buenos días Ing. Alomar. Seria tan amable de indicarme cual es el tiempo de respuesta de las entradas analogicas ? . Entre dos lecturas consecutivas de la misma entrada que tiempo de demora hay?

    ResponderEliminar
  9. alguine sabe de donde descargar la libreria que menciona:esp32-hal-adc?

    ResponderEliminar