Tags:
view all tags
---+ Exercise 9: Analogue to Digital (ADC) and Digital to Analogue (DAC) conversion ---++ Introduction The ESP32 has two 12 bit SAR (<b>S</b>uccessive <b>A</b>pproximation <b>R</b>egister) <b>A</b>nalogue to <b>D</b>igital <b>C</b>onverters (ADCs) and two 8 bit <b>D</b>igital to <b>A</b>nalogue <b>C</b>onverters on chip. Checking for drivers in the !MicroPython manual I only find a [[https://docs.micropython.org/en/latest/esp32/quickref.html#adc-analog-to-digital-conversion][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: <literal> from machine import Pin,DAC<br />from time import sleep_ms<br /><br />dac = DAC(Pin(26))<br />print("Running a triangular wave form with a frequency of ~ 1 Hz on pin 26")<br />while True:<br /> for i in range(256):<br /> dac.write(i)<br /> sleep_ms(2)<br /> for i in range(256):<br /> dac.write(256-i-1)<br /> sleep_ms(2)<br /> </literal> 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 an 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 | | <img alt="linearity.png" height="598" src="%ATTACHURL%/linearity.png" title="linearity.png" width="662" /> | <img alt="restrictedLinearity.png" height="598" src="%ATTACHURL%/restrictedLinearity.png" title="restrictedLinearity.png" width="662" /> | 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. -- %USERSIG{UliRaich - 2020-07-26}% ---++ Comments %COMMENT%
Attachments
Attachments
Topic attachments
I
Attachment
History
Action
Size
Date
Who
Comment
png
linearity.png
r1
manage
31.6 K
2020-07-30 - 12:14
UliRaich
png
restrictedLinearity.png
r1
manage
29.2 K
2020-07-30 - 12:14
UliRaich
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r10
|
r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r2 - 2020-07-30
-
UliRaich
Home
Site map
AFNOG web
Embedded_Systems web
IoT_Course_English web
IoT_Course_French web
Main web
Sandbox web
TWiki web
IoT_Course_English Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Register User
Edit
Attach
Copyright © 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