Digital to Analog Converters (Outputs)

A digital to analog converter (also called a DAC) is a piece of hardware that can take a numeric, or digital, value and turn it into a voltage, or analog value. This is useful for interfacing with devices that expect varying analog signals, like controlling the intensity of a LED or driving a speaker to play sounds. Not all boards and processors support a digital to analog converter so check your board’s documentation to see if it has such a feature. Luckily the Atmel SAMD21 processor used in many CircuitPython boards like the Circuit Playground Express and Metro M0 express have a digital to analog converter built-in.

To demonstrate the digital to analog converter you can control the voltage output by a pin to brighten and dim a LED. You’ll need the following components:

  • A single color LED. You want a simple single color LED and not a fancier multi-color LED or NeoPixel. Look for a LED that has two legs, a short one and long one. Check out the Adafruit LED guide for more details on LEDs.
  • A resistor in the range of 300-1,000 ohms. You must use a resistor when wiring up a LED to your board or else you might damage the digital output on the board. The resistor limits the amount of current to the LED and prevents damage to the board or LED. The exact value of the resistor isn’t super important for this demonstration–pick any resistor in the 300-1,000 ohm range.
  • A breadboard and wires to connect the components and board together.

Connect the components to your board as follows:

  • The short leg (cathode) of the LED connects to one end of the resistor.
  • The other end of the resistor connects to the ground or GND pin of the board.
  • The long leg (anode) of the LED connects to a the digital to analog converter output of your board. You might need to check your board’s documentation to find this pin. On the Metro M0 Express and Circuit Playground Express look for the A0 pin with a squiggly line next to it (the squiggle indicates this pin is a DAC output).

Now at the REPL import the analogio and board module to create an instance of the analogio.AnalogOut class:

Download: file
>>> import board
>>> import analogio
>>> led = analogio.AnalogOut(board.A0)

Just like with an analog input the analogio.AnalogOut class initializer needs to be told which pin will be used as the output. In this case the board pin A0 is being used as the DAC output.

Once the AnalogOut class is created you’re ready to control its voltage. You can change the voltage by updating the analogio.AnalogOut.value attribute. Just like with an analog input the range of possible values go from 0 to 65535, or all 16-bit unsigned integer values. For example to set the value to 0, or ground, and turn off the LED:

Download: file
>>> led.value = 0

And to turn on the LED to maximum brightness with the highest possible voltage output value use the value 65535:

Download: file
>>> led.value = 65535

Notice the LED turns on very brightly! Now try changing the value to a slightly lower value, like 50000:

Download: file
>>> led.value = 50000

You should see the LED light up less bright. Try experimenting with setting different values in the range of 0 to 65535 and notice how the LED responds.

One thing you might see is if the value is set to a low number, like 10000, the LED turns off just like if the value was set to 0. The reason for this is that the LED has a minimum voltage before it turns on and starts emitting light. For most LEDs this voltage is around 1.8 to 2 volts and with a low enough value like 10000 the voltage output by the DAC is below the LED’s ‘turn on’ (or forward) voltage.

As a side note if you have a multimeter that measures DC voltage try hooking up the probes to measure the voltage output by the A0 pin. Put the positive probe on the A0 output or LED anode and the negative probe on the board ground, then measure the DC voltage. As you set the value see how voltage read by the multimeter changes!

Just like with an analog input the digital to analog converter converts its digital value (the number like 10000) to a voltage based on an internal analog reference voltage. For the Metro M0 Express and Circuit Playground Express this reference voltage is 3.3 volts, so a value of 65535 means a full 3.3 volt output and a value of 0 means a 0 volt output. An in-between value will set a proportionally in-between voltage.

So a value of 10000 with a 3.3 volt reference voltage means you should see a DAC output voltage of (try typing this equation in the Python REPL):

Download: file
>>> 10000 / 65535 * 3.3
0.5035477225909819

A voltage of ~0.5 volts is too far below the ~2 volt threshold to turn on the LED. What if you try a higher value like 50000, what voltage should you expect? Again you can compute it with the same equation:

Download: file
>>> 50000 / 65535 * 3.3
2.517738612954909

So a value of 50000 means the output voltage is about 2.5 volts. Enough to turn on the LED but not very brightly. Try setting the DAC value to other values above 50000 to see how an increase in the voltage increases the brightness of the LED!

Remember you can create a Python function to simplify setting the output value for a desired output voltage:

Download: file
>>> def dac_value(volts):
...    return int(volts / 3.3 * 65535)
>>> led.value = dac_value(2.5)
This guide was first published on Sep 02, 2017. It was last updated on Sep 02, 2017. This page (Digital to Analog Converters (Outputs)) was last updated on Aug 19, 2019.