Voice Activated Robo Car on Microcontroller with TinyML

Using voice recognition with the in-built microphone, Wio Terminal (Voice Activated Robo Car) will be able to recognize the go, stop and background noise by TinyML.
Voice Activated Robo Car on Microcontroller with TinyML

Story of Voice Activated Robo Car

The microphone on Wio Terminal will only able to detect the sound intensity (check the schematic below orĀ download to view full schematic) and hence, it will be challenging for it to do speech recognition for different words. Hence, I thought of some creative ways to capture the sound input and then train them with the Edge Impulse engine to see if it is able to do simple speech recognition. We will see the result at the end later.

Wio Terminal Mic Scematic

Curious how itā€™s possible? Check out the detailed steps below to make one yourself!

In this detailed tutorial, we will cover the following:

  • Whatā€™s UART Serial Communication?
  • UART Serial Communication between Wio Terminal and uKit Explore
  • 1.0 Train an Embedded Machine Learning Model Using Codecraft
  • 2.0 Arduino Text Code Modification
  • 3.0 Program the Robo Car (uKit Explore ā€“ Arduino Mega 2560 based)
  • 4.0 Expected Result
  • Giveaways

Whatā€™s UART Serial Communication?

TheĀ UART, or universal asynchronous receiver-transmitter, is one of the most used device-to-device communication protocols. This article shows how to use a UART as a hardware communication protocol by following the standard procedure.

When properly configured, the UART can work with many different types of serial protocols that involve transmitting and receiving serial data. In serial communication, data is transferred bit by bit using a single line or wire.

In layman term, UART allows an embedded device such as Arduino to send data over to another Arduino via the TX (transmit) and RX (receive) line as shown below.

TX (transmit) and RX (receive) line

Source:Ā https://learn.sparkfun.com/tutorials/serial-communication/all

I often use UART as the communication protocols between embedded devices. Let me give you an example case study. Well, Arduino UNO has no built in WiFi and hence itā€™s not possible to do IOT related projects. With the understanding of the basic UART serial communication, I was able to leverage on the ESP8266/ESP32 as the co-processor to the Arduino UNO so that the data collected from the sensors attached to Arduino UNO will be sent over to ESP8266/ESP32 to send over to the cloud platform such as web server, Blynk orĀ FAVORIOT.

With the ability to exchange data between two embedded devices (example Arduino UNO and ESP8266/ESP32), the possibility is unlimited!

Example as below where I am trying to establish a simple serial communication between TTGO T-Display (Serial2 line) & uKit Explore (Serial line).

simple serial communication between TTGO T-Display (Serial2 line) & uKit Explore

 

We will see that how this concept being applied in my voice activated Robo Car project.

UART Serial Communication between Wio Terminal and uKit Explore for Voice Activated Robo Car

As we check the Wiki page for Wio Terminal, thereā€™sĀ TX/ RX pin available on the pin 8 and 10.

UART Serial Communication

Whereas for uKit Explore, as it has the headers like in Arduino UNO pinout, thereā€™s also TX/RX pin available on the pin D0 and D1. You can check the full uKit Explore pinout here:Ā https://ubtechedu.gitbook.io/ukit-explore/v/english/ukit-explore-quick-start/pinout

uKit Explore pinout

1.0 Train an Embedded Machine Learning Model Using Codecraft

In this first section, our goal is to create an embedded machine learning model (voice recognition) using Codecraft platform

1.1.0Create and select models

Go toĀ https://ide.tinkergen.com/.Ā SelectĀ ā€œ(for TinyML) Wio Terminalā€.

Wio Terminal

1.1.1 Create the ā€œWake-Up Words Recognition (built-in microphone)ā€ model

Click onĀ ā€œModel Creationā€Ā on the embedded machine learning box on the middle left. Then select the ā€œWake-Up Words Recognition (built-in microphone)ā€ as shown below.

Enter the name for the model according to the requirements.

Wake-Up Words Recognition (built-in microphone)

ClickĀ OkĀ and the window will automatically switch to the ā€œData Acquisitionā€ interface.

Data Acquisition

1.2.0 Data Acquisition (on-board)

1.2.1 Default Labels

There are 3 default labels (hi wio, background and other words) that are automatically created for you.

Default Labels

You can use it without any changes unless you want to have different names for your labels. For my case, I changed two of the default labels as below:

  • hi wioĀ changedtoĀ go
  • other wordsĀ changed toĀ stop

1.2.2 Collecting data from modified labels and Data Acquisition Program Modification

Click on the ā€œhi wioā€ label, you will be prompted to change the label name. I renamed the label toĀ goĀ and click ā€œOkā€

