Example Code for Arduino-UART Control LED
Control the voice recognition module via UART, get the recognized command ID and play the corresponding reply audio according to the ID; set the wake-up state duration, set mute mode, set volume, and control the LED to turn on/off based on the command ID.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- Gravity: Voice Recognition Module x I2C & UART x 1
- Gravity: Digital RED LED Light Module x 1
Software Preparation
- Arduino IDE
- Download and install the DFRobot_DF2301Q Library. (About how to install the library?)
Wiring Diagram

Other Preparation Work
Please switch the communication mode switch to the UART and download the required library file DFRobot_DF2301Q library for the code.
Sample Code
/*!
* @file uart.ino
* @brief Control the voice recognition module via UART
* @n Get the recognized command ID and play the corresponding reply audio according to the ID;
* @n Set the wake-up state duration, set mute mode, set volume, enter the wake-up state, and reset the module
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [qsjhyy]([email protected])
* @version V1.0
* @date 2022-04-02
* @url https://github.com/DFRobot/DFRobot_DF2301Q
*/
#include "DFRobot_DF2301Q.h"
#define Led 8
/**
@brief DFRobot_URM13_RTU constructor
@param serial - serial ports for communication, supporting hard and soft serial ports
@param rx - UART The pin for receiving data
@param tx - UART The pin for transmitting data
*/
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266)) // Use software serial
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
DFRobot_DF2301Q_UART asr(/*softSerial =*/&softSerial);
#elif defined(ESP32) // Use the hardware serial with remappable pin: Serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1, /*rx =*/D9, /*tx =*/D7);
#else // Use hardware serial: Serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1);
#endif
void setup() {
Serial.begin(115200);
pinMode(Led, OUTPUT); //Init LED pin to output mode
digitalWrite(Led, LOW); //Set LED pin to low
// Init the sensor
while (!(asr.begin())) {
Serial.println("Communication with device failed, please check connection");
delay(3000);
}
Serial.println("Begin ok!");
/**
@brief Reset module
*/
// asr.resetModule();
/**
@brief Set commands of the module
@param setType - Set type
@n DF2301Q_UART_MSG_CMD_SET_VOLUME: Set volume, the set value range 1-7
@n DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP: Enter wake-up state; set value 0
@n DF2301Q_UART_MSG_CMD_SET_MUTE Mute mode; set value 1: mute, 0: unmute
@n DF2301Q_UART_MSG_CMD_SET_WAKE_TIME ; Wake-up duration; the set value range 0-255s
@param setValue - Set value, refer to the set type above for the range
*/
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_MUTE, 0);
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_VOLUME, 7);
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 20);
//asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP, 0);
/**
@brief Play the corresponding reply audio according to the command word ID
@param CMDID - Command word ID
*/
asr.playByCMDID(23);
}
void loop() {
/**
@brief Get the ID corresponding to the command word
@return Return the obtained command word ID, returning 0 means no valid ID is obtained
*/
uint8_t CMDID = asr.getCMDID();
switch (CMDID) {
case 103: //If the command is “Turn on the light”
digitalWrite(Led, HIGH); //Turn on the LED
Serial.println("received'Turn on the light',command flag'103'"); //Serial transmits "received"Turn on the light",command flag"103""
break;
case 104: //If the command is “Turn off the light”
digitalWrite(Led, LOW); //Turn off the LED
Serial.println("received'Turn off the light',command flag'104'"); //The serial transmits "received"Turn off the light",command flag"104""
break;
default:
if (CMDID != 0) {
Serial.print("CMDID = "); //Print command ID
Serial.println(CMDID);
}
}
delay(300);
}
Result
Awaken the speech recognition module using a predefined or learned awakening phrase. Subsequently, command the illumination module with the phrases "Turn on the light" or "Turn off the light" for controlling its functionality.

Was this article helpful?
