Components and supplies
Jumper wires (generic)
Arduino UNO
Earphones with microphone
Resistor 221 ohm
Breadboard (generic)
LED (generic)
Apps and platforms
Arduino IDE
Visual Studio 2015
Project description
Code
Arduino Source Code
c_cpp
Arduino Source Code for voice Recognition project
1/* This Program is about controlling leds states ON and OFF using voice recognition library of c# (using system.speech) 2 * Exciting Part is you dont need to have any external module to transmit data to arduino because you can easily 3 * use your builtin computer microphone or earphones microphone. 4 * 5 * 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. 6 * Keep Learning, Share, think and Repeat 7 * Enjoy ! 8 * 9 * By Jalal Mansoori 10 */ 11 12const int blueLed=10; 13const int redLed=9; 14const int greenLed=8; 15 16char incomingData='0'; 17 18void setup() { 19 // put your setup code here, to run once: 20//getting leds ready 21Serial.begin(9600); 22pinMode(blueLed, OUTPUT); 23pinMode(redLed, OUTPUT); 24pinMode(greenLed, OUTPUT); 25} 26 27void loop() { 28 // put your main code here, to run repeatedly: 29 incomingData=Serial.read(); 30 31 // Switch case for controlling led in our case we have only 3 Blue, Green and Red 32 switch(incomingData) 33 { 34 //These cases are only for state ON of led 35 // For blue led 36 case 'B': 37 digitalWrite(blueLed, HIGH); 38 break; 39 40 // For red led 41 case 'R': 42 digitalWrite(redLed, HIGH); 43 break; 44 45 // For green led 46 case 'G': 47 digitalWrite(greenLed, HIGH); 48 break; 49 50 //These cases are for state OFF of led and case name z , x, c are just randomly given you can also change but 51 // make sure you change it in a c# code as well. 52 // For blue led 53 case 'Z': 54 digitalWrite(blueLed, LOW); 55 break; 56 57 // For red led 58 case 'X': 59 digitalWrite(redLed, LOW); 60 break; 61 62 // For green led 63 case 'C': 64 digitalWrite(greenLed, LOW); 65 break; 66 67 //For turning ON all leds at once :) 68 case 'V': 69 digitalWrite(blueLed, HIGH); 70 digitalWrite(redLed, HIGH); 71 digitalWrite(greenLed, HIGH); 72 break; 73 74 //For turning OFF all leds at once :) 75 case 'M': 76 digitalWrite(blueLed, LOW); 77 digitalWrite(redLed, LOW); 78 digitalWrite(greenLed, LOW); 79 break; 80 81 } 82} 83
C# Code
csharp
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 :)
1/* 2 This Program is to connect c# and Arduino to transmit data from computer microphone to arduino board 3 By Jalal Mansoori 4*/ 5using System; 6using System.Windows.Forms; 7using System.IO.Ports; // This library is for connecting c# and Arduino to transmit and receive data through ports 8//Below are libraries for voice recognition 9using System.Speech; 10using System.Speech.Recognition; 11using System.Speech.Synthesis; 12 13namespace CsharpCode 14{ 15 public partial class Form1 : Form 16 { 17 //Creating objects 18 SerialPort myPort = new SerialPort(); 19 SpeechRecognitionEngine re = new SpeechRecognitionEngine(); 20 SpeechSynthesizer ss = new SpeechSynthesizer(); // When you want program to talk back to you 21 Choices commands = new Choices(); // This is an important class as name suggest we will store our commands in this object 22 23 24 public Form1() 25 { 26 InitializeComponent(); 27 //Details of Arduino board 28 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 29 myPort.BaudRate = 9600; // This Rate is Same as arduino Serial.begin(9600) bits per second 30 processing(); 31 32 } 33 34 // Defined Function processing where main instruction will be executed ! 35 void processing() 36 { 37 //First of all storing commands 38 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" }); 39 40 //Now we will create object of Grammer in which we will pass commands as parameter 41 Grammar gr = new Grammar(new GrammarBuilder(commands)); 42 43 // 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 44 re.RequestRecognizerUpdate(); // Pause Speech Recognition Engine before loading commands 45 re.LoadGrammarAsync(gr); 46 re.SetInputToDefaultAudioDevice();// As Name suggest input device builtin microphone or you can also connect earphone etc... 47 re.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(re_SpeechRecognized); 48 49 50 } 51 52 void re_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 53 { 54 switch(e.Result.Text) 55 { 56 ////For Led State ON 57 // For blue led 58 case "Blue On": 59 sendDataToArduino('B'); 60 break; 61 62 // For red led 63 case "Red On": 64 sendDataToArduino('R'); 65 break; 66 67 // For green led 68 case "Green On": 69 sendDataToArduino('G'); 70 break; 71 72 //For Led State OFF 73 // For blue led 74 case "Blue Off": 75 sendDataToArduino('Z'); 76 break; 77 78 // For red led 79 case "Red Off": 80 sendDataToArduino('X'); 81 break; 82 83 // For green led 84 case "Green Off": 85 sendDataToArduino('C'); 86 break; 87 88 //For turning ON all leds at once :) 89 case "All On": 90 sendDataToArduino('V'); 91 break; 92 93 //For turning OFF all leds at once :) 94 case "All Off": 95 sendDataToArduino('M'); 96 break; 97 //Program will talk back 98 case "Arduino Say Good Bye to makers": 99 ss.SpeakAsync("Good Bye Makers"); // speech synthesis object is used for this purpose 100 break; 101 102 // To Exit Program using Voice :) 103 case "Exit": 104 Application.Exit(); 105 break; 106 } 107 txtCommands.Text += e.Result.Text.ToString() + Environment.NewLine;// Whenever we command arduino text will append and shown in textbox 108 } 109 110 void sendDataToArduino(char character) 111 { 112 myPort.Open(); 113 myPort.Write(character.ToString()); 114 myPort.Close(); 115 } 116 117 private void btnStop_Click(object sender, EventArgs e) 118 { 119 re.RecognizeAsyncStop(); 120 //btnStart.Enabled = true; 121 btnStop.Enabled = false; 122 btnStart.Enabled = true; 123 } 124 125 private void btnStart_Click(object sender, EventArgs e) 126 { 127 128 re.RecognizeAsync(RecognizeMode.Multiple); 129 btnStop.Enabled = true; 130 btnStart.Enabled = false; 131 MessageBox.Show("Voice Recognition Started !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 132 } 133 } 134} 135
Arduino Source Code
c_cpp
Arduino Source Code for voice Recognition project
1/* This Program is about controlling leds states ON and OFF using voice recognition library of c# (using system.speech) 2 * Exciting Part is you dont need to have any external module to transmit data to arduino because you can easily 3 * use your builtin computer microphone or earphones microphone. 4 * 5 * 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. 6 * Keep Learning, Share, think and Repeat 7 * Enjoy ! 8 * 9 * By Jalal Mansoori 10 */ 11 12const int blueLed=10; 13const int redLed=9; 14const int greenLed=8; 15 16char incomingData='0'; 17 18void setup() { 19 // put your setup code here, to run once: 20//getting leds ready 21Serial.begin(9600); 22pinMode(blueLed, OUTPUT); 23pinMode(redLed, OUTPUT); 24pinMode(greenLed, OUTPUT); 25} 26 27void loop() { 28 // put your main code here, to run repeatedly: 29 incomingData=Serial.read(); 30 31 // Switch case for controlling led in our case we have only 3 Blue, Green and Red 32 switch(incomingData) 33 { 34 //These cases are only for state ON of led 35 // For blue led 36 case 'B': 37 digitalWrite(blueLed, HIGH); 38 break; 39 40 // For red led 41 case 'R': 42 digitalWrite(redLed, HIGH); 43 break; 44 45 // For green led 46 case 'G': 47 digitalWrite(greenLed, HIGH); 48 break; 49 50 //These cases are for state OFF of led and case name z , x, c are just randomly given you can also change but 51 // make sure you change it in a c# code as well. 52 // For blue led 53 case 'Z': 54 digitalWrite(blueLed, LOW); 55 break; 56 57 // For red led 58 case 'X': 59 digitalWrite(redLed, LOW); 60 break; 61 62 // For green led 63 case 'C': 64 digitalWrite(greenLed, LOW); 65 break; 66 67 //For turning ON all leds at once :) 68 case 'V': 69 digitalWrite(blueLed, HIGH); 70 digitalWrite(redLed, HIGH); 71 digitalWrite(greenLed, HIGH); 72 break; 73 74 //For turning OFF all leds at once :) 75 case 'M': 76 digitalWrite(blueLed, LOW); 77 digitalWrite(redLed, LOW); 78 digitalWrite(greenLed, LOW); 79 break; 80 81 } 82} 83
C# Code
csharp
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 :)
1/* 2 This Program is to connect c# and Arduino to transmit data from computer microphone to arduino board 3 By Jalal Mansoori 4*/ 5using System; 6using System.Windows.Forms; 7using System.IO.Ports; // This library is for connecting c# and Arduino to transmit and receive data through ports 8//Below are libraries for voice recognition 9using System.Speech; 10using System.Speech.Recognition; 11using System.Speech.Synthesis; 12 13namespace CsharpCode 14{ 15 public partial class Form1 : Form 16 { 17 //Creating objects 18 SerialPort myPort = new SerialPort(); 19 SpeechRecognitionEngine re = new SpeechRecognitionEngine(); 20 SpeechSynthesizer ss = new SpeechSynthesizer(); // When you want program to talk back to you 21 Choices commands = new Choices(); // This is an important class as name suggest we will store our commands in this object 22 23 24 public Form1() 25 { 26 InitializeComponent(); 27 //Details of Arduino board 28 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 29 myPort.BaudRate = 9600; // This Rate is Same as arduino Serial.begin(9600) bits per second 30 processing(); 31 32 } 33 34 // Defined Function processing where main instruction will be executed ! 35 void processing() 36 { 37 //First of all storing commands 38 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" }); 39 40 //Now we will create object of Grammer in which we will pass commands as parameter 41 Grammar gr = new Grammar(new GrammarBuilder(commands)); 42 43 // 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 44 re.RequestRecognizerUpdate(); // Pause Speech Recognition Engine before loading commands 45 re.LoadGrammarAsync(gr); 46 re.SetInputToDefaultAudioDevice();// As Name suggest input device builtin microphone or you can also connect earphone etc... 47 re.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(re_SpeechRecognized); 48 49 50 } 51 52 void re_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 53 { 54 switch(e.Result.Text) 55 { 56 ////For Led State ON 57 // For blue led 58 case "Blue On": 59 sendDataToArduino('B'); 60 break; 61 62 // For red led 63 case "Red On": 64 sendDataToArduino('R'); 65 break; 66 67 // For green led 68 case "Green On": 69 sendDataToArduino('G'); 70 break; 71 72 //For Led State OFF 73 // For blue led 74 case "Blue Off": 75 sendDataToArduino('Z'); 76 break; 77 78 // For red led 79 case "Red Off": 80 sendDataToArduino('X'); 81 break; 82 83 // For green led 84 case "Green Off": 85 sendDataToArduino('C'); 86 break; 87 88 //For turning ON all leds at once :) 89 case "All On": 90 sendDataToArduino('V'); 91 break; 92 93 //For turning OFF all leds at once :) 94 case "All Off": 95 sendDataToArduino('M'); 96 break; 97 //Program will talk back 98 case "Arduino Say Good Bye to makers": 99 ss.SpeakAsync("Good Bye Makers"); // speech synthesis object is used for this purpose 100 break; 101 102 // To Exit Program using Voice :) 103 case "Exit": 104 Application.Exit(); 105 break; 106 } 107 txtCommands.Text += e.Result.Text.ToString() + Environment.NewLine;// Whenever we command arduino text will append and shown in textbox 108 } 109 110 void sendDataToArduino(char character) 111 { 112 myPort.Open(); 113 myPort.Write(character.ToString()); 114 myPort.Close(); 115 } 116 117 private void btnStop_Click(object sender, EventArgs e) 118 { 119 re.RecognizeAsyncStop(); 120 //btnStart.Enabled = true; 121 btnStop.Enabled = false; 122 btnStart.Enabled = true; 123 } 124 125 private void btnStart_Click(object sender, EventArgs e) 126 { 127 128 re.RecognizeAsync(RecognizeMode.Multiple); 129 btnStop.Enabled = true; 130 btnStart.Enabled = false; 131 MessageBox.Show("Voice Recognition Started !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 132 } 133 } 134} 135
Downloadable files
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
Schematic
Comments
Only logged in users can leave comments