change the label name

Do the same for the ā€œother wordsā€ label. You will now see your labels as shown below:

"other words" label

IMPORTANT:Ā Now, you have to remember to change the labels on theĀ default data acquisition programĀ to reflect the correct modified labels:

default data acquisition program

default data acquisition program

1.2.3 Connect Wio Terminal and Upload Data Acquisition Program

Connect Wio Terminal to laptop using the USB-C Cable. Click on the Upload button, and this action will upload the default data acquisition program. Typically, it takes around 10 seconds to upload. Once successfully uploaded, a pop-up window will appear to indicate ā€œUpload successfullyā€.

Click ā€œRogerā€ to close the window and return to the data acquisition interface.

Caution

Note:Ā You need to download ā€œCodecraft Assistantā€ to be able to Connect and upload code on Codecraft online IDE.

Caution: For the web version of Codecraft, if you donā€™t install or run the Device Assistant, you may get the message in the image below that you havenā€™t opened the Device Assistant yet. In this case you can check this page for further information:Ā Download, installation and ā€œDevice Assistanceā€ Usage.

need to download ā€œCodecraft Assistantā€

1.2.4 Data Acquisition

In the upper right hyperlink, you will find a step-by-step introduction to data acquisition.

Follow the instructions to collect data accordingly to your modified labels.

Pay attention on below:

  • Wio Terminal button location (A, B, C)
  • Animated gif has been accelerated; the actual action can slightly slow down.
  • Please notice the red tips.
  • Point the curser over Description Texts for more detailed content

After collecting the sample data for the 3 labels, now, the data acquisition step is completed. I collected 15 seconds for each of the labels.

1.2.4 Data Acquisition

1.3: Training and Deployment

Click on ā€œTraining & Deploymentā€, and you will be seeing the model training interface as shown below.

1.3: Training and Deployment

