Exercise 9: Analogue to Digital (ADC) and Digital to Analogue (DAC) conversion

Introduction

The ESP32 has two 12 bit SAR (Successive Approximation Register) Analogue to Digital Converters (ADCs) and two 8 bit Digital to Analogue Converters on chip. Checking for drivers in the MicroPython manual I only find a description of the ADC. Checking the MicroPython source code however, I see that also the driver for the DACs is available.

The ADC

ADC 2 is used for WiFi and therefore not accessible to us. ADC 1 however has multiplexed input and 8 ADC channels are available for use (on Pins 32-39). The ADC has a range 0..1V but attenuators are available. If we set the attenuation to 11 DB we get a voltage range of approximately 0 .. 3.6 V. Pin 36 and pin 26 are available on the WeMos D1 bus but pins on the ESP32 CPU card can also be used (e.g. pin 33, 34, 35).

The DAC

The DACs are accessible on pins 25 and 26. Since there is no description in the manual, here is the way how to access the DAC:   from machine import Pin,DAC
from time import sleep_ms

dac = DAC(Pin(26))
print("Running a triangular wave form with a frequency of ~ 1 Hz on pin 26")
while True:
    for i in range(256):
        dac.write(i)
        sleep_ms(2)
    for i in range(256):
        dac.write(256-i-1)
        sleep_ms(2)
   

This will generate a slow triangular wave form that can be observed on a multi-meter.

Checking linearity

If we connect the DAC output to the input of an ADC channel and we slowly ramp up the DAC value from 0 to its maximum and we read back the signal level with the ADC, then we expect a perfectly linear curve. This is true under the condition that both, the DAC and the ADC are perfectly linear.

linearity full range linearity restricted range
linearity.png restrictedLinearity.png
As we can see, the curve becomes very non-linear for values above 200 (3.3V * 200 / 256 ~ 2.6 V). Unfortunately we do not know if the ADC or the DAC is responsible for this non-linearity. We need an external ADC to check. We also see that the line does not pass though 0,0 as it should.

Verifying linearity with an external ADS1115

The ADS1115 is a high precision 16 bit Sigma/Delta ADC with an I2C interface. It can easily be connected to the WeMos D1 bus as follows:

ADS 1115 WeMos D1 bus and ESP32 WeMos D1 bus and ESP8266
Vdd 3.3V 3.3V
Gnd Gnd Gnd
SCL D1: GPIO 22 D1: GPIO 5
SDA D2: GPIO 21 D2: GPIO 4
A0 DAC 2: GPIO 26  
A driver MicroPython driver for the ADS1115 is available making it very easy to use: https://github.com/robert-hh/ads1x15

I modified the above program checking linearity, replacing ADC readout by access to the ADS1115 instead of the ESP32 internal ADC. This is the result:

linADS1115.png

This clearly shows that the ESP32 ADC is the culprit!

-- Uli Raich - 2020-07-26

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng linADS1115.png r1 manage 30.2 K 2020-07-30 - 13:30 UliRaich  
PNGpng linearity.png r1 manage 31.6 K 2020-07-30 - 12:14 UliRaich  
PNGpng restrictedLinearity.png r1 manage 29.2 K 2020-07-30 - 12:14 UliRaich  
Edit | Attach | Watch | Print version | History: r10 | r5 < r4 < r3 < r2 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r3 - 2020-07-30 - UliRaich
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback