Tags:
create new tag
view all tags

Exercise 9: ADC and DAC

Introduction

The ESP32 comes with two 12 bit ADCs and two 8 bit DACs. In this exercise we will see how to use them and what their limitations are.

Exercise 1: Basic ADC use

Read the ADC and DAC descriptions on this TWiki: ADC and DAC. Connect the analogue joystick to the ADC channels on pin 32 and 34 on the CPU card (not on the WeMos D1 mini bus!). Set the ADC attenuation to 11 dB. Read both joystick channels (hor and ver) once per second and print the results.

Exercise 2: Linearity check

Connect the DAC (pin D0 or GPIO 26 ) to the ADC (pin A0 or GPIO 36). Set the ADC attenuation to 11 dB. Write a program which ramps up the DAC from 0 to its max value 255 and which reads the ADC at each step. Write the ADC values to a file, transfer it to your laptop and plot the result.

This is what you would expect to get:

expected.png

Exercise 3: Check with external ADC

You will see in exercise 2 that the DAC/ADC system is not linear. Try to find out if it is the DAC or the ADC which introduces the non-linear behavior replacing the ESP32 ADC by an external ADS1115 16 bit ADC. You can find the driver software for the ADS1115 at https://github.com/robert-hh/ads1x15.

Exercise 4: Compensating the non-linearity by software

As you will see in exercise 3 that the non-linearity comes from the ADC and that the DAC is perfectly linear, you can compensate the non-linearity by software. On the TWiki page ADC and DAC it is explained how this is done. Follow the explication and create a polynomial fit to the inverse of the ADC vs DAC curve. Use this polynomial to do the correction. Try another ADC vs DAC measurement applying the correction and make sure that you correction works.

Exercise 5: A simple pulse generator

The DAC can be used to create a simple pulse generator for different wave forms. Create a Python program that pre-calculates a

  • sine
  • rectangular
  • sawtooth
  • triangual
wave form and sends these data to the DAC. The wave forms are pre-calculated such that the values stored in a list can be sent to the DAC quickly and at regular intervals (different time intervals due to different calculation times are eliminated). Observe the wave forms on an oscilloscope. This is what you would get:

rectWaveform.png

sawtooth.png

triangular.png

sinewave.png

The exercise sheet in odt format:

https://afnog.iotworkshop.africa/pub/UCC_Course_2020/ADCAndDACExercises/exercise_9.odt

-- Uli Raich - 2020-07-31

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatodt exercise_9.odt r1 manage 206.2 K 2020-08-09 - 09:03 UliRaich  
PNGpng expected.png r1 manage 33.7 K 2020-07-31 - 15:20 UliRaich  
PNGpng rectWaveform.png r1 manage 36.9 K 2020-07-31 - 15:33 UliRaich  
PNGpng sawtooth.png r1 manage 38.5 K 2020-07-31 - 15:33 UliRaich  
PNGpng sinewave.png r1 manage 39.4 K 2020-07-31 - 15:33 UliRaich  
PNGpng triangular.png r1 manage 40.4 K 2020-07-31 - 15:33 UliRaich  
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2020-08-09 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback