The Google AIY Voice Kit is a package that consists of a custom Raspberry Pi HAT (Hardware Attached on Top) board, a stereo microphone board, a push button switch with integrated LED, a small speaker and an assortment of cables and hardware to attach everything to a Raspberry Pi 3.  Everything fits together in a cute little cardboard box.

For instructions on putting the AIY Voice Kit together please see the first article and video in this series “Hacking the Google AIY Voice Kit – Part 1 – Putting it Together”.

In addition to the Raspberry Pi 3 you’ll also need to supply a micro-SD card to hold the Raspbian operating system and a USB power supply to provide power for your creation.  The custom version of Raspbian can be downloaded from the Google AIY website.

Once you have it all assembled you can run a couple of Python programs supplied by Google to turn your cardboard box into what is essentially a version of the Google Home Mini.  Talking to a cardboard box was never as fun as this!

But the real power of the AIY Voice Kit lies in its ability to integrate with the Raspberry Pi and control devices attached to your microcomputer.  Using the Google AIY Voice Kit you can construct custom projects with a powerful voice interface

In this article I’ll show you how I took my AIY Voice Kit out of the box and used it to build a (somewhat silly) LED-based “Traffic Light” and control the speed of a DC Motor.  I’ll also show you how to setup the Google Cloud Speech API and how to modify some of the sample Python code to control your projects by simply speaking to them

So let’s get started!

The Google AIY Voice Kit

As we discussed in the original article the Google AIY Voice Kit is the first in Googles AIY series of projects. The term “AIY” is a “sort of” acronym for “Artificial Intelligence Do It Yourself”, an initiative of Google’s to provide low cost development environments for experimenting with cutting edge artificial intelligence technologies.

Hacking the Google AYI Voice Kit Part 2

Google has also released the AIY Vision Kit which uses a Raspberry Pi Zero and a Pi Camera to experiment with computer vision. As soon as I can get my hands on one (as of this writing they are very hard to obtain) I’ll definitely get working with that as well.

With the Voice Kit Google has made it possible for experimenters to easily add speech recognition and voice feedback to their projects.

Of course Google is not the only company that has opened up their voice control technology to experimenters. Amazon has also done this with their Alexa API which they provide to allow you to work with their Alexa voice assistant.

It seems like in the future we’ll be doing a lot of talking to out little electronics projects!

New AIY Raspbian Build

If you followed along with the first article and video there is something you need to be aware of before you embark upon this latest voice control adventure.

The original article (and video) were made in December 2017 and for them I used what was the latest AIY Raspbian build. Since then Google has released a new build of AIY Raspbian and it has some significant changes. This article and video use the latest build (January 2018)  so if you are still on the hold build you need to be aware of the changes. I’ll go over them now.

The principle change with the new AIY Raspbian build is that it is not just designed for the AIY Voice Kit, it also has code for the AIY Vision Kit.  Because of this Google has changed the file structure for the sample code and demo files, so if you attempt to run the commands that worked perfectly in the older build you’ll be greeted with a “file not found” error.

In the old build the sample code was all in the home/pi/AIY-voice-kit-python/src/ directory.  As the Dev Terminal (the development command-line terminal on the AIY Raspbian Desktop) opens up in the home/pi/AIY-voice-kit-python/ directory you only needed to type src/ plus the name of the python file you wanted to run. For example the Assistant Library Demo was run with the command src/ .

In the new build the some code is now held in the home/pi/AIY-voice-kit-python/src/examples/voice/ directory,. As the Dev Terminal still opens up in the home/pi/AIY-voice-kit-python/ directory you now need to type src/examples/voice/ before the file name. So the Assistant Library Demo is now run by typing  src/examples/voice/ at the command line.

Updating the AIY Raspbian Build

Because of the file structure change you can’t just type sudo apt-get update and sudo apt-get upgrade in a command line to update your build. You actually need to install a brand new build by downloading the image from the Google AIY website and using it to burn a new micro-SD card.

Before you install your new micro-SD card you’ll need to get a copy of the assistant.json file that you created in the original build (this is the file that you downloaded from the Google Cloud Console and renamed). You’ll find that in the root of your home directory, which is the “pi” folder.

