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

Software Preparation

Wiring Diagram

  1. 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
  2. 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

  1. Connect according to the wiring instructions
  2. 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?

TOP