C#とArduino UNOで作るシンプル音声認識システム - 外部モジュール不要
部品と材料
| 部品 | 数量 | 備考 |
|---|---|---|
| Arduino UNO | 1 | |
| 抵抗 220Ω(任意の値で可、低すぎないこと) | 3 | LED電流制限用 |
| LED (青・赤・緑) | 各1 | 制御対象 |
| ブレッドボード | 1 | |
| ジャンパーワイヤー | 1セット | |
| マイク付きイヤホン | 1 | スマホ用でOK、PC内蔵マイクでも可 |
アプリとオンラインサービス
- Arduino IDE
- Microsoft Visual Studio 2015以降(.NET Framework対応版、Windows Formsアプリ作成用)
プロジェクト概要
注意: 更新版チュートリアル
このプロジェクトは2年前のものです。最新版(2020年更新)はこちらを推奨します:
https://medium.com/illumination/how-to-make-your-first-voice-project-using-arduino-956bfd494947
プロジェクト説明
人間には耳・舌・鼻などのセンサーがあり、双方向のデータ通信を行っています。同じ概念を機械に適用し、音声コマンドで電子部品を制御しようというのがこのプロジェクトです。初心者はまずLED点滅から始めますが、ここでは「音声でLEDをON/OFF」する仕組みを、外部音声認識モジュールなしで実現します。PCのマイクとC#の音声認識ライブラリを活用するため、非常にシンプルで低コストです。
なぜこのプロジェクトを作ったか
先生がC# Windows Formsで「音声で仮想電球を制御」するデモを見せてくれたのがきっかけです。それを「ハードウェア(LED)を物理的に制御する」形に発展させました。
なぜシンプルなのか
最大の理由は 外部音声認識モジュールが不要 な点です。PCの System.Speech ライブラリ(.NET標準)を使うため、ハードウェアはArduinoとLEDのみ。参考にした記事: C# Corner - Turning LED Off and On Through Voice Recognition
作り方
- 部品を準備
- ブレッドボード上に回路を組む(下記回路図参照)
- ArduinoをUSBでPCに接続
- Arduino IDEでスケッチを書き込み
- Visual StudioでC# Windows Formsアプリを作成・ビルド・実行
Visual Studioプロジェクトの準備
- Visual Studioをインストール(サンプルプロジェクト配布リンク も参照)
- プロジェクトで System.Speech 参照を追加:
- メニュー [Project] → [Add Reference] → [.NET] → 「System.Speech」を検索 → チェック → OK
- Arduino IDEのシリアルポート(例: COM5)と、C#コード内のポート名を一致させる
動作確認
- Arduinoスケッチをアップロード
- C#アプリを実行(緑の再生ボタン)
- [Start] ボタンをクリック → 「Voice Recognition Started!」ダイアログ
- マイクに向かって話しかける:
- "Blue On" / "Blue Off" - 青LED ON/OFF
- "Red On" / "Red Off" - 赤LED ON/OFF
- "Green On" / "Green Off" - 緑LED ON/OFF
- "All On" / "All Off" - 全LED一括制御
- "Arduino Say Good Bye to makers" - PCが「Good Bye Makers」と発話
- "Exit" - アプリ終了
- 認識したコマンドはテキストボックスに履歴表示されます
このプロジェクトから広がるアイデア
- リレーモジュールを追加して照明・家電を音声制御
- サーボモーターでカーテン開閉、ロボットアーム制御
- 温湿度センサーの値を音声で読み上げさせる(双方向化)
- 複数のArduinoをネットワーク接続し、家全体を音声制御
Learn, Create, Share and Repeat!
ソースコード
Arduino側スケッチ (C/C++)
/* Voice Recognition LED Control with C# System.Speech
* By Jalal Mansoori
* 外部モジュール不要、PCマイク+C#で音声認識、Arduinoはシリアル受信のみ
*/
const int blueLed = 10;
const int redLed = 9;
const int greenLed = 8;
char incomingData = '0';
void setup() {
Serial.begin(9600);
pinMode(blueLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}
void loop() {
incomingData = Serial.read();
switch (incomingData) {
// ONコマンド
case 'B': digitalWrite(blueLed, HIGH); break;
case 'R': digitalWrite(redLed, HIGH); break;
case 'G': digitalWrite(greenLed, HIGH); break;
// OFFコマンド(文字は任意、C#側と合わせる)
case 'Z': digitalWrite(blueLed, LOW); break;
case 'X': digitalWrite(redLed, LOW); break;
case 'C': digitalWrite(greenLed, LOW); break;
// 一括制御
case 'V': // All On
digitalWrite(blueLed, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, HIGH);
break;
case 'M': // All Off
digitalWrite(blueLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
break;
}
}
C#側コード (Windows Forms Application)
/* C# - Arduino Serial Communication + Voice Recognition
* By Jalal Mansoori
* 要: System.Speech 参照追加
*/
using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace CsharpCode {
public partial class Form1 : Form {
SerialPort myPort = new SerialPort();
SpeechRecognitionEngine re = new SpeechRecognitionEngine();
SpeechSynthesizer ss = new SpeechSynthesizer();
Choices commands = new Choices();
public Form1() {
InitializeComponent();
myPort.PortName = "COM5"; // ★自分の環境に合わせて変更
myPort.BaudRate = 9600;
processing();
}
void processing() {
// 認識コマンド登録
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"
});
Grammar gr = new Grammar(new GrammarBuilder(commands));
re.RequestRecognizerUpdate();
re.LoadGrammarAsync(gr);
re.SetInputToDefaultAudioDevice(); // 既定のマイク使用
re.SpeechRecognized += re_SpeechRecognized;
}
void re_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) {
switch (e.Result.Text) {
case "Blue On": sendDataToArduino('B'); break;
case "Red On": sendDataToArduino('R'); break;
case "Green On": sendDataToArduino('G'); break;
case "Blue Off": sendDataToArduino('Z'); break;
case "Red Off": sendDataToArduino('X'); break;
case "Green Off": sendDataToArduino('C'); break;
case "All On": sendDataToArduino('V'); break;
case "All Off": sendDataToArduino('M'); break;
case "Arduino Say Good Bye to makers":
ss.SpeakAsync("Good Bye Makers");
break;
case "Exit":
Application.Exit();
break;
}
txtCommands.Text += e.Result.Text + Environment.NewLine;
}
void sendDataToArduino(char character) {
myPort.Open();
myPort.Write(character.ToString());
myPort.Close();
}
private void btnStop_Click(object sender, EventArgs e) {
re.RecognizeAsyncStop();
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);
}
}
}
回路図
Fritzingファイル(schematic_ob2ZmiQwOV.fzz)を参照。接続手順:
- LEDと抵抗を直列にブレッドボードへ配置
- LEDアノード(+)をArduinoデジタルピンと同一列に接続:
- 緑LED → Digital 8
- 赤LED → Digital 9
- 青LED → Digital 10
- Arduino GNDをブレッドボードのマイナスレールに接続し、全LEDカソード(-)をそこに落とす
※ このプロジェクトは「外部音声認識モジュールなし」でPCの音声認識エンジンを活用する点が最大の特徴です。より高度な用途には、専用モジュール(例: Voice Recognition Module V3)やESP32+マイク、またはクラウド音声APIとの連携も検討してください。
製造プロセス
- MATRIX Voice搭載Raspberry PiへのGoogle Assistantインストール:ステップバイステップガイド
- Raspberry Piでリアルタイム顔認識:エンドツーエンドガイド
- Windows IoTとRaspberry Piで作るセキュアな顔認証ドアロック
- スマートペン:Raspberry PiとIMUで手書きと動きをリアルタイム追跡
- 自作LUMAZOID:Arduino音楽ビジュアライザーDIYガイド
- Arduinoで作るモニター用Ambilightシステム:没入感アップの自作ガイド
- Arduino Nanoで37LEDルーレットゲームを自作:ヨーロピアンスタイル完全再現
- Arduinoで超音波レベーション(浮遊)装置を自作:ステップバイステップガイド
- 「Go Away Robot」を作ろう:子供向け簡単Arduinoスターターキット
- アナログ風LED POVクロック – Arduino Nanoで作る残像時計
- シングルLEDライトペインティング:自作モーター駆動システムで動的な画像を描く