Copy that file to a safe place before you do your upgrade. I used a file server on my local area network but an easier method might be to simply use a USB thumb drive that you plug into one of the Raspberry Pi 3 USB ports.  However you do it make sure you save the file in a safe place, as you’ll need to reinstall it on the new build.

If you created any other custom files on the original micro-SD card you’ll want to copy those as well.

Now burn the image to a new micro-SD card – I’d advise not overwriting the old one just in case something goes wrong, it’s nice to have a backup.

Remove the old micro-sd card from your Raspberry Pi and install the new one. Power it up.

After it powers up and you arrive at the Raspberry Pi desktop copy the assistant.json file into the “pi” directory.  You’ll also want to setup your WiFi connection and set the correct locale for your keyboard, time zone and wireless network.  You can do that from the Preferences application, a reboot is required to make it all take effect.

Now that you’ve brought your software up to the latest version it’s time to begin controlling the world (or at least your little piece of it) with your voice.

AIY Voice HAT for Raspberry Pi

The main hardware component in the Google AIY Voice Kit is the AIY Voice HAT. Like any other HAT this is a board that attaches to the Raspberry Pi via the 40-pin GPIO.

The HAT is where you’ll be attaching external devices that you can then control with your voice.  As such you may find it easier to work with if you remove the whole Raspberry Pi and HAT combination from the confines of it’s cardboard prison.  It will make the wiring a lot easier.

In my case I went to extremes here. Not only did I remove the Raspberry Pi, HAT, microphone board and push button from the cardboard box, I also mounted everything onto a custom-built experimenters board of my own design.  I piggybacked the HAT with a “Pi Cobbler”, a device that lets you bring out all 40 pins of the GPIO to a solderless breadboard. This will provide me with a lot of flexibility in designing custom voice-controlled projects, as well as with experimenting with the Raspberry Pi in general.

Raspberry Pi Experimenters Platform

Now I certainly don’t expect that everyone will follow my lead and build a custom experimenters platform, but it is a good idea to at least remove the Raspberry Pi and the HAT from the cardboard box so it is easier to work with it. You can always put it back into its cardboard home when you are done!

HAT Functionality

As the HAT is the main hardware component in the AIY Voice Kit we should take a closer look at it.

In many respects the AIY Voice HAT is really a fancy sound card. It takes input from the microphone board and digitizes it to send up to the Google Cloud. It does NOT recognize or interpret the voice by itself.

When the response is returned by Google the AIY Voice HAT takes that response and converts it into sound. Its built-in audio amplifier is used to drive the speaker.  The AIY Voice HAT is actually capable of stereo output but by default it just drives the left speaker in monophonic mode.

The AIY Voice HAT also has connections to the Raspberry Pi GPIO exposed on a number of pins for convenience in attaching external devices. Let’s look at these pins now.

HAT Connections

In addition to the 40-pin Raspberry Pi GPIO connector the Google AIY Voice HAT has the following connections:

AIY Voice HAT Connections


  • Microphone – A 5-pin connector to attach the stereo microphone board.
  • Pushbutton – A 4-pin connector for the pushbutton with integrated LED.
  • Driver Outputs (4) – Each of these is a 3-pin connection. One pin goes to ground, one goes directly to a GPIO pin and the other one is the same GPIO output with a driver transistor for handling up to 500 ma of current.
  • Mono/Stereo Jumper – Shorting this will enable the Right audio channel for Stereo output.
  • 2nd Speaker – A connection for the Right speaker when operating in Stereo mode.
  • External Power Supply – Connections for an external power supply, used when driving high-current devices that would be too taxing on the Raspberry Pi 5-volt power supply.
  • External Power Jumper – This pad needs to be cut (broken) to use the External Power supply.
  • Servo Motors (6) – Each of these 3-pin connections can be used to drive a servo motor. They can also be used to drive LEDs and other low current devices.
  • I2C Bus – A connection to the Raspberry Pi I2C bus for attaching external I2C devices.
  • Serial RX & TX – A connection to the Raspberry Pi serial receive and transmit lines.
  • SPI Bus – A connection to the Raspberry Pi SPI bus for connecting SPI peripherals.


