hack4electronics.com

Unlock the Power of ESP32 ADC: A Comprehensive Guide

Analog-to-Digital Converters (ADCs) are essential components in microcontrollers, enabling the conversion of analog signals, such as voltage, into digital data that the microcontroller can process. The ESP32 microcontroller offers robust ADC functionality, making it a popular choice for various applications, including sensor data acquisition and control systems, In this artcile helps to use how to program ESP32 ADC for your projects

Understanding ESP32 ADC

ADCs are used to convert analog signals into digital data. This is crucial for microcontrollers that process digital information but often interface with the analog world through sensors.

ESP32 ADC Overview

The ESP32 features several ADC channels capable of reading analog signals. Each system on a chip (SoC) or module has a different number of ADC channels and pins, which are documented in their respective datasheets.

ADC OneShot Mode

The ADC OneShot mode in ESP32 is straightforward, compatible with Arduino’s analogRead function. Here are the primary functions used:

  • analogRead
uint16_t analogRead(uint8_t pin);

Reads the raw ADC value from the specified pin.

  • analogReadMilliVolts
uint32_t analogReadMilliVolts(uint8_t pin);

Reads the ADC value and converts it to millivolts.

  • analogReadResolution
void analogReadResolution(uint8_t bits);

Sets the resolution of the ADC.

  • analogSetAttenuation
void analogSetAttenuation(adc_attenuation_t attenuation);

Sets the attenuation for the ADC, affecting the input voltage range.

ADC Continuous Mode

The ADC Continuous mode allows for background analog conversions on multiple pins, with the results accessible through a callback function. Key functions include:

  • analogContinuous
bool analogContinuous(uint8_t pins[], size_t pins_count, uint32_t conversions_per_pin, uint32_t sampling_freq_hz, void (*userFunc)(void));

Configures the ADC for continuous conversion

  • analogContinuousRead
bool analogContinuousRead(adc_continuous_data_t **buffer, uint32_t timeout_ms);

Reads the ADC data into a buffer.

  • analogContinuousStart
bool analogContinuousStart();

Starts the continuous ADC conversions.

  • analogContinuousStop
bool analogContinuousStop();

Stops the continuous ADC conversions.

Example 1: Using ESP32 ADC in OneShot Mode

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

void loop() {
  int analogValue = analogRead(2);
  int analogVolts = analogReadMilliVolts(2);
  Serial.printf("ADC analog value = %d\n", analogValue);
  Serial.printf("ADC millivolts value = %d\n", analogVolts);
  delay(100);
}

Example 2: Using ESP32 ADC in Continuous Mode

#define CONVERSIONS_PER_PIN 5
uint8_t adc_pins[] = {36, 39, 34, 35};
uint8_t adc_pins_count = sizeof(adc_pins) / sizeof(uint8_t);
volatile bool adc_coversion_done = false;
adc_continuous_data_t *result = NULL;

void ARDUINO_ISR_ATTR adcComplete() {
  adc_coversion_done = true;
}

void setup() {
  Serial.begin(115200);
  analogContinuousSetWidth(12);
  analogContinuousSetAtten(ADC_11db);
  analogContinuous(adc_pins, adc_pins_count, CONVERSIONS_PER_PIN, 20000, &adcComplete);
  analogContinuousStart();
}

void loop() {
  if (adc_coversion_done) {
    adc_coversion_done = false;
    if (analogContinuousRead(&result, 0)) {
      analogContinuousStop();
      for (int i = 0; i < adc_pins_count; i++) {
        Serial.printf("\n ADC PIN %d data:", result[i].pin);
        Serial.printf("\n   Avg raw value = %d", result[i].avg_read_raw);
        Serial.printf("\n   Avg millivolts value = %d", result[i].avg_read_mvolts);
      }
      delay(1000);
      analogContinuousStart();
    } else {
      Serial.println("Error reading data.");
    }
  }
}

The ESP32 ADC capabilities, both in OneShot and Continuous modes, provide powerful tools for reading and processing analog signals. Whether you’re developing a simple sensor application or a complex data acquisition system, understanding and utilizing these ADC functions will enhance your project’s performance and accuracy.

For more detailed information, refer to the ESP32 ADC documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *