Using the Pyboard's Analog Pins for DAC

Contents

Pyboard's DAC Pins

There are two different types of analog pins on the Pyboard.

  • Analog-to-Digital Converter (ADC): Converts a continuous physical voltage read at the Pyboard's analog pin into a discrete digital number.
  • Digital-to-Analog Converter (DAC): Performs the opposite function to the ADC. It converts a discrete digital signal (a series of 0's and 1's) into a continuous analog voltage signal as an output on the Pyboard's analog pin.

This article deals exclusively with the Pyboard's DAC pins. If interested the article Using the Pyboard's Analog Pins for ADC discusses the Pyboard's ADC pins.

The Pyboard has two × 12-bit DAC channels. They are called DAC1 and DAC2. Each channel is connected to one GPIO pin on the Pyboard:

  • DAC1 : connected to pin X5
  • DAC2 : connected to pin X6

The next section explains how to control a DAC with MicroPython.

MicroPython pyb.DAC Class

Control of the DAC is through the pyb.DAC class. This section describes how to create a DAC object assigned to one of the two DAC channels. Several of the DAC methods are described with examples. Any analog output will be examined with an oscilloscope.

The official MicroPython documentation mentions that this class will be subject to change in later versions of MicroPython. As always, the official documentation[1] should be consulted.

Since the Pyboard is a 3.3V device, the DAC output voltage on the pin will between 0V and 3.3V inclusive.

Assigning a Pin for DAC

Assigning a pin for DAC is very simple. Unlike the case of ADC, there is no need to firstly define a Pin object.


Syntax:
<dac_variable> = pyb.DAC(port, bits=8)

Creates a DAC object.

Where:
  port : One of 1 (Pin X5) or 2 (Pin X6)
  bits : One of 8 (bits) or 12 (bits)

Syntax:
pyb.DAC.deinit()

De-initialise the DAC making its pin available
for other uses.

Examples:
from pyb import DAC

# Define Pin X5 for DAC (8-bits)
dac1 = DAC(1, bits=8)

# Define Pin X6 for DAC (12-bits)
dac2 = DAC(2, 12)
# De-initialise DAC on Pin X6
dac2.deinit()
            

Assigning DAC output to the Pin

The pyb.DAC class has several methods that output a continuous voltage on a DAC configured pin. For sake of brevity only three of these methods will be discussed:

  • DAC.write() : A discrete digital value is passed to the method which converts it to an analog voltage on the pin.
  • DAC.noise() : Generates a pseudo-random noise signal.
  • DAC.triangle() : Generates a triangle wave.

Syntax:
DAC.write(value)

The parameter value is used to calculate a voltage
between 0V and 3.3V. The voltage calculated depends
upon the resolution i.e. 8-bit or 12-bit.

8-bit :  Volts = value * 3.3 / 255
         Value = Volts * 255 /3.3

12-bit : Volts = value * 3.3 /4095
         Value = Volts * 4095 / 3.3

Example:
from pyb import DAC

# Define Pin X5 for DAC (12-bits)
dac = DAC(1, bits=12)
# Set Pin X5 to 2.5V
dig_value = int(2.5 * 4095 / 3.3)
dac.write(dig_value)
            
Output at Pin X5 is 2.54V.
Fig 1 - Average output at Pin X5 is 2.54 Volts
Syntax:
DAC.noise(freq)

Generate a pseudo-random noise signal. A new random sample
is written to the DAC output at the given frequency.
The frequency is in units of Hz.

Example:
# Output a random noise signal on Pin X5
# at a frequency of 1000 Hz, i.e. the pin
# voltage changes every 1ms.

from pyb import DAC

# Define Pin X5 for DAC (12-bits)
dac = DAC(1, bits=12)
# Write a random voltage signal
dac.noise(1000)
            
A random noise signal with a new sample added every 1ms.
Fig 2 - A random noise signal with a new sample added every 1ms (1000 Hz)
Syntax:
DAC.triangle(freq)

Generate a triangle wave. The value on the DAC output
changes at the given frequency and ramps through the full
12-bit range (up and down). Therefore the frequency of the
repeating triangle wave itself is 8192 times smaller.

Example:
# Output a triangle wave on Pin X5
# with the voltage changing at 81.920 KHz.
# This gives the triangle wave a frequency
# of 10 Hz i.e. wave period = 100ms.

from pyb import DAC

# Define Pin X5 for DAC (12-bits)
dac = DAC(1, bits=12)
# Write a triangle wave.
dac.triangle(81920)
            
A triangle wave form with a period of 100ms.
Fig 3 - A triangle wave form with a period of 100ms