The AIY Voice HAT does not come with pins soldered for these connections, instead they are just empty holes on the printed circuit board. You can solder pins into the holes as you require them, solder in all of them (which is what I did) or just solder wires directly into them.

AIY Voice HAT Pins Top View

You’ll notice form this image that I also soldered a 2.1 mm barrel connector into the External Power Supply connections, the board is laid out to accept this type of connector. This is the same type of power connector used on most Arduino boards.

I found that the holes on the AIY Voice HAT board were a bit too small for my barrel connector to fit flush with the board so I “squeezed” the pins a bit to make them fit! This resulted in the connector being mounted a bit elevated from the board. A quick test with a multimeter confirmed that the connections were sound.

AIY Power Connector Side View

The above image is a side view of the power connector.

Now that I had my pins soldered I was ready to connect things to my Google AIY Voice Kit.

Alternatives to the AIY Voice HAT

Some people have found it difficult to obtain a Google AIY Voice Kit, although the availability seems to be improving. I had listed four sources for the kit in the original article, there are more now (I noticed that DigiKey now seems to stock them for example) .

If you can’t find an AIY Voice Kit and really don’t need the cardboard box there are other alternatives.

One of them is the ReSpeaker series of microphone HATs made by Seeed Studio. There are a number of different models of these, some of them retailing for under 20 US dollars.  These HAT’s include multiple microphones, connections for both speakers and headphones and also connections to some of the Raspberry Pi GPIO ports.

These boards can be used with both the Google Cloud Speech API and the Amazon Alexa Voice Services API. Many of them are of a smaller form factor (sometimes called a P-HAT) that are ideal for use with the lower powered Raspberry Pi Zero.

So if you can’t get a hold of a genuine Google AIY Voice Kit you can still experiment with voice control using one of these alternative HATs.

Google Cloud Speech API

In order to build your own voice controlled projects you’ll need to write code to interface with the Google Cloud Speech API.  This API is the heart of all of the Google voice services that we know and love (at least I love them, I don’t know about you).

The Google Cloud Speech API converts spoken audio into text. It can “speak” 110 languages and variants so it is by no means limited to English, or even to American English.

Google has provided this API to developers around the world so that they can integrate it into their projects and designs.  However, before you nominate the Google design team for sainthood for providing this wonderful gift to the world you should be aware of something – the Google Cloud Speech API is not free.

In order to use the Cloud Speech API you’ll need to pay for the services. Don’t panic, the experiments you are about to perform won’t cost you a penny. But you will need to setup a billing account, and we will look at doing that now.

Setting up a Billing Account

In order to make use of the Google Cloud Speech API you’ll need to have a billing account so that Google can be paid for their services. It is very unlikely that you will ever be charged however as Google provides a very generous free allotment of Cloud services (Speech and other services).

You’ll get a credit for 300 US dollars worth of Cloud Services when you setup your billing account. This free allotment is valid for one year from the date you setup your account, parceled out as 60 minutes of use per month.  If you about to exceed the free amount Google will warn you so that you don’t have any surprises on your next credit card statement.

Performing all of the experiments in this article and video will likely use up about a minute of your allotment.  You can monitor your usage through the Google Cloud Console.

If you reside in Europe you should be aware that you’ll probably need to set up a Business billing account instead of a personal one. The Cloud Speech API is only available to business accounts in certain European countries. You can check with Google for more information if this applies to you.

To setup a billing account go to the Google Cloud Platform Console at . If you followed along with the first article and video you will already have an account here, otherwise set one up using your Google (i.e. GMail) credentials.

Open the left-side menu by clicking the menu icon in the top left corner. Select the Billing item in the menu.

Click the prompt to Manage Billing Accounts. You’ll then get to a screen where you have an option to setup a new billing account.

The payment options for the billing account will depend upon which country you reside in. Unfortunately PayPal is not an option, only credit and (some) debit cards are valid payment options.

Enter your payment method information as required. You’ll need to agree to Google’s license agreement and will also have the option to subscribe to a Cloud Services newsletter.

Once you have set up billing successfully you are ready to setup the Cloud Speech API to use with your Google AIY Voice Kit.

