Jalal Mansoori
Published © GPL3+

Simplest Way for Voice Recognition Project Using c#toarduino

Easy way to control devices via voice commands.

BeginnerFull instructions provided47,471
Simplest Way for Voice Recognition Project Using c#toarduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Resistor 221 ohm
Resistor 221 ohm
You can any resistance value of resistor just make sure dont use too less resistance value
×3
LED (generic)
LED (generic)
I used led blue, Red, green led.
×3
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Earphones with microphone
These earphones are the one you use with smarphones Simple! You can also use built_in Computer microphone :)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Visual Studio 2015
Microsoft Visual Studio 2015
You can use any version of Microsoft visual studio just make sure it contains .net framework to create windows form application

Story

Read more

Schematics

Schematic

Simply follow Schematic and connect components on your breadboard
1 -> First of all connect leds and resistors as shown in diagram
2-> Then start one by one and connect led +ive terminal on same column where resistor is connected to arduino digital pin box
GreenLed +ive terminal to digital Pin 8
RedLed +ive terminal to digital Pin 9
BlueLed +ive terminal to digital Pin 10
3-> At last connect Ground from arduino to leds -ive terminal using jumper wire as shown in diagram

Code

Arduino Source Code

C/C++
Arduino Source Code for voice Recognition project
/* This Program is about controlling leds states ON and OFF using voice recognition library of c# (using system.speech) 
 * Exciting Part is you dont need to have any external module to transmit data to arduino because you can easily  
 * use your builtin computer microphone or earphones microphone.   
 *  
 * This Program is just to give basic idea specially to beginners and then its your own creativity how you use it in a useful way.
 * Keep Learning, Share, think and Repeat
 * Enjoy !
 * 
 * By Jalal Mansoori
 */

const int blueLed=10;
const int redLed=9;
const int greenLed=8;

char incomingData='0';
 
void setup() {
  // put your setup code here, to run once:
//getting leds ready
Serial.begin(9600);
pinMode(blueLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  incomingData=Serial.read();

  // Switch case for controlling led in our case we have only 3 Blue, Green and Red 
  switch(incomingData)
  {
      //These cases are only for state ON of led
      // For blue led
      case 'B':
      digitalWrite(blueLed, HIGH);
      break;    
      
      // For red led
      case 'R':
      digitalWrite(redLed, HIGH);
      break;    
      
      // For green led
      case 'G':
      digitalWrite(greenLed, HIGH);
      break;    

      //These cases are  for state OFF of led and case name z , x, c are just randomly given you can also change but 
      // make sure you change it in a c# code as well.
      // For blue led
      case 'Z':
      digitalWrite(blueLed, LOW);
      break;    
      
      // For red led
      case 'X':
       digitalWrite(redLed, LOW);
      break;    
      
      // For green led
      case 'C':
       digitalWrite(greenLed, LOW);
      break;

      //For turning ON all leds at once :)
      case 'V':
      digitalWrite(blueLed, HIGH);
      digitalWrite(redLed, HIGH);
      digitalWrite(greenLed, HIGH);
      break;

      //For turning OFF all leds at once :)
      case 'M':
      digitalWrite(blueLed, LOW);
      digitalWrite(redLed, LOW);
      digitalWrite(greenLed, LOW);
      break;
      
  }
}

C# Code

C#
This code is for visual studio. In this code you can see on top some libraries are included there is using.system.speech library which you need to include in your project reference
This is how you do it
Toolbar
Project->Add Reference->.Net-> Search using system.speech->click OK
Thats all Enjoy :)
/*
  This Program is to connect c# and Arduino to transmit data from computer microphone to arduino board
  By Jalal Mansoori
*/
using System;
using System.Windows.Forms;
using System.IO.Ports; // This library is for connecting c# and Arduino to transmit and receive data through ports
//Below are libraries for voice recognition
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace CsharpCode
{
    public partial class Form1 : Form
    {
        //Creating objects
        SerialPort myPort = new SerialPort();
        SpeechRecognitionEngine re = new SpeechRecognitionEngine();
        SpeechSynthesizer ss = new SpeechSynthesizer(); // When you want program to talk back to you 
        Choices commands = new Choices(); // This is an important class as name suggest we will store our commands in this object
        
       
        public Form1()
        {
            InitializeComponent();
            //Details of Arduino board
            myPort.PortName = "COM5"; // My Port name in Arduino IDE selected COM5 you need to change Port name if it is different  just check in arduinoIDE
            myPort.BaudRate = 9600;  // This Rate is Same as arduino Serial.begin(9600) bits per second
            processing();
            
        }

        // Defined Function processing where main instruction will be executed ! 
        void processing()
        { 
            //First of all storing commands
            commands.Add(new string[] { "Blue On", "Red On", "Green On", "Blue Off", "Red Off", "Green Off", "Exit", "All On", "All Off","Arduino Say Good Bye to makers" });

            //Now we will create object of Grammer in which we will pass commands as parameter
            Grammar gr = new Grammar(new GrammarBuilder(commands));

            // For more information about below funtions refer to site https://docs.microsoft.com/en-us/dotnet/api/system.speech.recognition?view=netframework-4.7.2
            re.RequestRecognizerUpdate(); // Pause Speech Recognition Engine before loading commands
            re.LoadGrammarAsync(gr);
            re.SetInputToDefaultAudioDevice();// As Name suggest input device builtin microphone or you can also connect earphone etc...
            re.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(re_SpeechRecognized);
            
            
        }

        void re_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch(e.Result.Text)
            {
                ////For Led State ON 
                // For blue led
                case "Blue On":
                    sendDataToArduino('B');
                    break;

                // For red led
                case "Red On":
                    sendDataToArduino('R');
                    break;

                // For green led
                case "Green On":
                    sendDataToArduino('G');
                    break;
                
                //For Led State OFF 
                // For blue led
                case "Blue Off":
                    sendDataToArduino('Z');
                    break;

                // For red led
                case "Red Off":
                    sendDataToArduino('X');
                    break;

                // For green led
                case "Green Off":
                    sendDataToArduino('C');
                    break;

                //For turning ON all leds at once :)
                case "All On":
                    sendDataToArduino('V');
                    break;

                //For turning OFF all leds at once :)
                case "All Off":
                    sendDataToArduino('M');
                    break;
                //Program will talk back 
                case "Arduino Say Good Bye to makers":
                    ss.SpeakAsync("Good Bye Makers"); // speech synthesis object is used for this purpose
                    break;
                
                // To Exit Program using Voice :)
                case "Exit":
                    Application.Exit();
                    break;
            }
            txtCommands.Text += e.Result.Text.ToString() + Environment.NewLine;// Whenever we command arduino text will append and shown in textbox
        }

        void sendDataToArduino(char character)
        {
            myPort.Open();
            myPort.Write(character.ToString());
            myPort.Close();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            re.RecognizeAsyncStop();
            //btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnStart.Enabled = true;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            
            re.RecognizeAsync(RecognizeMode.Multiple);
            btnStop.Enabled = true;
            btnStart.Enabled = false;
            MessageBox.Show("Voice Recognition Started !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

Credits

Jalal Mansoori

Jalal Mansoori

3 projects • 46 followers
BS Computer Science | Passionate blogger | Learn, Create, and Share

Comments