ESP32 ADC

What is an ADC?

An Analog-to-Digital Converter (ADC) is a crucial component in electronics and embedded systems, serving the essential function of converting continuous analog signals into discrete digital values. This conversion is necessary because most microcontrollers and digital devices operate in the digital domain, processing binary data. Analog signals, which can vary continuously, must be transformed into a format that digital systems can understand and manipulate.

For example, sensors like temperature sensors, light sensors, and potentiometers typically produce analog outputs. An ADC allows these analog outputs to be interpreted by digital systems, such as microcontrollers, enabling various applications ranging from simple data logging to complex control systems.

Key Parameters of ADCs

When discussing ADCs, several key parameters determine their performance and suitability for specific applications:

  1. Resolution: This is the number of bits that the ADC uses to represent the analog input signal. Higher resolution means more precise representation. For example, a 10-bit ADC can represent the input signal with 1024 discrete levels, while a 12-bit ADC can represent it with 4096 levels.
  2. Sampling Rate: This is the speed at which the ADC samples the analog input signal, usually measured in samples per second (SPS). Higher sampling rates are needed for accurately digitizing fast-changing signals.
  3. Input Range: This is the range of voltage levels that the ADC can convert. It typically depends on the reference voltage used by the ADC.
  4. Accuracy: This includes factors like linearity, offset error, and gain error, which affect how accurately the digital output represents the analog input.
  5. Input Impedance: The impedance presented by the ADC’s input circuit, which can affect the accuracy of the conversion if not properly matched with the source impedance.

ESP32 ADC Overview

The ESP32 microcontroller is equipped with two Analog-to-Digital Converters (ADCs), ADC1 and ADC2, which allow it to read analog signals from multiple input channels. Understanding the capabilities and configurations of these ADCs is essential for leveraging the ESP32 in applications that require analog input.

Number of ADCs

  • ADC1: This ADC has 8 input channels, allowing it to read analog signals from up to 8 different GPIO pins.
  • ADC2: This ADC has 10 input channels, providing additional options for reading analog signals.

Resolution

  • Resolution: Both ADC1 and ADC2 on the ESP32 can operate with a resolution of up to 12 bits. This means that the ADC can convert an analog signal into a digital value ranging from 0 to 4095. The higher the resolution, the more precise the digital representation of the analog signal.

NOTE:
When reading an analog value with the ESP32, it involves measuring voltage levels that can vary between 0 V and 3.3 V. The measured voltage is then translated into a numerical value ranging from 0 to 4095. In this scale, 0 V corresponds to 0, and 3.3 V corresponds to 4095. Any voltage level between 0 V and 3.3 V is represented by a proportional value within this range.

Additional functions are available for more advanced usage of ADC pins in different projects:

  • analogReadResolution(resolution):
    • Set the sample bits and resolution between 9 to 12 bits.
    • Default is 12-bit resolution.
  • analogSetWidth(width):
    • Similar to analogReadResolution(), set sample bits and resolution.
    • Default is 12-bit resolution.
  • analogSetCycles(cycles):
    • Set the number of cycles per sample.
    • Default is 8, range: 1 to 255.
  • analogSetSamples(samples):
    • Set the number of samples in the range, affecting sensitivity.
    • Default is 1 sample.
  • analogSetClockDiv(attenuation):
    • Set the divider for the ADC clock.
    • Default is 1, range: 1 to 255.
  • analogSetAttenuation(attenuation):
    • Set input attenuation for all ADC pins.
    • Default is ADC_11db.
    • Accepted values:
      • ADC_0db: No attenuation (measures up to approximately 800 mV).
      • ADC_2_5db: Attenuated input, measuring up to approx. 1100 mV.
      • ADC_6db: Attenuated input, measuring up to approx. 1350 mV.
      • ADC_11db: Attenuated input, measuring up to approx. 2600 mV.
  • analogSetPinAttenuation(pin, attenuation):
    • Set the input attenuation for a specified pin.
  • adcAttachPin(pin):
    • Attach a pin to ADC, clearing any other analog mode.
    • Returns TRUE or FALSE result.
  • adcStart(pin), adcBusy(pin), and adcEnd(pin):
    • Start an ADC conversion on the attached pin’s bus.
    • Check if conversion on the pin’s ADC bus is running (returns TRUE or FALSE).
    • Get the result of the conversion (returns a 16-bit integer).

    Leave a Reply

    Your email address will not be published.

    Need Help?