Google Cloud Speech API Setup

There are several steps required to setup the Google Cloud Speech API.  You’ll need to attach the billing account you created to the project you are using (the same project you setup in the original article and video) , you will create a Service Account and download a JSON file with it’s credentials and you’ll need to enable the Cloud Speech API itself.

Here is a step-by-step breakdown of what you need to do. You can also follow along in the video associated with this article.

  1. Login to Google Cloud Platform on Pi –
  2. Select Project you created in first article/video
    1. Go to drop-down at top (if more than one project)
    2. Select required project from list
  3. Select Billing (left-side menu)
  4. Menu asks to “Link a Billing Account” or “Manage Billing Accounts”
    1. If you have already created an account you want to use select “Link a Billing account” and select the account from the list
    2. Otherwise select “Manage Billing Accounts” and select “Create a Billing Account” to create a new account.  Agree to the Terms of Service You will need a valid payment method such as a credit card. You are provided with $300 USD of free credits and will be informed if you are about to exceed them
  5. Go to APIs and Services (on left side menu)
  6. Select Credentials from sub-menu
  7. Click Create Credentials
  8. Select Service Account Key from drop-down menu
  9. Select New Service Account from “Select” drop-down list (only item available)
  10. Give account a name (i.e. My_Pi_AIY_Services)
  11. Select Project under “Role”
  12. Leave JSON radio button selected
  13. Click “Create”. A download dialog box will display
  14. Download file to default location on Pi (/home/Pi/Downloads)
  15. After download a confirmation box will display. Close the box
  16. Open File explorer. Navigate to Downloads folder
  17. Rename (right-click on file and choose “Rename”) to “cloud_speech.json (case-sensitive, all lowercase)
  18. Copy or move file to Home directory (/home/pi). This is the same folder as the “assistant.json” file made in last article/video
  19. Return to Menu on left side
  20. Select APIs and Services
  21. Select Library sub-menu. API Library page will open
  22. Search for “Speech API”. Select “Google Cloud Speech API”
  23. Enable the API. Select Enable Billing when prompted and then click Enable to enable the Speech API
  24. This completes the Speech API setup on the Google Cloud Platform

You are now ready to use the Google Cloud Speech API. The next step is to test everything to be sure that it’s working correctly.

Testing the Cloud Speech API

Minimize the web browser and search the AIY Raspbian desktop for an icon that says “Check Cloud”. Click on that icon.

If everything is working well a terminal window will open up and some text saying that the connection to the Google Cloud is being checked.  That should be followed by a message saying that the connection to the cloud was successful.

If this works give yourself a pat on the back – you’ve got the most difficult part of the job finished!

On the off chance that the test fails make sure your Internet connection is good. Keep in mind that while the Google AIY Voice Kit instructions specify WiFi as a connection method everything will also work with an Ethernet cable plugged into the Raspberry Pi. In some cases this may be a more reliable method of connecting to the Internet.

Assuming you have successfully connected to the cloud you can proceed with the following demonstrations, which actually use the Cloud Speech API.

Cloud Speech Demo

The first demonstration that you should try is the Cloud Speech Demo. This script will test all of the essential voice recognition functions, it will capture and interpret your commands and use them to control the LED embedded inside the push button switch.

Here is the Cloud Speech Demo code, which like all of these examples is written in Python.


Run the script by opening the Dev Terminal and then typing the following at the command prompt: /src/examples/voice/ .

After the script loads you will be prompted to push the button and give a command. The following commands are recognized by the demo:

  • Turn on the light. This turns on the light inside the push button. There is also an LED on the Voice HAT that will turn on as well.
  • Turn off the light. As you might expect this will turn off the light!
  • Blink. This will blink both the push button light and the onboard LED.
  • Goodbye. This will end the script and return the Dev Terminal to the command prompt.

You’ll notice that this script does not require you to issue an “OK Google” prompt before each instruction. It also won’t accept any commands other than the ones listed above.

The reason that “OK Google is not required is that this script uses the push button as a “trigger”.  If you wish you could replace the push button with a relay or another type of momentary contact switch (say a pressure switch on a doormat for example).  The trigger prompts the device to go into listen mode where it waits for a command it understands and then takes the required action.

