Example Code for Arduino-Record
This article offers a comprehensive guide on using FireBeetle ESP32 and DFRobot IIS for audio recording, including hardware setup, software downloads, and sample code for recording and playback.
Hardware Preparation
- FireBeetle ESP32 IoT Microcontroller(SKU: DFR0478) ×1
- Camera & Audio Media Board - FireBeetle Covers(SKU: DFR0498) ×1
- Micro SD Card (FAT32 File System) ×1
- USB Cable ×1
Software Preparation
- Download Arduino IDE: Click to Download Arduino IDE
- Download Arduino Library: Click to download DFRobot_IIS and refer to the guide: How to Install a Library?
Other Preparation Work
- This media board only supports FireBeetle ESP32 main board at present, for other main boards do not equipped with IIS interface.
- The flat ribbon cable of camera is relatively easy-to-break, please be careful and light in connection. The camera shot must be opposite to the SD card slot.
- Do not support video shooting and transmission.
Download programs as below to ESP32 main control board and plug a SD Card.
Once the board connected to microphone by MIC, the other onboard microphone is disabled.
Sample Code
Pressing to record when downloaded, repressing the button to stop and save the file: record.wav to SD card and press to play it.
/*!
* @file DFRobot_IIS.h
* @brief DFRobot's IIS Record Module
* @n IIS Module for Record sound and Save WAV file in SD card
* Insert sd card
* Call the function by pressing user key to control recorder
* This Module will record sound and save it as record.wav then play record.wav
*/
#include <Wire.h>
#include <DFRobot_IIS.h>
DFRobot_IIS iis;
const int buttonPin = 16;
enum Status{
ePrepare,
eRecording,
eStop
}eState=ePrepare;
void setup(){
Serial.begin(115200);
iis.SDCardInit(); // SD card init
iis.init(AUDIO); // Init Audio mode
iis.initRecorder(); // Init recorder
iis.initPlayer(); // Init musice player
iis.record("/record.WAV"); // Enter file name to save recording
Serial.println("Ready to record");
}
void loop(){
static unsigned long timepoint = millis();
static byte count = 0;
if(millis()-timepoint > 200){
if((!digitalRead(buttonPin))&&eState==ePrepare){
timepoint = millis();
iis.recorderControl(BEGIN); // Begin recording
Serial.println("Recording");
eState=eRecording;
}else if((!digitalRead(buttonPin))&&eState==eRecording){
timepoint = millis();
iis.recorderControl(STOP); // Stop recording
Serial.println("Stop and save data");
eState=eStop;
}else if((!digitalRead(buttonPin))&&eState==eStop){
timepoint = millis();
iis.playMusic("/record.WAV"); // Play your record
iis.setSpeakersVolume(50);
iis.setHeadphonesVolume(50);
iis.playerControl(PLAY);
eState=ePrepare;
}
delay(100);
}
}
Result
Button triggers record→stop save→play audio in order, Serial prints status.
Functions
- Initialize SD card in the beginning.
SDCardInit():
- Enter AUDIO mode, state its mode (AUDIO / CAMERA) in use when the SD card is initialized.
init(AUDIO):
- Initialize the recorder: to prepare for calling the record function in AUDIO.
initRecorder():
- Name the record file that will be saved to the SD card, call and play.
record("/record.WAV"):
- Recorder control: to play, pause, stop and save in order.
recorderControl(BEGIN),recorderControl(STOP):
Was this article helpful?
