zoologist 发表于 2024-7-28 10:24:42

MAX98357 I2S功放模块测试

MAX98357 I2S功放模块【参考1】是一款 I2S 接口的数字式D类功放。这个模块使用3.3V~5V宽电压供电,可以切换左右声道。

这次编写简单的代码进行发声测试。
硬件连接如下:






MAX98357ESP32S3用途
SPK+/- 连接喇叭 连接喇叭正负极,喇叭输出
DIN   48从ESP32S3 发送的 I2S数据
BCLK45从ESP32S3 发送的 I2SClock
LRC35从ESP32S3 发送的 I2S 左右声道选择信号
GNDGND地
VCC5V供电


按照上述方案连接好后,烧录如下代码:
#include <I2S.h>
const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 32000; // amplitude of square wave
const int sampleRate = 8000; // sample rate in Hz
const int bps = 16;

const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave

short sample = amplitude; // current sample value
int count = 0;

i2s_mode_t mode = I2S_PHILIPS_MODE; // I2S decoder is needed
// i2s_mode_t mode = ADC_DAC_MODE; // Audio amplifier is needed

// Mono channel input
// This is ESP specific implementation -
//   samples will be automatically copied to both channels inside I2S driver
//   If you want to have true mono output use I2S_PHILIPS_MODE and interlay
//   second channel with 0-value samples.
//   The order of channels is RIGH followed by LEFT
//i2s_mode_t mode = I2S_RIGHT_JUSTIFIED_MODE; // I2S decoder is needed

void setup() {
Serial.begin(115200);
Serial.println("I2S simple tone");
delay(5000);

    //setAllPins(int sckPin, int fsPin, int sdPin, int outSdPin, int inSdPin);
I2S.setAllPins(45      , 35       , 48       , 48          , -1);

// start I2S at the sample rate with 16-bits per sample
if (!I2S.begin(mode, sampleRate, bps)) {
   
    Serial.println("Failed to initialize I2S!");
    while (1); // do nothing
}
}

void loop() {

   while (Serial.available()) {
    char c = Serial.read();
    if (c == '3') {
      ESP.restart();
    }
    // 主机端发送 l, 回复 z 用于识别串口
    if (c == '1') {
      Serial.print('z');
    }
    // 主机端发送 l, 回复 z 用于识别串口
    if (c == '2') {
      Serial.printf("getSckPin:%d getFsPin:%d getDataPin:%d",
                      I2S.getSckPin(),
                      I2S.getFsPin(),
                      I2S.getDataPin());
    }   
}


    if (count % halfWavelength == 0 ) {
      // invert the sample every half wavelength count multiple to generate square wave
      sample = -1 * sample;
    }

    if(mode == I2S_PHILIPS_MODE || mode == ADC_DAC_MODE){ // write the same sample twice, once for Right and once for Left channel
      I2S.write(sample); // Right channel
      I2S.write(sample); // Left channel
    }else if(mode == I2S_RIGHT_JUSTIFIED_MODE || mode == I2S_LEFT_JUSTIFIED_MODE){
      // write the same only once - it will be automatically copied to the other channel
      I2S.write(sample);
    }

    // increment the counter for the next sample
    count++;
}

代码实现的是生成一个 440Hz的音频,然后通过喇叭播放出来。
【DFRobot的 max98357 iis 测试】 https://www.bilibili.com/video/BV1yH4y1c7NT/?share_source=copy_web&vd_source=5ca375392c3dd819bfc37d4672cb6d54

参考:
1.https://www.dfrobot.com.cn/goods-3573.html


页: [1]
查看完整版本: MAX98357 I2S功放模块测试