Speech Triggers

All voice activation systems require a trigger before they will start listening for commands, they don’t process voice until the trigger is received. This Is true of systems like the Amazon Alexa, Apple Sir and Microsoft Cortana as well.

In most common applications the trigger is a preset phrase or “Wake Word”, such as “Alexa”. “Siri” or “OK Google”. This is the way most of us are used to interacting with voice control systems.

The previous example used a press of the push button as its trigger. It was just a demonstration of course as in “real life” it would not really be all that useful.  After all if we really wanted to turn on the light and we have a push button we could just wire it directly to the LED and eliminate the voice recognition (and the Raspberry Pi and the Internet and the Google Cloud) altogether!  It’s an excellent example of a design overkill.

Now there are some applications where using a switch or other hardware device such like a motion sensor as a trigger would make sense. You could have the voice system respond to your commands when you enter a room, in this case “turn on the light” would be a very useful command and not having to precede each command with a wake word would be convenient.

But in most cases we will want to trigger our voice controlled device using a wake word.  So in the next example we will do exactly that.

Assistant Library with Local Commands

The next example you’ll want to try is the Assistant Library with Local Commands script. This python script will respond to the OK Google wake word trigger instead of requiring you to press the push button.

This python script, which is reproduced here for your convenience, is an important script to understand as it can be used as the basis for writing your own custom scripts that work on the “OK Google” wake word.


You can run the script from the Dev Terminal by typing /src/examples/voice/ .

The script has three commands that it will accept, all of them activated by preceding them with the “OK Google” wake word:

  • Power Off. This will actually shut down and power off the Raspberry Pi.
  • Reboot. Yes this will reboot the Raspberry Pi.
  • IP Address. This will read back the local IP address of your Raspberry Pi.

You can also say “OK Google” and give it a “normal” question, such as “OK Google, what is the capital city of France?”.  It will respond with the “regular” Google voice, informing you that the answer is Paris.  

Custom commands will not use the standard “Lady Google” voice but instead use a voice synthesized tone that reminds me of many 1990’s speech synthesizers.

To understand the script you really only need to understand two concepts.

  1. For every defined action there is a custom function.
  2. Each function is called by a “if” or “elif” statement that evaluates the text that the API returns when you speak.

For example, let’s look at the “power off” command.

You’ll see a custom function called “power_off_pi()” beginning on line 43. It does two things:

  1. It uses an “” command to speak to you. In this case it says “Good bye”.
  2. It performs the desired action. In this case it issues a “sudo shutdown now” command in the shell to shutdown the Raspberry Pi.

You will also notice an “if” statement that evaluates a variable called “text”. The “text” variable is assigned to the response text you get from the Google Cloud Speech API, this text is the interpretation of the voice it decoded. You’ll see that starting on line 68, on line 71 it runs an “if” statement that is satisfied when the text equals “power off”. If then does two things:

  1. It runs an “assistant.stop_conversation()” command. This stops the unit from listening for additional voice commands while it processes the current one.
  2. It calls the function we defined earlier, in this case “power_off_pi()”.

Once you get the grasp of that it’s actually pretty simple. And you should realize that you can start adding your own commands by (a) defining a function to execute the command and (b) adding the text to interpret as another “elif” statement evaluation.

Let’s do exactly that and write some code to control our own devices.

Controlling LEDs with Voice

Our first example will be controlling three LEDs using voice commands. Now while you’ll find an example to control LEDs on the Google AIY Voice website this one differs in that we will use the “OK Google” wake word as opposed to pushing a button.

To do this we will just modify the Assistant Library with Local Commands script that we dissected earlier.

Note that the Google code samples are all licensed under the Apache License Version 2.0.  The license terms state that you can modify and reuse the code to your heart’s intent but you need to leave the original license agreement intact plus add a few lines to state that you were the one who did the modifications. In all of the following examples I will respect this license and you should do the same if/when you modify this code for your own purposes.

As the example uses three LEDs I suggest we have some fun with it and use a red, yellow and green LED (although you could really use any colors you like) and make our own traffic light!  We can then give it commands to stop, go and proceed with caution.

