CircuitPython comes with audiopwmio
, which provides built-in audio output support using pulse width modulation (PWM).
PWM converts the audio signal to a series of rectangular waveforms, or frequencies. By varying the PWM frequency, you can generate tones and play, pause and resume audio files. The faster your microcontroller is, the better sound quality you'll be able to achieve with PWM audio.
Necessary Hardware
You'll need the following additional hardware to complete the examples on this page.
- STEMMA Speaker SIG to Metro pin A1 (white wire)
- STEMMA Speaker GND to Metro GND (black wire)
- Speaker Speaker VIN to Metro 3.3V (red wire)
PWM Audio Pin Limitations
The Metro M7 can only do PWM audio on pins A0 and A1. No other pins on the board support PWM audio.
PWM Tone Playback
The first example generates one period of a sine wave and then loops it to generate a tone. You can change the volume and the frequency (in Hz) of the tone by changing the associated variables. Inside the loop, you play the tone for one second and stop it for one second.
Update your code.py to the following.
Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the folder that matches your CircuitPython version, and copy the code.py file to your CIRCUITPY drive.
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """ CircuitPython PWM Audio Out tone example Plays a tone for one second on, one second off, in a loop. """ import time import array import math import board from audiocore import RawSample from audiopwmio import PWMAudioOut as AudioOut audio = AudioOut(board.A1) 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((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1)) sine_wave_sample = RawSample(sine_wave) while True: audio.play(sine_wave_sample, loop=True) time.sleep(1) audio.stop() time.sleep(1)
Now you'll hear one second of a 440Hz tone, and one second of silence.
You can try changing the 440
Hz of the tone to produce a tone of a different pitch. Try changing the number of seconds in time.sleep()
to produce longer or shorter tones.
PWM WAV File Playback
The second example plays a WAV file. You open the file in a readable format. Then, you play the file and, once finished, print Done playing!
to the serial console. You can use any supported wave file.
Update your code.py to the following.
Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the folder that matches your CircuitPython version, and copy the StreetChicken.wav file and the code.py file to your CIRCUITPY drive.
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """ CircuitPython PWM Audio Out WAV example Play a WAV file once. """ import board from audiocore import WaveFile from audiopwmio import PWMAudioOut as AudioOut audio = AudioOut(board.A1) with open("StreetChicken.wav", "rb") as wave_file: wave = WaveFile(wave_file) print("Playing wav file!") audio.play(wave) while audio.playing: pass print("Done!")
Now you'll hear the wave file play, and on completion, print Done Playing!
to the serial console.
You can play a different WAV file by updating "StreetChicken.wav"
to be the name of your CircuitPython-compatible WAV file.
You can do other things while the WAV file plays! There is a pass
in this example where you can include other code, such as code to blink an LED.
Text editor powered by tinymce.