For Feather M0 Express, ItsyBitsy M0 Express and Metro M0 Express, no changes are needed for the code to work.
For Feather M4 Express, comment out
audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
and uncomment
# audio =
audiobusio.I2SOut(board.D1, board.D10, board.D11)
.
For Metro M4 Express, comment out
audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
and uncomment
# audio =
audiobusio.I2SOut(board.D3, board.D3, board.D8)
.
Now you'll hear one second of a 440Hz tone, and one second of silence. Remember, listen for it without headphones
on your ears first as the volume is quite high.
You can try changing the Hz of the tone to produce different tones. Try changing the number of seconds in
time.sleep()
to produce longer or shorter tones.
Wave File
The second example plays a wave file. We open the file in a readable format. Then inside the loop, we play the file
and tell the code to continue playing the file until it's completed. You can use any
supported wave
file
(https://adafru.it/BRj)
. We've included the wave file used in the code.
https://adafru.it/BTM
https://adafru.it/BTM
import time
import array
import math
import audioio
import board
import audiobusio
tone_volume = 0.1 # Increase this to increase the volume of the tone.
frequency = 440 # Set this to the Hz of the tone you want to generate.
length = 8000 // frequency
sine_wave = array.array("h", [0] * length)
for i in range(length):
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 -1))
# For Feather M0 Express, ItsyBitsy M0 Express, Metro M0 Express
audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
# For Feather M4 Express
# audio = audiobusio.I2SOut(board.D1, board.D10, board.D11)
# For Metro M4 Express
# audio = audiobusio.I2SOut(board.D3, board.D9, board.D8)
sine_wave_sample = audioio.RawSample(sine_wave)
while True:
audio.play(sine_wave_sample, loop=True)
time.sleep(1)
audio.stop()
time.sleep(1)
© Adafruit Industries
https://learn.adafruit.com/adafruit-i2s-stereo-decoder-uda1334a
Page 42 of 45