Example Code for Arduino-I2C Recording
This guide details the process of setting up an Arduino-I2C voice recorder, outlining the necessary hardware and software preparations, and providing sample code to facilitate the recording process.
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 // default I2C address 0x30
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!");
Serial.println("Delect Voice 0");
voicerecorder.setVoiceNumber(VOICE_NUMBER_0);
Serial.println("Delete Voice");
voicerecorder.deleteVoice();
for (int8_t n = 3; n > 0; n--)
{
Serial.println(n);
delay(1000);
}
Serial.println("Recode Start");
voicerecorder.recordvoiceStart();
for (int8_t n = 20; n > 0; n--)
{
Serial.println(n);
delay(1000);
}
voicerecorder.recordVoiceEnd();
Serial.println("Recode End");
}
void loop()
{
}
Result
Expected behavior: The serial monitor will display the connection status, countdown, recording start, and recording end. The module will record for 20 seconds, with the digital tube showing the remaining recording time.
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?
