Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
Ripred
Published

Trainable Voice (or Sound) Commands for Any Microprocessor

I got some new toys in the mail this weekend! I got a new DF2301Q sound recognition module from dfrobot.com module that's pretty amazing.

IntermediateFull instructions provided1 hour433
Trainable Voice (or Sound) Commands for Any Microprocessor

Things used in this project

Hardware components

DFRobot Gravity: Offline Language Learning Voice Recognition Sensor for Arduino / Raspberry Pi / Python / ESP32 - I2C & UART
×1
Arduino UNO
Arduino UNO
×1
DC motor (generic)
×1
5mW Laser Module emitter - Red Point
Seeed Studio 5mW Laser Module emitter - Red Point
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×2
Resistor 1k ohm
Resistor 1k ohm
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

DF2301Q_Demo.ino

C/C++
Compile for Arduino Uno and upload. Attach the DF2301Q to the I2C pins SDA (A4) and and SCL (A5)
/*!
 * @file  i2c.ino
 * @brief  Control the voice recognition module via I2C
 * @n  Get the recognized command ID and play the corresponding reply audio according to the ID;
 * @n  Get and set the wake-up state duration, set mute mode, set volume, and enter the wake-up state
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence  The MIT License (MIT)
 * @author  [qsjhyy](yihuan.huang@dfrobot.com)
 * @version  V1.0
 * @date  2022-12-30
 * @url  https://github.com/DFRobot/DFRobot_DF2301Q
 * 
 * 
 * Testing version 1.1 July 2023 ++trent m. wyatt
 * 
 */
#include "DFRobot_DF2301Q.h"

/*
    ***********************************     ***********************************     ***********************************
    Wake-up words                    ID
    Wake-up words for learning        1
    Hello robot                       2

    ***********************************     ***********************************     ***********************************
    Commands for learning            ID     Commands for learning            ID     Commands for learning            ID

    The first custom command          5     The second custom command         6     The third custom command          7
    The fourth custom command         8     The fifth custom command          9     The sixth custom command         10
    The seventh custom command       11     The eighth custom command        12     The ninth custom command         13
    The tenth custom command         14     The eleventh custom command      15     The twelfth custom command       16
    The thirteenth custom command    17     The fourteenth custom command    18     The fifteenth custom command     19
    The sixteenth custom command     20     The seventeenth custom command   21

    ***********************************     ***********************************     ***********************************
    Fixed Command Words              ID     Fixed Command Words              ID     Fixed Command Words              ID

    Go forward                       22     Retreat                          23     Park a car                       24
    Turn left ninety degrees         25     Turn left forty-five degrees     26     Turn left thirty degrees         27
    Turn right forty-five degrees    29     Turn right thirty degrees        30     Shift down a gear                31
    Line tracking mode               32     Light tracking mode              33     Bluetooth mode                   34
    Obstacle avoidance mode          35     Face recognition                 36     Object tracking                  37
    Object recognition               38     Line tracking                    39     Color recognition                40
    Tag recognition                  41     Object sorting                   42     Qr code recognition              43
    General settings                 44     Clear screen                     45     Learn once                       46
    Forget                           47     Load model                       48     Save model                       49
    Take photos and save them        50     Save and return                  51     Display number zero              52
    Display number one               53     Display number two               54     Display number three             55
    Display number four              56     Display number five              57     Display number six               58
    Display number seven             59     Display number eight             60     Display number nine              61
    Display smiley face              62     Display crying face              63     Display heart                    64
    Turn off dot matrix              65     Read current posture             66     Read ambient light               67
    Read compass                     68     Read temperature                 69     Read acceleration                70
    Reading sound intensity          71     Calibrate electronic gyroscope   72     Turn on the camera               73
    Turn off the camera              74     Turn on the fan                  75     Turn off the fan                 76
    Turn fan speed to gear one       77     Turn fan speed to gear two       78     Turn fan speed to gear three     79
    Start oscillating                80     Stop oscillating                 81     Reset                            82
    Set servo to ten degrees         83     Set servo to thirty degrees      84     Set servo to forty-five degrees  85
    Set servo to sixty degrees       86     Set servo to ninety degrees      87     Turn on the buzzer               88
    Turn off the buzzer              89     Turn on the speaker              90     Turn off the speaker             91
    Play music                       92     Stop playing                     93     The last track                   94
    The next track                   95     Repeat this track                96     Volume up                        97
    Volume down                      98     Change volume to maximum         99     Change volume to minimum        100
    Change volume to medium         101     Play poem                       102     Turn on the light               103
    Turn off the light              104     Brighten the light              105     Dim the light                   106
    Adjust brightness to maximum    107     Adjust brightness to minimum    108     Increase color temperature      109
    Decrease color temperature      110     Adjust color temperature to maximum 111 Adjust color temperature to minimum 112
    Daylight mode                   113     Moonlight mode                  114     Color mode                      115
    Set to red                      116     Set to orange                   117     Set to yellow                   118
    Set to green                    119     Set to cyan                     120     Set to blue                     121
    Set to purple                   122     Set to white                    123     Turn on ac                      124
    Turn off ac                     125     Increase temperature            126     Decrease temperature            127
    Cool mode                       128     Heat mode                       129     Auto mode                       130
    Dry mode                        131     Fan mode                        132     Enable blowing up & down        133
    Disable blowing up & down       134     Enable blowing right & left     135     Disable blowing right & left    136
    Open the window                 137     Close the window                138     Open curtain                    139
    Close curtain                   140     Open the door                   141     Close the door                  142

    ***********************************     ***********************************     ***********************************
    Learning-related commands        ID     Learning-related commands        ID     Learning-related commands        ID
    Learning wake word              200     Learning command word           201     Re-learn                        202
    Exit learning                   203     I want to delete                204     Delete wake word                205
    Delete command word             206     Exit deleting                   207     Delete all                      208
 */

