World of Electronics

Chatting with your Arduino is easier than you think! Voice Commands on Voice Recognition Module Full Tutorial Code + Lab

For this tutorial, you’ll need:

  1. 1 x Arduino UNO
  2. 1 x Resistor 330E
  3. Jumper Wires
  4. 1 x Led 5MM
  5. 1 x Voice Recognition Module

Watch this full video:

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

Download the library before you start

Led Control Example Code:

#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"

VR myVR(2,3);    // 2:RX 3:TX, you can choose your favourite pins.

uint8_t records[7]; // save record
uint8_t buf[64];


int led = 13;


#define onRecord    (0)
#define offRecord   (1) 
#define play (2)


#define playtime 500


/**
  @brief   Print signature, if the character is invisible, 
           print hexible value instead.
  @param   buf     --> command length
           len     --> number of parameters
*/
void printSignature(uint8_t *buf, int len)
{
  int i;
  for(i=0; i<len; i++){
    if(buf[i]>0x19 && buf[i]<0x7F){
      Serial.write(buf[i]);
    }
    else{
      Serial.print("[");
      Serial.print(buf[i], HEX);
      Serial.print("]");
    }
  }
}


/**
  @brief   Print signature, if the character is invisible, 
           print hexible value instead.
  @param   buf  -->  VR module return value when voice is recognized.
             buf[0]  -->  Group mode(FF: None Group, 0x8n: User, 0x0n:System
             buf[1]  -->  number of record which is recognized. 
             buf[2]  -->  Recognizer index(position) value of the recognized record.
             buf[3]  -->  Signature length
             buf[4]~buf[n] --> Signature
*/
void printVR(uint8_t *buf)
{
  Serial.println("VR Index\tGroup\tRecordNum\tSignature");


  Serial.print(buf[2], DEC);
  Serial.print("\t\t");


  if(buf[0] == 0xFF){
    Serial.print("NONE");
  }
  else if(buf[0]&0x80){
    Serial.print("UG ");
    Serial.print(buf[0]&(~0x80), DEC);
  }
  else{
    Serial.print("SG ");
    Serial.print(buf[0], DEC);
  }
  Serial.print("\t");


  Serial.print(buf[1], DEC);
  Serial.print("\t\t");
  if(buf[3]>0){
    printSignature(buf+4, buf[3]);
  }
  else{
    Serial.print("NONE");
  }
  Serial.println("\r\n");
}