LED “Traffic Light” Hookup

First we’ll need to hook the LEDs up to the AIY Voice HAT. We will use the Servo outputs for that as they provide enough current for an LED but by changing the GPIO pins defined in the code we could also use the Drivers outputs if we had wanted to.

AIY LED Hookup

Each LED is hooked to the output pin on the servo connectors through a dropping resistor, I used three 330 ohm resistors but any value from 220 to 470 ohms would work.

The Servo outputs are connected to the Raspberry Pi GPIO pins as follows:

  • Servo 0 – Connected to GPIO #26. This is the Red LED.
  • Servo 1 – Connected to GPIO #6. This is the Yellow LED.
  • Servo 2 – Connected to GPIO #13. This is the Green LED.

The cathode leads of all of the LEDs are connected to ground, any of the servo ground connections will work.

Now that we have our LEDs hooked up lets take a look at the Python code we will use to control our “traffic light”.

LED “Traffic Light” Code

To control our little “traffic light” we will modify the Assistant Library with Local Commands script by adding several extra functions. We also need to import a library to interact with the Raspberry Pi GPIO.


You’ll notice that I left the original functions from the Assistant Library with Local Commands script intact. If you wish you can remove them.

The GPIO library is imported on line 43.  The lines following that setup the GPIO pins as outputs.

Line number 45 has a statement “GPIO.setmode(GPIO.BCM)” . This tells the code to use the Broadcom numbers for the GPIO pins, this is the standard method of addressing GPIO pins when working with the Raspberry Pi.  The Broadcom pin numbers are the internal processor numbers, they don’t match the physical pin numbers on the 40-pin GPIO connector. If we had wanted to use the physical pin numbers instead we could have used the statement “GPIO.setmode(GPIO.BOARD)”.

The next line, “GPIO.setwarnings(False)” is optional, it just suppresses the error messages you will receive when the GPIO pins have already been defined by another script. Experiment by taking it out to see what I am talking about.

The next three lines set GPIO numbers 26 (Red LED), 6 (Yellow LED)and 13 (Green LED) as outputs.

Starting at line 57 you’ll see several functions, each one written to control an LED. As with the functions we examined previously they perform two tasks:

  1. They each use an “” command to say something.
  2. They then use a “GPIO.output” command to control the respective LED (or LEDs in some cases). A “false” will turn off the LED while a “True” will turn it on.  

You can change the command to make it say your own phrase if you wish.

You should also note that the way I wrote it the voice will FIRST say the text and THEN perform the action. You can reverse the order of the statements if you wish so that the text is spoken AFTER the action takes place.

Moving down to line number 149 you will see the start of several “elif” statements, each one to interpret some text and to then call one of our custom functions. Again you can change the text that you wish interpreted if you wish.

You should place the python script into the same folder as the examples Google provided. Also, if you get the script form the Resources box at the end of this article you will need to give it the correct permission to run.  You can do that at the command line but it’s easier to use the Raspbian File Manager:

  1. Copy the script into the home/pi/AIY-voice-kit-python/src/examples/voice/ folder.
  2. Right click on the python file you just copied. A menu will appear.
  3. Select “Properties” from the menu.
  4. Select the “Permissions” tab in the dialog box that displays.
  5. Change the “Execute” permission to “Anyone”.
  6. Click OK to save the changes.

If you don’t do that you’ll receive an error when you attempt to run the script from the Dev Terminal.

Testing the LED “Traffic Light”

Open up the Dev Terminal and type src/examples/voice/ (if you wrote your own script substitute the script name for, which is the name of the script in the ZIP file).  After it loads you’ll be prompted to say “OK Google” followed by a command.

The script has several commands:

  • Red light on – Turns on the Red LED,leaves the other LEDs in their current state.
  • Red light off – Turns off the Red LED,leaves the other LEDs in their current state.
  • Yellow light on – Turns on the Yellow LED,leaves the other LEDs in their current state.
  • Yellow light off – Turns off the Yellow LED,leaves the other LEDs in their current state.
  • Green light on – Turns on the Green LED,leaves the other LEDs in their current state.
  • Green light off – Turns off the Green LED,leaves the other LEDs in their current state.
  • Traffic Go – Turns on the Green LED. Turns off the other LEDs.
  • Traffic Stop – Turns on the Red LED. Turns off the other LEDs.
  • Traffic Caution – Turns on the Yellow LED. Turns off the other LEDs.
  • All lights on – Turns on all of the LEDs.
  • All lights off – Turns off all of the LEDs.

