Example Code for Arduino-VOICE_REPLACE_MODE
Guides users in setting up and coding Arduino's VOICE_REPLACE_MODE for voice synthesis, detailing hardware preparation, software installation, wiring, and presenting sample code for synthesizing Fibonacci sequence values into speech.
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);
uint32_t a[3] = {0, 0, 1};
void setup()
{
Serial.begin(115200);
while (voicerecorder.begin() != 0)
{
Serial.println("i2c device number error!");
delay(1000);
}
Serial.println("i2c connect success!");
}
void loop()
{
Serial.println(String(a[2]));
voicerecorder.voiceSynthesis(CHINESE_LANGUAGE, String(a[2]), VOICE_REPLACE_MODE);
a[0] = a[1], a[1] = a[2];
a[2] = a[0] + a[1];
while (voicerecorder.getNowState() != VOICE_NONE);
delay(3000);
}
Result
Expected behavior: The serial monitor will display the Fibonacci sequence values. The module will synthesize each value into speech using the VOICE_REPLACE_MODE, which reads each digit individually.
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. The VOICE_REPLACE_MODE supports up to 32 characters, skipping non-numeric characters.
Was this article helpful?
