pdf-icon

Arduino Guide

Atomic Echo Base Arduino 使用教程

1.准备工作

2.案例程序

参考 M5Atomic-EchoBase 中的RecordPlay案例程序。根据实际连接的设备, 在程序中修改实际使用的IO信息。本教程中使用的主控设备为AtomS3R, 使用的IO配置与AtomS3一致。

注意事项
Atomic Echo Base麦克风支持的采样率范围为16KHz-64KHz, 初始化时需要配置有效的范围内。

初始化

// Initialize the EchoBase with ATOMS3 pinmap.
echobase.init(16000 /*Sample Rate*/, 38 /*I2C SDA*/, 39 /*I2C SCL*/, 7 /*I2S DIN*/, 6 /*I2S WS*/, 5 /*I2S DOUT*/,
              8 /*I2S BCK*/, Wire);

echobase.setSpeakerVolume(50);             // Set speaker volume to 50%.
echobase.setMicGain(ES8311_MIC_GAIN_6DB);  // Set microphone gain to 6dB.

录制与播放

// Recording
echobase.setMute(true);
delay(10);
echobase.record(buffer, RECORD_SIZE);  // Record audio into buffer.
delay(100);

// Playing
echobase.setMute(false);
delay(10);
echobase.play(buffer, RECORD_SIZE);  // Play audio from buffer.
delay(100);

完整程序

基于M5Unified和M5GFX添加基本的显示, 和按键操作,实现按下按键, 开始录制, 完成后自动播放录音。

/*
 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 *
 * SPDX-License-Identifier: MIT
 */

#include "M5Unified.h"
#include <Arduino.h>
#include <M5EchoBase.h>

#define RECORD_SIZE (1024 * 96)  // Define the size of the record buffer to 96KB.

M5EchoBase echobase(I2S_NUM_0);    // Create an instance of the M5EchoBase class.
static uint8_t *buffer = nullptr;  // Pointer to hold the audio buffer.

void setup()
{
    M5.begin();
    M5.Display.setFont(&fonts::FreeMonoBold9pt7b);
    Serial.begin(115200);

    // Initialize the EchoBase with ATOMS3 pinmap.
    echobase.init(16000 /*Sample Rate*/, 38 /*I2C SDA*/, 39 /*I2C SCL*/, 7 /*I2S DIN*/, 6 /*I2S WS*/, 5 /*I2S DOUT*/,
                  8 /*I2S BCK*/, Wire);

    // Initialize the EchoBase with ATOM pinmap.
    // echobase.init(16000 /*Sample Rate*/, 25 /*I2C SDA*/, 21 /*I2C SCL*/, 23 /*I2S DIN*/, 19 /*I2S WS*/, 22 /*I2S
    // DOUT*/, 33 /*I2S BCK*/, Wire);

    echobase.setSpeakerVolume(50);             // Set speaker volume to 50%.
    echobase.setMicGain(ES8311_MIC_GAIN_6DB);  // Set microphone gain to 6dB.

    buffer = (uint8_t *)malloc(RECORD_SIZE);  // Allocate memory for the record buffer.
    // Check if memory allocation was successful.
    if (buffer == nullptr) {
        // If memory allocation fails, enter an infinite loop.
        while (true) {
            M5.Display.println("Failed to allocate memory :(");
            Serial.println("Failed to allocate memory :(");
            delay(1000);
        }
    }

    Serial.println("EchoBase ready, start recording and playing!");
    M5.Display.println("Click! For Record and Play");
}

void loop()
{
    M5.update();
    if (M5.BtnA.wasClicked()) {
        M5.Display.fillScreen(BLACK);
        M5.Display.setCursor(0, 0);
        M5.Display.println("Recording");
        // Recording
        echobase.setMute(true);
        delay(10);
        echobase.record(buffer, RECORD_SIZE);  // Record audio into buffer.
        delay(100);

        M5.Display.println("Playing");
        // Playing
        echobase.setMute(false);
        delay(10);
        echobase.play(buffer, RECORD_SIZE);  // Play audio from buffer.
        delay(100);
        M5.Display.println("Done");
    }
}

3.编译上传

  • 1.下载模式: 不同设备进行程序烧录前需要下载模式, 不同的主控设备该步骤可能有所不同。详情可参考 Arduino IDE上手教程 页面底部的设备程序下载教程列表, 查看具体的操作方式。

  • AtomS3R长按复位按键(大约2秒)直到内部绿色LED灯亮起,便可松开,此时设备已进入下载模式,等待烧录。

  • 2.选中设备端口, 点击Arduino IDE左上角编译上传按钮, 等待程序完成编译并上传至设备。

4.录制与播放

On This Page