It's easy to use the STEMMA Audio Amp with CircuitPython and the built-in audiopwmio
module. This module allows you to play a simple tune through the speaker connected to the amp.
This page uses the Feather RP2040 to demonstrate.
Wiring
Connect up the amp exactly as follows.
This diagram shows wiring it up using the STEMMA connector and the speaker terminal block.
Amp to Feather:
- Feather A0 to STEMMA Signal (white wire)
- Feather 3.3V to STEMMA VIN (red wire)
- Feather GND to STEMMA GND (black wire)
Speaker to amp:
- - side of terminal to speaker - (black wire)
- + side of terminal to speaker + (red wire)
This diagram shows wiring it up using a breadboard.
Feather to amp:
- Feather GND to breakout GND (black wire)
- Feather 3.3V to breakout VIN (red wire)
- Feather A0 to breakout Signal (white wire)
Speaker to amp:
- Speaker - to breakout VO- (black wire)
- Speaker + to breakout VO+ (red wire)
Example Code
To use with CircuitPython, you simply need to update code.py on your CIRCUITPY drive with the example code. No external libraries are needed.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download code.py file in a zip file. Extract the contents of the zip file, and copy code.py file to your CIRCUITPY drive.
Your CIRCUITPY drive should resemble the following.
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """ CircuitPython PWM Audio Short Tone Tune Demo Plays a five-note tune on a loop. """ import time import array import math import board from audiocore import RawSample from audiopwmio import PWMAudioOut as AudioOut # Increase this to increase the volume of the tone. tone_volume = 0.1 # The tones are provided as a frequency in Hz. You can change the current tones or # add your own to make a new tune. Follow the format with commas between values. tone_frequency = [784, 880, 698, 349, 523] audio = AudioOut(board.A0) while True: # Play each tone in succession. for frequency in tone_frequency: # Compute the sine wave for the current frequency. length = 8000 // frequency sine_wave = array.array("H", [0] * length) for index in range(length): sine_wave[index] = int((1 + math.sin(math.pi * 2 * index / length)) * tone_volume * (2 ** 15 - 1)) sine_wave_sample = RawSample(sine_wave) # Play the current frequency. audio.play(sine_wave_sample, loop=True) time.sleep(0.5) audio.stop() time.sleep(1) # All done playing all tones; start over from the beginning.
Once you have copied code.py to your CIRCUITPY drive, you should hear a series of five tones, 0.5 seconds each, with a 1 second delay between. These tones play in a loop. That's all there is to using the STEMMA Audio amp to play a short tune!
Code Configuration
There are two things you can configure in this demo.
The first is tone volume. It defaults to 0.1. You can increase this value to increase the volume of the tones.
tone_volume = 0.1
The second is the tone frequencies. There are five frequencies, in Hz, provided in a specific order. You can change the Hz of any of these values to change one of the five tones. You can also add more values, following the same format with commas between them, to create a longer tune.
tone_frequency = [784, 880, 698, 349, 523]
Code Walkthrough
The whole demo put together is conceptually quite short.
Following imports and configuration, you create an audio
object on pin A0.
audio = AudioOut(board.A0)
Inside the while
loop, there is a for
loop. This nested loop runs once for each tone frequency, and then repeats.
while True: for frequency in tone_frequency:
Inside the for
loop, you first compute the sine wave for the current frequency.
length = 8000 // frequency sine_wave = array.array("H", [0] * length) for index in range(length): sine_wave[index] = int((1 + math.sin(math.pi * 2 * index / length)) * tone_volume * (2 ** 15 - 1)) sine_wave_sample = RawSample(sine_wave)
At the end of the for
loop, you play the computed sine wave for the current frequency for 0.5 seconds. Then you stop playing for 1 second.
audio.play(sine_wave_sample, loop=True) time.sleep(0.5) audio.stop() time.sleep(1)
When the code reaches the end of the 1 second silence, it begins the for loop again for the next frequency in the cycle.
Text editor powered by tinymce.