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
Содержание Feather M4 Express
Страница 1: ...Adafruit Feather M4 Express Created by lady ada Last updated on 2019 03 04 10 41 14 PM UTC...
Страница 5: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 10 of 183...
Страница 58: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 63 of 183...
Страница 164: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 169 of 183...
Страница 168: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 173 of 183...