Experiment with the commands, change them or change the voice responses as you see fit. It’s your traffic light!

DC Motor Control

Our final python script will illustrate how you can control a small DC motor using your voice. There is a sample in the Google AIY Voice Kit instruction manual on their website, this script differs in the following fashion:

  • It allows you to control the speed of the motor as well as turn it on or off.
  • It doesn’t use the push button as a trigger, it is triggered with the “OK Google” wake word.

Let’s see how we can accomplish this magic.

Controlling DC Motors with PWM

Our motor will be driven using Pulse Width Modulation or PWM. This is a method of sending pulses of voltage to drive a DC motor, the wider the pulses get the faster the motor turns.

If you need a full description of how PWM works please see my article on using the L298N H-Bridge controller to drive a couple of DC motors.  In the article (and in its associated video) I give a full explanation of how Pulse Width Modulation is used to control motor speed.

I have made no attempt to control the motor direction in this simple python code. If you wish you can hook an H-Bridge controller like the L298N to the raw GPIO outputs and use it to control the motor direction. To simplify things (and to prevent you needing an additional component) I won’t cover that today.

Motor Control Hookup

The motor hookup is very simple, but you’ll notice that I added an additional component that you may not have expected – a small capacitor which I soldered directly across the motor leads.

I did this because when I first built this demonstration I encountered problems with noise. Noise of two types actually:

  • Electrical noise generated by the motor itself. This is the reason for the capacitor, it suppresses a lot of this noise.
  • The actual sound of the motor was so loud my microphones had trouble hearing my voice!

The second bit of noise can be reduced by simply moving the motor away from the pickup pattern of the microphones.  

I used a 100 nf capacitor for noise suppression but its value is not critical. Don’t use a polarized capacitor as the motor generates a “back EMF” in the reverse polarity of the voltage applied to it, The AIY Voice HAT has diodes on each of the Driver outputs to suppress this back EMF.

Here is the hookup, as I said it’s very simple:

AIY Motor Hookup

In addition to hooking up the motor you’ll need to supply an external power supply as the motor will take more current than you want to suck out of your Raspberry Pi. I used my 5-volt bench supply and it worked even though my motor was rated at 6 volts.  You could also use a 6-volt battery pack if you wish.

Remember, you need to cut the External Power jumper in order to use an external power supply.

I left the three LEDs from the last experiment in the circuit just for fun!  Now let’s look at the python code.

Motor Control Code

As with the previous demonstration I used a modification of the Assistant Library with Local Commands script to control my motor.  The script is similar to the last demonstration in many ways, it uses the GPIO library to control the three LEDs as you might expect.

It also imports another library, the “PWMOutputDevice” library which itself is part of the “gpiozero” library.  This is the library that will provide the Pulse Width Modulation functions to drive our motor.


In this script I start by setting up the LEDs in the same fashion as the previous script. Next I create an object I called “motor1” that represents my PWM output device, which in this case is a motor connected to GPIO pin 4.

The three functions I defined do the following:

  • motor_on_full() –  This turns on the motor at full speed.
  • motor_off() – As you might expect this turns the motor off.
  • motor_on_half() – This spins the motor at half speed.

To control the motor speed we use the “value” parameter on the motor object. This can be set from any value from 0 to 1 with a value of “0” stopping the motor and a value of “1” driving it at full speed.

To drive the motor at half speed I gave the parameter a value of “0.5”. You can expand upon this if you wish and give your motor many different speed increments.

Testing the Motor

Create or copy the script in the the home/pi/AIY-voice-kit-python/src/examples/voice/ folder. If you used the script I provided in the ZIP file (from the Resources box below this article) you’ll need to set the permissions using the same technique described for the last script.

