Example Code for Arduino-I2C Playback
This guide provides detailed instructions for setting up and using the I2C Voice Recorder Module with Arduino, including hardware and software preparation, wiring diagrams, and sample playback code.
Hardware Preparation
- DFRduino UNO R3 + Gravity IO Expansion Shield * 1
- Gravity: I2C Voice Recorder Module EDU * 1
- Gravity 4pin cable * 1 (Comes with the Gravity module)
- 3.5mm headset or PH2.0 speaker
Software Preparation
- Download Arduino IDE
- Download DFRobot_VoiceRecorder library .
- For Arduino IDE V2.0 (or above), you can search and install the "DFRobot_VoiceRecorder" in the library manager.
- For the Arduino IDE V1.8.19(or below), you can refer to (About how to install the library?) to install .zip library
Wiring Diagram
- Connect the Gravity-4P I2C interface to the main control board I2C interface. If you don’t need I2C control, leave the blue and green wires unconnected.
- Red: 3.3~5V +
- Black: GND -
- Blue: I2C SCL
- Green: I2C SDA
- Connect the 3.5mm headphone jack or the PH2.0 speaker port, the two cannot be connected at the same time, otherwise the audio will not be played normally.
Other Preparation Work
- Connect according to the wiring instructions
- Install the library file as per the link provided.
Sample Code
#include "DFRobot_VoiceRecorder.h"
#define I2C_ADDRESS (0x30) //I2C Address
DFRobot_VoiceRecorder_I2C voicerecorder(&Wire, I2C_ADDRESS);
void setup() {
Serial.begin(115200);
while (voicerecorder.begin() != 0)
{
Serial.println("i2c device number error!");
delay(1000);
}
Serial.println("i2c connect success!");
}
void loop() {
for (uint8_t voiceNum = 0; voiceNum < 10; voiceNum++)
{
voicerecorder.setVoiceNumber(voiceNum); //Set number
Serial.println("setVoiceNumber " + String(voiceNum));
if (VOICE_NONE == voicerecorder.playVoiceStart()) //Start playing, and check if it is empty
{
Serial.println(String(voiceNum) + " is empty"); //Enter the next number if it is empty
delay(3000);
continue;
}
Serial.println("VoiceStart");
while (voicerecorder.getNowState() == VOICE_PLAYING) //Show remaining available time, exist when ending
{
Serial.println("TimeRemaining " + String(voicerecorder.getTimeRemaining()));
delay(1000);
}
delay(3000);
}
}
Result
Expected behavior: The module will loop through recordings 0 to 9. For each number, if there is a recording, it will play and the serial monitor will display the current number and remaining play time. Empty recordings will be skipped.
Additional Information
The default I2C address of the module is 0x30. To change the address, refer to the I2C Address Settings in the Communication Protocol Description section.
Was this article helpful?
