Using the I2S BFF and the QT Py RP2040 with Arduino involves soldering up the two boards, connecting them in the appropriate orientation, and running the provided example code. These examples do not require any separate libraries.
QT Py to BFF
Connect a QT Py RP2040 to the I2S BFF by soldering on a pair of pin headers to one, and a pair of socket headers to the other.
In this setup, the pin headers are on the QT Py on the top of the image, and the socket headers are on the I2S BFF on the bottom of the image. They should be plugged in with the backs of the boards facing each other. The I2S BFF has on the back, a USB label with an arrow, to indicate which end of the board should be aligned with the USB connector on the QT Py.
I2S BFF to Speaker
Connect a speaker to your I2S BFF as shown below.
Plug the PicoBlade-compatible connector on your speaker into the PicoBlade-compatible socket on your I2S BFF.
The socket is keyed along with the connector to ensure avoiding plugging it in backwards.
Speaker to Connector Cable (Optional)
If your speaker comes with bare wires, you'll want to pick up this cable, and solder it to your speaker.
Solder the cable to the speaker by connecting the wires on the cable to the same color wires on the speaker.
Tone Example Code
This example plays a tone. The code below was adapted from the SimpleTone.ino demo in the arduino-pico core I2S library examples.
// SPDX-FileCopyrightText: 2016 Sandeep Mistry // SPDX-FileCopyrightText: 2022 Earle F. Philhower, III // SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries // // SPDX-License-Identifier: MIT /* This example generates a square wave based tone at a specified frequency and sample rate. Then outputs the data using the I2S interface to a MAX08357 I2S Amp Breakout board. created 17 November 2016 by Sandeep Mistry modified for RP2040 by Earle F. Philhower, III <[email protected]> bool setBCLK(pin_size_t pin); - This assigns two adjacent pins - the pin after this one (one greater) is the WS (word select) signal, which toggles before the sample for each channel is sent bool setDATA(pin_size_t pin); - Sets the DOUT pin, can be any valid GPIO pin */ #include <I2S.h> // Create the I2S port using a PIO state machine I2S i2s(OUTPUT); // GPIO pin numbers #define pBCLK A2 // QT Py BFF default BITCLOCK #define pWS A1 // QT Py BFF default LRCLOCK #define pDOUT A0 // QT Py BFF default DATA const int frequency = 440; // frequency of square wave in Hz const int amplitude = 500; // amplitude of square wave const int sampleRate = 16000; // 16 KHz is a good quality const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave int16_t sample = amplitude; // current sample value int count = 0; void setup() { Serial.begin(115200); while (!Serial) delay(10); Serial.println("I2S simple tone"); i2s.setBCLK(pBCLK); i2s.setDATA(pDOUT); i2s.setBitsPerSample(16); // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(sampleRate)) { Serial.println("Failed to initialize I2S!"); while (1); // do nothing } } void loop() { if (count % halfWavelength == 0) { // invert the sample every half wavelength count multiple to generate square wave sample = -1 * sample; } // write the same sample twice, once for left and once for the right channel i2s.write(sample); i2s.write(sample); // increment the counter for the next sample count++; }
Once you have uploaded the sketch to your QT Py, you'll hear a tone.
You can change the frequency (pitch) of the tone by updating the frequency
variable.
const int frequency = 440; // frequency of square wave in Hz
Audio Playback Example Code
This example plays a PCM audio file when the Boot button is pressed. Check out this Python script to convert WAV files to PCM files.
Click the button below to download the source code and header file. Unzip it, and open it with the Arduino IDE.
When you open the code in the Arduino IDE, you will see the Arduino sketch code in one tab, and the header file in a second tab.
// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries // // SPDX-License-Identifier: MIT /* This example plays a 'raw' PCM file from memory to I2S */ #include <I2S.h> #include "startup.h" // audio file in flash // Create the I2S port using a PIO state machine I2S i2s(OUTPUT); // GPIO pin numbers #define pBCLK A2 // QT Py BFF default BITCLOCK #define pWS A1 // QT Py BFF default LRCLOCK #define pDOUT A0 // QT Py BFF default DATA #define USERBUTTON 21 // QT Py RP2040 built in button // variable shared between cores volatile bool playaudio = false; void setup() { Serial.begin(115200); //while (!Serial) delay(10); Serial.println("I2S playback demo"); pinMode(USERBUTTON, INPUT_PULLUP); } void loop() { // on button press tell the other core to play audio clip! if (!digitalRead(USERBUTTON)) { playaudio = true; } else { playaudio = false; } } void setup1() { i2s.setBCLK(pBCLK); i2s.setDATA(pDOUT); i2s.setBitsPerSample(16); } void loop1() { // the main loop will tell us when it wants us to play! if (playaudio) { play_i2s(startupAudioData, sizeof(startupAudioData), startupSampleRate); } } void play_i2s(const uint8_t *data, uint32_t len, uint32_t rate) { // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(rate)) { Serial.println("Failed to initialize I2S!"); delay(500); i2s.end(); return; } for(uint32_t i=0; i<len; i++) { uint16_t sample = (uint16_t)data[i] << 6; // our data is 10 bit but we want 16 bit so we add some gain // write the same sample twice, once for left and once for the right channel i2s.write(sample); i2s.write(sample); } i2s.end(); }
Once you've uploaded the sketch onto the QT Py RP2040, press the Boot button to hear the clip played.
The Boot button (highlighted in blue in the image), is located towards the bottom edge of the board between the STEMMA QT connector on the right and the USB connector on the left.
Text editor powered by tinymce.