EasyManuals Logo

Adafruit Feather M4 Express User Manual

Adafruit Feather M4 Express
178 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #109 background imageLoading...
Page #109 background image
CircuitPython Analog Out
This example shows you how you can set the DAC (true analog output) on pin A0.
Copy and paste the code into code.py using your favorite editor, and save the file.
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. Gemma M0 and Trinket M0 cannot!
Check out the Audio Out section of this guide (https://adafru.it/BRj) for examples!
A0 is the only true analog output on the M0 boards. No other pins do true analog output!
# CircuitPython IO demo - analog output
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
© Adafruit Industries https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51 Page 114 of 183

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Adafruit Feather M4 Express and is the answer not in the manual?

Adafruit Feather M4 Express Specifications

General IconGeneral
BrandAdafruit
ModelFeather M4 Express
CategoryMicrocontrollers
LanguageEnglish

Related product manuals