Waveforms for go, background and stop raw data are as shown below for reference. (can be viewed from the ā€œSample dataā€ tab.

go

If you observe the waveform, for label ā€œgoā€œ, my sound input is ā€œgo, go, go!ā€ In 5 seconds window, I recorded twice with about 1 seconds rest.

waveform for label "go"
waveform for label ā€œgoā€

background

For label ā€œbackgroundā€, I will just hold the Wio Terminal on my hand to capture the surrounding noise inputs. Hence, you could see the sound input level is not 0 as thereā€™s some surrounding noise such as fan rotating sound level.

waveform for label "background"
waveform for label ā€œbackgroundā€

stop

For label ā€œstopā€, to differentiate it with ā€œgoā€, I try on my sound input as ā€œstopppppā€¦ā€¦ā€¦ā€ for about 3 seconds.

waveform for label "stop"
waveform for label ā€œstopā€

1.3.1 Select neural network and parameters

Select the suitable neural network size:Ā small,Ā mediumĀ andĀ large

Set parameters:

  • number of training cycles (positive integer),
  • learning rate (number from 0 to 1)
  • minimum confidence rating (number from 0 to 1)

The interface provides default parameter values of training cycles ofĀ 50, however, theĀ accuracy was not very good. Hence I changed the training cycles toĀ 100.

Select neural network and parameters

1.3.2 Start training the model

Click ā€œStart trainingā€. When you click ā€œStart trainingā€, the windows will display ā€œLoading..ā€! Wait for the training to be done!

The duration of ā€œLoading..ā€ varies depending on the size of the selected neural network (small, medium and large) and the number of training cycles. The larger the network size is and the greater number of training cycles are, the longer it will take.

You can also infer the waiting time by observing the ā€œLogā€. In the figure below, ā€œEpoch: 68/500ā€ indicates the total number of training rounds is 68 out of total of 500 rounds.

Start training the model

After loading, you can see ā€œTrainModel Job Completedā€ in the ā€œLogā€, and ā€œModel Training Reportā€ tab will be appeared on the interface.

TrainModel Job Completed

1.3.3 Observe the model performance to select the ideal model

In the ā€œModel Training Reportā€ window, you can observe the training result including theĀ accuracy, loss and performanceĀ of the model.

If the training result is not satisfactory, you can go back to the first step of training the model by selecting another size of the neural network or adjust the parameters and train it until you get a model with satisfactory results. If changing the configurations do not work, you may want to go back to collect the data again.

Model Training Report

For my case, I am able to achieve 100% accuracy with modification on the training cycles toĀ 100.

1.3.4 Deploy the ideal model

In the ā€œModel Training Reportā€ window, click on ā€œModel Deploymentā€

Model Deployment

Once the deployment is completed, click ā€œOkā€ to go the ā€œProgrammingā€ windows which is the last step before we deploy the model to the Wio Terminal.

 deployment is completed

1.4. Programming & Model Usage

Alright, so now we done trained the model and the fun part of integrating Artificial Intelligence (Machine Learning in this case) with robotics (a Robo Car) using the UART communication protocol.

This is the sample program that created from the block programming interface:

Programming & Model Usage

Let me try to explain what I am trying to achieve with this code. I make it into bullet points for easy understanding:

  • As previously, we use the if-else conditional statements to evaluate the confidence of the labels.
  • If the confidence of ā€œgoā€ is greater than 0.8 (80%), I will print ā€œ1ā€ on the serial terminal.
  • If the confidence of ā€œstopā€ is greater than 0.8 (80%), I will print ā€œ2ā€ on the serial terminal.
  • Otherwise, if the confidence of ā€œbackgroundā€ is greater than 0.8 (80%), I will print ā€œ0ā€ on the serial terminal.

Ok, so for now, just remember 3 different conditions:

ā€œgoā€ > 0.8, the command is ā€˜1ā€™

ā€œstopā€ > 0.8, the command is ā€˜2ā€™

ā€œbackgroundā€. the command is ā€˜0ā€™

After some research online for the pinout of TX/RX for Wio Terminal, hereā€™s my findings:

  • We have theĀ TX/ RX pin available on the pin 8 and 10Ā that can be used to connect to another board (uKit Explore in this case).

TX by RX for Wio Terminal

  • The serial line you can access from the 40 pin header isĀ Serial1Ā instead of usual Serial which basically shows the output via Serial Terminal.

Alright, so take note on theĀ 2 important findingsĀ as above as it will be the critical part for our project!

If we have a look at the text coding for the corresponding blocks code, you will notice that theĀ Serial.printĀ is NOT using the Serial1 line. Hence, this leads us to our second step which is to continue our coding on Arduino IDE for customization.

Serial.print

 Serial.print
Serial.print

2.0 Wio Terminal Arduino Text Code Modification

In this first section, our goal is to modify the Arduino text code to useĀ Serial1.printlnĀ instead ofĀ Serial.printlnĀ so that the data can be transmitted via theĀ TX/ RX pin available on the pin 8 and 10.

2.1 Toggle to the Text Code area & Copy The Text Code

Wio Terminal Arduino Text Code Modification

On the text code area, copy all the code by pressing onĀ CTRL + AĀ to select all the code.

Open up Arduino IDE, create a new file, paste the code into the empty sketch by pressing onĀ CTRL+V. Proceed to save the sketch with the desired name.

paste the code into the empty sketch by pressing on CTRL+V

2.2 Copy the Edge Impulse TinyML Arduino Library

So,Ā the question now is, how do I get all files containing the data for the trained model on Codecraft platform onto Arduino IDE?

Well, itā€™s easy. Follow the steps below:

  • Navigate toĀ C:\Users\<User_Name>\AppData\Local\Programs\cc-assistant\resources\compilers\Arduino\contents\libraries
  • Find the folder name that has the same number with the Edge Impulse header file on top of your Arduino text code (in my case it will beĀ 47606)folder name that has the same number with the Edge Impulse header file on top of your Arduino text code
  • CopyĀ the entire ei-project_47606 folder and paste it ontoĀ C:\Users\<User_Name>\Documents\Arduino\libraries\

Folder Libraries

2.3 Modification on theĀ Serial.printlnĀ function

Lastly,Ā letā€™s look back at the code, scroll down to the void loop section andĀ modify theĀ Serial.printlnĀ function toĀ Serial1.printlnĀ instead.

modify the Serial.println

2.4 Upload the Code

Finally, we are ready to upload the code.

Make sure you have installed the Wio Terminal board support package. If not, please refer to the ā€œGet Started with Wio Terminalā€ guide on Seeedā€™s Wiki.

Make sure you selected theĀ correct boardĀ andĀ COM portĀ before you upload the code to Wio Terminal.

Upload the Code

Alright, we have done the necessary modifications! Right now, the below condition still true for each cases and this time the command ā€˜1ā€™, ā€˜2ā€™, and ā€˜0ā€™ will be able to send out from theĀ TX/ RX pin available on the pin 8 and 10.

ā€œgoā€ > 0.8, the command is ā€˜1ā€™

ā€œstopā€ > 0.8, the command is ā€˜2ā€™

ā€œbackgroundā€. the command is ā€˜0ā€™

Source: Voice Activated Robo Car on Microcontroller with TinyML

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.