Example Code for Arduino-VOICE_SYNTHESIS_MODE
This article guides you through using the DFRobot Voice Recorder Module with Arduino for voice synthesis, covering hardware and software setup, wiring, and example code to convert analog inputs into synthesized 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.
- (Optional) Connect a sensor to the A0 pin of the main control board to generate analog input.
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);
uint16_t AnalogRaw0;
float AnalogVolt0;
void setup()
{
Serial.begin(115200);
while (voicerecorder.begin() != 0)
{
Serial.println("i2c device number error!");
delay(1000);
}
Serial.println("i2c connect success!");
}
void loop()
{
AnalogRaw0 = analogRead(A0);
AnalogVolt0 = 5.0 * AnalogRaw0 / 1024;
Serial.println(String(AnalogRaw0));
voicerecorder.voiceSynthesis(CHINESE_LANGUAGE, String(AnalogRaw0), VOICE_SYNTHESIS_MODE);
while (voicerecorder.getNowState() != VOICE_NONE);
delay(1000);
Serial.println(String(AnalogVolt0, 2));
voicerecorder.voiceSynthesis(CHINESE_LANGUAGE, String(AnalogVolt0, 2), VOICE_SYNTHESIS_MODE);
while (voicerecorder.getNowState() != VOICE_NONE);
delay(3000);
}
Result
Expected behavior: The serial monitor will display the raw analog value from A0 and the calculated voltage (rounded to two decimal places). The module will synthesize these values into speech, first the raw value then the voltage.
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. The VOICE_SYNTHESIS_MODE supports values from -999999999.999999999 to 999999999.999999999.
Was this article helpful?
