Using the Adafruit STEMMA Audio Amp with Arduino is as easy as wiring up the amp to an RP2040 microcontroller, and running the provided example code. No libraries are necessary! This page uses the Feather RP2040 to demonstrate.
Wiring
Connect the audio amp to a Feather RP2040 exactly as shown below.
You can wire it up using the STEMMA port 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)
Alternatively, you can use a solderless 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)
// SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries // SPDX-FileCopyrightText: Earle F. Philhower, III // // SPDX-License-Identifier: MIT /* This example plays a tune through a mono amplifier using a simple sine wave. Released to the public domain by Earle F. Philhower, III <[email protected]> Adapted from stereo original example 2023 by Kattni Rembor */ #include <PWMAudio.h> PWMAudio pwm(0, true); // GP0 = left, GP1 = right const int freq = 48000; // Output frequency for PWM int16_t mono = 0; const int notes[] = { 784, 880, 698, 349, 523 }; const int dly[] = { 400, 500, 700, 500, 1000 }; const int noteCnt = sizeof(notes) / sizeof(notes[0]); int freqMono = 1; double sineTable[128]; // Precompute sine wave in 128 steps unsigned int cnt = 0; void cb() { while (pwm.availableForWrite()) { double now = ((double)cnt) / (double)freq; int freqScale = freqMono << 7; // Prescale by 128 to avoid FP math later on pwm.write((int16_t)(mono * sineTable[(int)(now * freqScale) & 127])); cnt++; } } void setup() { // Set up sine table for waveform generation for (int i = 0; i < 128; i++) { sineTable[i] = sin(i * 2.0 * 3.14159 / 128.0); } pwm.setBuffers(4, 32); // Give larger buffers since we're are 48khz sample rate pwm.onTransmit(cb); pwm.begin(freq); } void loop() { delay(1000); mono = 0; Serial.println("loop"); for (int i = 0; i < noteCnt; i++) { freqMono = notes[i]; mono = 5000; delay(dly[i]); } mono = 0; delay(3000); }
Once you've successfully uploaded the sketch to your Feather RP2040, you'll hear series of tones played through the speaker. That's your short tune!
Text editor powered by tinymce.