reSpeaker XVF3800 USB Mic Array with XIAO ESP32S3 I2S Test
This project serves as a test sketch to verify the functionality of the I2S interface between the XIAO ESP32S3 and the ReSpeaker XVF3800 USB 4-Mic Array. The goal is to confirm that data transmission and reception through the I2S bus are working correctly. To achieve this, the sketch generates a synthetic square wave audio signal and writes it to the I2S interface. It then reads audio data from the XVF3800 microphone array and verifies the integrity of the received samples.
Objective
The primary objective of this project is to test and validate both I2S transmission (TX) and reception (RX) operations. It aims to ensure that the ReSpeaker XVF3800 microphone array can successfully send audio data back to the ESP32S3 over I2S. By doing so, it establishes a reliable baseline for I2S communication between the two devices, paving the way for more advanced audio and voice-processing applications.
How It Works
During the setup phase, the sketch initializes serial communication and starts the I2S interface using stereo configuration with 32-bit sample width. The I2S pins are configured according to the ESP32S3’s hardware mappings. In the transmit (TX) phase, a 440 Hz square wave is generated by toggling the amplitude value of the audio signal. A total of 32,000 samples are written to the I2S bus. In the receive (RX) phase, the sketch reads 32,000 samples from the XVF3800 microphone array. It then counts how many of these samples are non-zero and valid. If more than 16,000 valid samples are received, the test passes. If not, a second read attempt is made. If the result still falls below the threshold, the test is marked as failed.
Code
#include <ESP_I2S.h>
#include <wav_header.h>
I2SClass I2S;
const int sampleRate = 16000; // sample rate in Hz
const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 500; // amplitude of square wave
int32_t sample = amplitude; // current sample value
const int halfWavelength = sampleRate / frequency; // half wavelength of square wave
int count = 0;
int i2s_read = 0;
bool i2s_test = true;
void setup() {
Serial.begin(115200);
while(!Serial); // Wait for the serial port to connect
I2S.setPins(8, 7, 44, 43); // Configure I2S pins
if (!I2S.begin(I2S_MODE_STD, sampleRate, I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO)){
Serial.println("Failed to initialize I2S!");
while(1); // Halt if failed to initialize
}
}
void loop() {
if(i2s_test){
Serial.println("I2S test!");
// Generate and write square wave samples
for(int i = 0; i < 32000; i++){
if(count % halfWavelength == 0){
sample = -sample; // Toggle sample value to create square wave
}
I2S.write(sample); // Write sample to right channel
count++;
}
// Read samples from I2S and count non-zero samples
i2s_read=0;
for(int i = 0; i < 32000; i++){
int sample_read = I2S.read();
Serial.print(sample_read);
Serial.print(" ");
if(sample_read != 0 && sample_read != 0xFFFF){
i2s_read++;
}
}
Serial.println();
// Check if the number of valid samples is above a threshold
if(i2s_read > 16000){
Serial.println("I2S RX pass!");
} else{
i2s_read = 0;
for(int i = 0; i < 32000; i++){
int sample_read = I2S.read();
if (sample_read != 0 && sample_read != 0xFFFF){
i2s_read++;
}
}
if(i2s_read > 16000)
Serial.println("I2S RX pass!");
else
Serial.println("I2S RX fail!");
}
Serial.println("OVER");
i2s_test = false;
}
}
Tech Support & Product Discussion
Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.