Hey guys. If you remember, a few months ago I made a video about this module.
This is a voice recognition module and is pretty decent. But you see, this module is
a bit limited. And the worst limitation is the command time. Because if you remember,
commands must be below 1.5 seconds with this module. 1.5 seconds is very short. You could
only use it for simple commands such as hello, do that, turn on, turn off, and stuff like that.
But for example, I was making tests with the command, “tell me a joke” and it didn’t work
all the time. Because the length of the command was more than 1.5 seconds. But today I have a way
better solution for you. And with this solution you can detect full sentences
such as this example here
And not just that, but this solution is able to detect any voice,
not just mine. This tool could be very powerful for robotics projects and it uses google to
detect the words. And the best of all, I’ve made it compatible with Arduino and UART communication.
So, you want to know how to do that and also make it work with the Arduino?
Well, stick till the end of the video. So let’s get started.
What’s up my friends, welcome back. The voice recognition module from elechouse costs around
20 dollars. But with 29 dollars you can buy a raspberry pi from AliExpress for example.
Now we all know raspberry pi is like a computer and you could even use Alexa voice recognition
with it if you want. But for me I want to keep it simple. Because I’m not that good
at programming a raspberry pi, especially for robotics. I’m way better at Arduino programming
and I use it all the time. That’s why I want a module that will detect my voice commands and
send the results to the Arduino using some kind of serial communication, UART for example. That’s
exactly how the voice module works but I want something better. So, to make a voice recognition
module with a raspberry pi we need to make several steps. First we add all the needed hardware.
Then we need to install a few packages such as speech recognition, py audio and a flac encoder.
The next step is to run a simple code using google voice API and detect the voice commands.
The final step is to get that command and send it through the UART port of the raspberry pi so we
also need to configure that, so let's begin. Get the raspberry pi and a micro SD card.
Go to raspberrypi.com and go to software and from here download the windows or MAC version.
Insert SD card to an adaptor and then to your computer.
Run the downloaded imager software. Select the raspberry pi OS,
for the storage select the SD card and click the write button. This process will take a while.
Place the SD card inside the PI. For the first setup you will need an HDMI screen,
keyboard and a mouse. So connect those to the USB and HDMI connectors.
Supply 5V to the PI and turn on everything.
You will have to complete the initial setup but that’s very easy so just follow the steps.
Now we have a working PI with the raspberry PI operating system.
Next step is to install the needed hardware. All we need is a USB sound card and a microphone.
For the sound card I will use this one that cost me only 3 dollars.
For the microphone you can use any microphone or even some headphones with an included microphone.
In my case I will use this one. Connect the sound card and check if is working.
You should see it on the audio devices. run the terminal and type alsamixer.
Then here press F6 and select the USB audio input. Lower the capture level a little bit. Also make
sure to be connected to the Internet since the google speech API package needs internet. Ok, so
go back to the terminal. Type this line and check for the version of python. If it is older than
3.7 we should update it using this line in the terminal but in my case I don't need to do that.
Next we install the speech recognition package. For that run this line,
pip install speech recognition. In a few seconds we have the package installed.
This package could recognise voice commands from sound files but not directly from the microphone
yet. For that we need to install another package called pyaudio. For that run this command.
Then we need one more step. This project will use google speech API and that platform needs
flac encoded format of the commands. So let’s install the flac encoder by running this line.
Now we have everything we need. I have this example code from github
and I will share below all the information and videos I have about this and thanks to
Audas YouTube channel for his video about this topic. Copy this code from github.
Now go to programming and run thonny which is preinstalled on the raspberry pi OS.
In the code as you can see we import the speech recognition module first.
Then we create the recognition variable. Inside of the while true,
we get the audio from the microphone. We equal the words variable to the response from Google.
Then we print the words so we can check the results. Run the code and let’s test it.
. So as you can see the results are very good.
I can say full sentences and it works great, way better than the other voice recognition module.
But you see, I’m not a good raspberry pi programmer, but I do love Arduino.
If you remember, I want to make my own automatic robot companion able to talk back to me. So I want
to use this system with Arduino. Raspberry Pi has a few UART ports.
We can output the results on that port and then use the Arduino to read them.
On GPIO 14 and 15 we have the TX and RX pins. But by default the UART port is disabled.
To enable it, on the terminal run this line. It will open this window. Go to interface options.
Here go to i6 Serial. Select no for the shell and on the next question select yes
for the serial port enable. The UART port is now enabled but we have to reboot the PI.
If you go to the terminal and run this line, at the bottom you should see that
the UART port is equal to 1 so is enabled. But if you try to use it at this point,
it might not work. At least in my case it didn’t. That’s because the PI has several ports.
In the terminal type this line and press enter. As you can see the serial 0 and 1 are mapped on
the tty S0 and AMA0. But my PI has an on board Bluetooth module that is using the same ports.
We need to swap these ports. Basically, we need to have the tty AMA0 for serial 0
and tty S0 for serial 1. For that run this line once again and open the configuration file.
Copy this line and go at the bottom of the file and paste it there. Save the file and exit.
Then reboot the PI. Once it starts up, check again the port mapping by running this line. As you can
see, the ports are swapped. Before we had this and now we have this. Now we can use the serial port.
We go back to the recognition code. We import the serial module.
We create the serial port with this line and here we have to place the tty AMA0 port.
This is the baud rate and let’s set it to 115 200 because that’s common with Arduino.
Now, each time we detect a new voice command, we use these lines to send the data on the UART port.
First we pass the text from String format to bytes. Then we use the write function to send
the data. That’s it. Let 's test it. Here I have my oscilloscope connected on GPIO 14 which is
the TX pin. I run the code and say something. As you can see, we get the serial data with
the text. Now let’s test it with Arduino. I
make a simple code that will read the data from the UART port and print it on a small screen.
This is the code where we get the data from serial and print it on the small screen.
I connect the PI to the Arduino serial port and run the example.
As you can see I get the text from my voice commands on the Arduino.
That’s great, just as I wanted. Now I can make my robot using way better voice commands.
The problem is that we need the pi to run by itself.
We need to make a simple script that will run this py file automatically when the raspberry pi boots.
On instructables you can find a guide on how to make that.
A problem that I still have is this error. You see, the microphone will be active each time
it hears sounds. If you talk, it will give you the voice commands. But each time it hears only noise,
the recognizer will give an error since it would not be able to recognise any command. I don’t know
how to set a noise threshold so maybe you guys could help me improve this. On the terminal,
if you type alsamixer and press enter, you can set the audio levels for your inputs. But that will
only improve the voice detection but it still gives us error when it hears any other noise.
For example if I start clapping, the code will not be able to detect a voice command
and give an error and stop the code. I don’t know how to solve this error. So guys that’s how you
can make a decent voice recognition module with a raspberry pi, a microphone and google voice API.
It also has a UART output just as the other module. So guys, I hope that you have learned
something new. If so, give me a like or comment below. Thanks again and see you later guys.