This example shows you how you can set the DAC (true analog output) on pin A0.

A0 is the only true analog output on the M0 boards. No other pins do true analog output!

Copy and paste the code into code.py using your favorite editor, and save the file.

```# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#

"""CircuitPython Analog Out example"""
import board
from analogio import AnalogOut

analog_out = AnalogOut(board.A0)

while True:
# Count up from 0 to 65535, with 64 increment
# which ends up corresponding to the DAC's 10-bit range
for i in range(0, 65535, 64):
analog_out.value = i
```

## Creating an analog output

`analog_out = AnalogOut(A0)`

Creates an object `analog_out` and connects the object to A0, the only DAC pin available on both the M0 and the M4 boards. (The M4 has two, A0 and A1.)

## Setting the analog output

The DAC on the SAMD21 is a 10-bit output, from 0-3.3V. So in theory you will have a resolution of 0.0032 Volts per bit. To allow CircuitPython to be general-purpose enough that it can be used with chips with anything from 8 to 16-bit DACs, the DAC takes a 16-bit value and divides it down internally.

For example, writing 0 will be the same as setting it to 0 - 0 Volts out.

Writing 5000 is the same as setting it to 5000 / 64 = 78, and 78 / 1024 * 3.3V = 0.25V output.

Writing 65535 is the same as 1023 which is the top range and you'll get 3.3V output

## Main Loop

The main loop is fairly simple, it goes through the entire range of the DAC, from 0 to 65535, but increments 64 at a time so it ends up clicking up one bit for each of the 10-bits of range available.

CircuitPython is not terribly fast, so at the fastest update loop you'll get 4 Hz. The DAC isn't good for audio outputs as-is.

Express boards like the Circuit Playground Express, Metro M0 Express, ItsyBitsy M0 Express, ItsyBitsy M4 Express, Metro M4 Express, Feather M4 Express, or Feather M0 Express have more code space and can perform audio playback capabilities via the DAC. QT Py M0, Gemma M0 and Trinket M0 cannot!

Check out the Audio Out section of this guide for examples!

## Find the pin

Use the diagrams below to find the A0 pin marked with a magenta arrow!

Circuit Playground Express

A0 is located between VOUT and A1 near the battery port.

Trinket M0

A0 is labeled "1~" on Trinket! A0 is located between "0" and "2" towards the middle of the board on the same side as the red LED.

Gemma M0

A0 is located in the middle of the right side of the board next to the On/Off switch.

QT Py M0

A0 is located next to the USB port, by the "QT" label on the board silk.

Feather M0 Express

A0 is located between GND and A1 on the opposite side of the board from the battery connector, towards the end with the Reset button.

Feather M4 Express

A0 is located between GND and A1 on the opposite side of the board from the battery connector, towards the end with the Reset button, and the pin pad has left and right white parenthesis markings around it

ItsyBitsy M0 Express

A0 is located between VHI and A1, near the "A" in "Adafruit", and the pin pad has left and right white parenthesis markings around it.

ItsyBitsy M4 Express

A0 is located between VHI and A1, and the pin pad has left and right white parenthesis markings around it.

Metro M0 Express

A0 is between VIN and A1, and is located along the same side of the board as the barrel jack adapter towards the middle of the headers found on that side of the board.

Metro M4 Express

A0 is between VIN and A1, and is located along the same side of the board as the barrel jack adapter towards the middle of the headers found on that side of the board.

On the Metro M4 Express, there are TWO true analog outputs: A0 and A1.

This guide was first published on Sep 30, 2020. It was last updated on 2021-02-15 10:10:46 -0500.

This page (CircuitPython Analog Out) was last updated on Jun 29, 2022.