|
META TOPICPARENT |
name="Exercises" |
Exercise 9: Analogue to Digital (ADC) and Digital to Analogue (DAC) conversion
Introduction |
| 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) |
> > | 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 |
|
|
|
< < | The fit gives up the coefficients (a..e) of the polynomial and we can use this to correct for linearity. Instead of returning the raw ADC value we enter it into the polynomial and return the calculated value multiplied by 16 to get 0..4096 instead of 0..256. |
> > | The fit returns the coefficients (a..e) of the polynomial and we can use this to correct for linearity. Instead of returning the raw ADC value we enter it into the polynomial and return the calculated value multiplied by 16 to get 0..4096 instead of 0..256. |
|
Here is the DAC versus ADC measurement on the ESP32 ADC after correction: |