void setup()
{
  /** initialize */
  myVR.begin(9600);
  
  Serial.begin(115200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
  
  pinMode(led, OUTPUT);
    
  if(myVR.clear() == 0){
    Serial.println("Recognizer cleared.");
  }else{
    Serial.println("Not find VoiceRecognitionModule.");
    Serial.println("Please check connection and restart Arduino.");
    while(1);
  }
  
  if(myVR.load((uint8_t)onRecord) >= 0){
    Serial.println("onRecord loaded");
  }
  
  if(myVR.load((uint8_t)offRecord) >= 0){
    Serial.println("offRecord loaded");
  }
    if(myVR.load((uint8_t)play) >= 0){
    Serial.println("play loaded");
    }
}


void loop()
{
  int ret;
  ret = myVR.recognize(buf, 50);
  if(ret>0){
    switch(buf[1]){
      case onRecord:
        /** turn on LED */
        digitalWrite(led, HIGH);
        Serial.println("LED ON");
        delay(1000);
        break;
      case offRecord:
        /** turn off LED*/
        digitalWrite(led, LOW);
        Serial.println("LED OFF");
        delay(1000);
        break;
       case play:
        digitalWrite(led, HIGH);        
        delay(playtime);  
        digitalWrite(led, LOW);        
        delay(playtime);
        digitalWrite(led, HIGH);        
        delay(playtime);
        digitalWrite(led, LOW);        
        delay(playtime);
        digitalWrite(led, HIGH);        
        delay(playtime);  
        digitalWrite(led, LOW);        
        delay(playtime);
        digitalWrite(led, HIGH);        
        delay(playtime);
        digitalWrite(led, LOW);        
        delay(playtime);    
      default:
        Serial.println("Record function undefined");
        break;
    }
    /** voice recognized */
    printVR(buf);
  }
}

Talking With Arduino Code:

#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"




VR myVR(2,3);    // 2:RX 3:TX, you can choose your favourite pins.


uint8_t records[7]; // save record
uint8_t buf[64];


int led = 13;


#define Hello    (0)
#define Kifik   (1) 
#define Hamdellah (2)
#define ChouFikeTa3mle (3)
#define Bye (4)


#define playtime 500


/**
  @brief   Print signature, if the character is invisible, 
           print hexible value instead.
  @param   buf     --> command length
           len     --> number of parameters
*/
void printSignature(uint8_t *buf, int len)
{
  int i;
  for(i=0; i<len; i++){
    if(buf[i]>0x19 && buf[i]<0x7F){
      Serial.write(buf[i]);
    }
    else{
      Serial.print("[");
      Serial.print(buf[i], HEX);
      Serial.print("]");
    }
  }
}


/**
  @brief   Print signature, if the character is invisible, 
           print hexible value instead.
  @param   buf  -->  VR module return value when voice is recognized.
             buf[0]  -->  Group mode(FF: None Group, 0x8n: User, 0x0n:System
             buf[1]  -->  number of record which is recognized. 
             buf[2]  -->  Recognizer index(position) value of the recognized record.
             buf[3]  -->  Signature length
             buf[4]~buf[n] --> Signature
*/
void printVR(uint8_t *buf)
{
  Serial.println("VR Index\tGroup\tRecordNum\tSignature");


  Serial.print(buf[2], DEC);
  Serial.print("\t\t");


  if(buf[0] == 0xFF){
    Serial.print("NONE");
  }
  else if(buf[0]&0x80){
    Serial.print("UG ");
    Serial.print(buf[0]&(~0x80), DEC);
  }
  else{
    Serial.print("SG ");
    Serial.print(buf[0], DEC);
  }
  Serial.print("\t");


  Serial.print(buf[1], DEC);
  Serial.print("\t\t");
  if(buf[3]>0){
    printSignature(buf+4, buf[3]);
  }
  else{
    Serial.print("NONE");
  }
  Serial.println("\r\n");
}


void setup()
{
  /** initialize */
  myVR.begin(9600);
  
  Serial.begin(115200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
  
  pinMode(led, OUTPUT);
    
  if(myVR.clear() == 0){
    Serial.println("Recognizer cleared.");
  }else{
    Serial.println("Not find VoiceRecognitionModule.");
    Serial.println("Please check connection and restart Arduino.");
    while(1);
  }




  
  if(myVR.load((uint8_t)Hello) >= 0){
//    Serial.println("You: Hello");
  }
  
  if(myVR.load((uint8_t)Kifik) >= 0){
//    Serial.println("You: Kifik?");
  }
    if(myVR.load((uint8_t)Hamdellah) >= 0){
 //   Serial.println("You: Hamdellah");
    }
        if(myVR.load((uint8_t)ChouFikeTa3mle) >= 0){
//    Serial.println("You: Chou Fike Ta3mle?");
    }
        if(myVR.load((uint8_t)Bye) >= 0){
 //   Serial.println("You: Bye");
    }




    
}


void loop()
{
  int ret;
  ret = myVR.recognize(buf, 50);
  if(ret>0){
    switch(buf[1]){
      case Hello:
        Serial.println("");
        Serial.println("You: Hello");
        Serial.println("Arduino: Ahlan");
        Serial.println("");
        break;
        
      case Kifik:
        Serial.println("");
        Serial.println("You: Kifik?");
        Serial.println("Arduino: Mniha w inta?");
        Serial.println("");
        break;
        
       case Hamdellah:
        Serial.println("");
        Serial.println("You: Hamdellah");
        Serial.println("Arduino: Good!");
        Serial.println("");
        break;


       case ChouFikeTa3mle:
        Serial.println("");
        Serial.println("You: Chou Fike Ta3mle?");
        Serial.println("You: Natretak T2ele");
        Serial.println("");
        break;


       case Bye:
        Serial.println("");
        Serial.println("You: Bye");
        Serial.println("Arduino: Allah ma3ak, ntebih 3al tari2 inta w rayi7, Iza lta2et bel 3askar l 3esmene ma tghayir tari2ak, 3am bi la2to s3edin");
        Serial.println("");
        break;
         
      default:
        Serial.println("Record function undefined");
        break;
    }
    /** voice recognized */
   // printVR(buf);
  }
}