Run the script from the Dev Terminal by typing src/examples/voice/ at the command line – if you created your own script substitute its name for

You can now speak the three motor commands to drive your motor:

  • Motor on – Turns on the motor at full speed.
  • Motor off – Turns off the motor
  • Motor half – Turns on the motor at half speed.

You may notice some distortion in the speaker when the motor is spinning, the capacitor reduces the electrical noise but doesn’t eliminate it completely. Shielding the microphone lines and enclosing the project in a shielded metal enclosure would probably eliminate that in a permanent design.


Hopefully you now have an idea as to how versatile the Google AIY Voice Kit is. Far from just being a cardboard Google Home Mini the AIY Voice Kit is a developers tool capable of prototyping advanced voice control devices.

If you have any questions about the code here or the AIY voice HAT please comment below. And please look out for the next video and article in this series as I’ll continue to explore the amazing capabilities and potential of the Google AIY Voice Kit.

Now start talking to your project!



Hacking the Google AIY Voice Kit – Part 1 – The original article in this series, building the Google AIY Voice Kit.

Code for this Article – All of the Python code I showed in the article in one easy to use ZIP file!

Google AIY Voice Website – The official Google AIY Voice Kit website.

Google AIY Raspbian Image – The custom build of the Raspbian operating system for the Raspberry Pi.

Google Cloud Console – The Google Cloud Console where you will setup the Cloud Speech API.

Google Cloud Speech API Documentation – Documentation for the Google Cloud Speech API.


Hacking the Google AIY Voice Kit – Part 2- Voice Control
Hacking the Google AIY Voice Kit - Part 2- Voice Control
Article Name
Hacking the Google AIY Voice Kit - Part 2- Voice Control
Learn to voice-control LED's and motors with the Google AIY Voice Kit for Raspberry Pi.
Publisher Name
DroneBot Workshop
Publisher Logo
Notify of

Inline Feedbacks
View all comments
6 years ago

Thank you for posting the valuable and helpful projects.

Currently the voice kit will light up when I say a wake word “Hey, Google”

Can I make a change so that so that when I say a wake word “Hey, Google” the system will response by voice such as “Yes” or ‘OK’ as well as light up.


Sudharm Phatak
6 years ago

Thanks for the tutorials, really helpful!!

6 years ago

Finally i can compete my project because of this tutorial!
Thanks man!

5 years ago

Is it possible to hook up a camera to the voice kit and use inbuilt computer vision python libraries to interface voice as well as image AI?

5 years ago

Thanks for the great post. I’m intrigued by your experiment board. I also have something similar to the pi-cobbler, and I’m interested in understanding how you were able to piggy back the voice kit hat so I can build something similar.
BTW, great tutorials. Just what I needed to jump start my own experiments!

5 years ago

Is there going to be a part 3?


Régis Saint Girons
4 years ago

Hello, I was wondering if you are still planing to make a part 3?

Ran Talbott
5 years ago

Thanks for documenting all this, Bill! You answered my #1 question about getting into this: whether I could use the kit for “Speech to command” for my own programs. That’s especially helpful, because everything else I’ve read so far just focuses on “Isn’t it way kewl that you can make your own ugly cardboard Google Home Lite?”. 😉 But you raised another: how likely is it that I’ll find out on the 20th of the month that I can’t robo-feed my cat, or unlock my front door, with voice commands for the next 10 days because I’ve used up my… Read more »

5 years ago

How do I switch my voice kit to use an USB plugged microphone ?

Ritwik Mehrotra
5 years ago

How can I control motor direction

4 years ago

Hi, could you do a video on the “Google AIY Vision Kit” please. Thanks

Keep up those GREAT videos

8 months ago

Hey! Nice tutorial, helped me hack the voicekit and add GPIO functionality for 2 extra buttons. I’m running into a problem though; the only way for the speaker to work is by unplugging the microphone. If i plug the microphone, the speaker stops working! I added 2 buttons on the servo pins 0 and 1 (using GPIO 4 and 17 with the GPIO library) and those buttons work fine. The voicekit button also works fine and its connected as it comes. I’m sure the problem is not with the code, cause i’ve made various tests (Without the extra buttons, with… Read more »