Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Adafruit Logo
0
Adafruit Trinket M0 CircuitPython Analog Out

Adafruit Trinket M0

CircuitPython or Arduino IDE on this tiny little microcontroller board

Image for user adafruit2
published August 23, 2017, last edited June 24, 2025
Save Link Note Download
161
Beginner
Product guide

CircuitPython Analog Out

This example shows you how you can use the DAC (Digital to Analog Converter).

Many chips do not have a DAC, including Espressif ESP32-S3, nRF52840, and various others. IfAnalogOut is not available, its use will raiseNotImplementedError. Check the board guide for your board.

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

In the example below, click theDownload Project Bundle button below to download the necessary libraries and thecode.py file in a zip file. Extract the contents of the zip file, open the directoryCircuitPython_Essentials/CircuitPython_AnalogOut/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to yourCIRCUITPY drive.

YourCIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Analog Out example"""import boardfrom analogio import AnalogOutanalog_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
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Analog Out example"""import boardfrom analogio import AnalogOutanalog_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 objectanalog_out and connects the object toA0, 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 outthe Audio Out section of this guide for examples!

circuitpython_SaleaeAnalogOut.png

Find the pin

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

circuitpython_CPXBoardAnalogOut.png

Circuit Playground Express

 

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

circuitpython_TrinketA0AnalogOut.png

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.

circuitpython_GemmaM0BoardAnalogOut.png

Gemma M0

 

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

circuitpython_QT_Py_Essentials_Analog_Out.png

QT Py M0

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

circuitpython_FeatherM0ExpressBoardAnalogOut.png

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.

circuitpython_FeatherM4A0.png

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

circuitpython_ItsyBitsyM0ExpressBoardAnalogOut.png

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.

circuitpython_ItsyBitsyM4A0.png

ItsyBitsy M4 Express

 

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

circuitpython_MetroM0ExpressBoardAnalogOut.png

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.

circuitpython_MetroM4Board.jpg

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.

Page last edited January 22, 2025

Text editor powered bytinymce.

Related Guides
Search

Search

Categories

[8]ページ先頭

©2009-2025 Movatter.jp