EE lab track, module 10

ADC and DAC

Graded ADC and DAC labs that run your sketch: print millivolts from analogRead, set a divider for a code, make a PWM DAC with an RC filter and measure ripple.

On the syllabus

  • ADC resolution, reference and the code-to-volts conversion
  • Integer arithmetic for scaling without overflow
  • Choosing a divider for an ADC input
  • PWM plus an RC low-pass filter as a DAC
  • DAC ripple and the filter’s time constant

You will be able to

  • Convert an ADC code to millivolts without overflow or floating point.
  • Choose a divider that puts a signal where the ADC wants it.
  • Make a steady voltage from PWM with an RC filter.
  • Predict and measure the ripple a PWM DAC leaves.

The first challenge is free. The rest, the viva and the certificate are part of Pro, and everything is here to read.

Graded challenges

Values like 4.7k, 100n or 2.2u are fine. Readings are the number only, in the unit shown. Every check measures the simulation, not a bench: ideal parts within their models, no noise.

1. Print millivolts from analogRead

An ADC turns a voltage into a whole number. The Uno’s is 10 bits against its 5 V supply, so 0 V reads 0 and 5 V reads 1023. To get volts back you scale the code by the reference.

FormulamV = code × 5000 / 1023

Worked exampleA code of 300 is 300 × 5000 / 1023 = 1466 mV. Done in a 16-bit int, 300 × 5000 = 1 500 000 overflows; done in a long it fits.

On the scope or meterThe serial monitor prints a new line every 100 ms. The voltmeter on A0 and your printed millivolts should agree to within a few mV.

Your task

A divider, 10 kΩ over 4.7 kΩ, puts about 1.6 V on A0. The starter prints the raw ADC code.

Change the sketch to print the voltage in millivolts, as a line like mv=1598, at least every 200 ms. Watch the arithmetic: 1023 × 5000 does not fit in an int.

    Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

    It is checked for

    • It prints the voltage on A0 in millivolts

    2. Choose a divider for an ADC code of 512

    A divider sets where a voltage sits in the ADC’s range. The code is the tap voltage as a fraction of 5 V, times 1023.

    Formulacode = 1023 × R2 / (R1 + R2)

    Worked exampleWith R1 = 3.3 kΩ and R2 = 1 kΩ: 1023 × 1 / 4.3 = 238.

    On the scope or meterThe printed raw code changes as you change R2. A voltmeter on A0 reads the code times about 4.9 mV.

    Your task

    The sketch prints the raw code on A0. The divider’s top resistor R1 is 10 kΩ.

    Choose R2, the bottom resistor, so the code is 512, within 5 counts: the middle of the ADC’s range. Build the sketch as it is and check.

      Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

      Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

      Your design

      Ω

      It is checked for

      • A0 reads 512

      3. Make 2.0 V with a PWM DAC

      PWM switches a pin between 0 V and 5 V. A low-pass filter well below the PWM frequency keeps only the average, which is the duty cycle times 5 V. analogWrite takes 0 to 255 for 0% to 100%.

      FormulaVout = 5 V × value / 255

      Worked exampleFor 3.0 V you need 60% duty: 0.6 × 255 = 153.

      On the scope or meterThe scope on C1 shows a nearly flat line with a small ripple on top. It takes a few time constants (RC = 100 ms here) to settle.

      Your task

      Pin 9 drives an RC low-pass filter, 10 kΩ into 10 µF, which smooths the PWM into a steady voltage.

      Change the analogWrite value so the filtered voltage averages 2.0 V, within 3%.

        Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

        Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

        It is checked for

        • The filtered output averages 2.0 V

        4. Measure the ripple a PWM DAC leaves

        A filter cannot remove all of the switching. The capacitor charges a little in the high time and discharges in the low time, leaving a ripple. A bigger RC smooths more but answers more slowly.

        FormulaΔV ≈ 5 V × D (1 − D) / (f R C)

        Worked exampleAt 25% duty, 490 Hz, 10 kΩ and 10 µF: 5 × 0.25 × 0.75 / (490 × 0.1) = 19 mV.

        On the scope or meterZoom the scope in on C1: a small triangle wave at the PWM frequency, riding on about 2.5 V.

        Your task

        The same idea with a faster filter: 10 kΩ into 1 µF, a 10 ms time constant, and analogWrite(9, 128), about 50% duty. Build the sketch as it is.

        Put the scope on the capacitor. Measure the ripple, peak to peak, in millivolts. Then estimate it: ΔV ≈ 5 V × D (1 − D) / (f R C).

          Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

          Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

          Your readings

          mV

          Viva questions

          The questions an examiner asks at the bench. 75% passes; every answer comes with why.

          Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

          1. What is the resolution of the Uno’s ADC with its 5 V reference?

          2. Why does int code * 5000 / 1023 go wrong on an Uno?

          3. What limits the largest voltage an Uno’s ADC can measure, and how do you measure more?

          4. Why should the source driving an ADC input have a low impedance (about 10 kΩ or less)?

          5. What does the RC filter after a PWM pin do?

          6. Doubling the RC time constant of a PWM DAC filter does what?

          7. What is quantization error?

          8. Name two kinds of DAC besides a filtered PWM.