// I2C communication
DFRobot_DF2301Q_I2C sr;

void send_cmd(uint8_t const cmd) {
    sr.playByCMDID(cmd);
}

void reset_all() {
    send_cmd(0);
}

void check_serial() {
    uint32_t start = millis();
    while (millis() < start + 100) {
        int len = Serial.available();
        if (len >= 2) {
            int value = Serial.parseInt();
            if (value >= 0) {
                Serial.print(F("Recieved serial command: "));
                Serial.print(value);
                Serial.print(F(", sending to DF2301Q\n"));
                send_cmd(value);
            }
        }
    }
}

#define LASER_PIN  2
#define MOTOR_PIN  3

void setup()
{
    Serial.begin(115200);
    while (!Serial) {}
    Serial.println(F("starting"));
    
    pinMode(MOTOR_PIN, OUTPUT);
    pinMode(LASER_PIN, OUTPUT);

    // Init the sensor
    while( !( sr.begin() ) ) {
        Serial.println(F("Communication with the DFRobot DF2301Q failed, check the connections"));
        Serial.println(F("The I2C address should be 0x64"));
        delay(3000);
    }
    Serial.println(F("Communication with the DFRobot DF2301Q successfully started"));

    /**
     * @brief Set voice volume
     * @param voc - Volume value(1~7)
     */
    sr.setVolume(6);
    
    /**
     * @brief Set mute mode
     * @param mode - Mute mode; set value 1: mute, 0: unmute
     */
    sr.setMuteMode(0);
    
    /**
     * @brief Set wake-up duration
     * @param wakeTime - Wake-up duration (0-255)
     */
    sr.setWakeTime(255);

    /**
      * @brief Get wake-up duration
      * 
      * 
      * @return The currently-set wake-up period
      */
    uint8_t wakeTime = 0;
    wakeTime = sr.getWakeTime();
    Serial.print("wakeTime = ");
    Serial.println(wakeTime, DEC);

    /**
     * @brief Play the corresponding reply audio according to the command word ID
     * @param CMDID - Command word ID
     * @note Can enter wake-up state through ID-1 in I2C mode
     */
//    sr.playByCMDID(23);   // Common word ID

}

void loop()
{
    check_serial();

    /**
     * @brief Get the ID corresponding to the command word 
     * @return Return the obtained command word ID, returning 0 means no valid ID is obtained
     */
//    Serial.print(F("checking..\n"));
    uint8_t cmd = sr.getCMDID();

    if (0 != cmd) {
        Serial.print(F("Command = "));
        Serial.println(cmd, DEC);
    }
    
    switch (cmd) {
        case 2: // Wake up word 2
            break;
        
        case 5:
            analogWrite(LASER_PIN, 255);
            break;
        
        case 6:
            analogWrite(LASER_PIN, 0);
            break;
        
        case 7:
            digitalWrite(MOTOR_PIN, HIGH);
            break;
        
        case 8:
            digitalWrite(MOTOR_PIN, LOW);
            break;
        
        case 9:
            analogWrite(LASER_PIN, 5);
            break;
        
        case 10:
            analogWrite(LASER_PIN, 2);
            break;
        
        default:
            break;
    }

//    delay(1000);
}

Credits

Ripred
9 projects • 5 followers
30++ yrs development exp. Kernel, Compiler, ML, Sentiment Analysis, Robotics, Embedded, Game Theory, Networking, and Intelligence work.
Contact

Comments

Please log in or sign up to comment.