Saturday, 06 May 2017 11:48

Google AIY Voice HAT Customise Voice Commands

Written by 
  • Author Type: Individual
  • Operating System: Raspbian, Android Things
  • Country: UK

Google's Artificial Intelligent Assistant comes to the Raspberry Pi 3 with the AIY Projects Voice Hat. Now you can control your GPIO projects with Voice controls.

With issue 57 of The MagPi magazine a Voice HAT was given away that uses Googles AIY Projects service to bring voice controls to Raspbery Pi Projects. The Kit contained a GPIO breakout circuit board, microphone board, a speaker, a button, led and connectors. Once the kit is built and the Google Assistant API has been setup using a Google Account on the Google API Console you can press the button and ask a question to receive and answer. In the same way OK Google, Apples Siri or Amazon's Echo works.


The setup uses python scripts to interact with the google software which is where the Voice HAT's various connections come into use.


Using Python you can setup voice commands to control devices, sensors, servos, LED's etc connected to the HAT.

The Raspberry PI's GPIO port has been broken out across the board so you can use any project you currently have using the GPIO connectors including I2C, SPI and UART.

The guide in The MagPi and the guide at use a Raspbian OS image with the Google Assistant SDK setup which needs to be written to a new SD card before you start.

Though once you know what you are doing it could be setup on an existing Raspbian image.

With the setup there are several python scripts that can be used to get voice commands to control GPIO connected devices. There is also a guide in The Magpi but the script warns that there is an error in that guide so use the info in the script.

Google API Console

As part of the setup you will need to create the API's to use the google software at The Voice HAT can use the google Assistant API or the Speech API. The Speech API is more featured and has to have a billing account setup because if you use it for more than 60 minutes a month you will be charged for each 15 seconds at $0.0016.

If you just use the Assistant API you will not need a billing account as there will be no charges and you can still create your own commands. So it's best not to create a Speech API unless you wish to use those features. When you use your own commands with the Assistant API the voice is not so realistic. With the Speech Api the voice will be better

On the Desktop is an icon to test Cloude Services. This uses the Speech Api and will fail if no billing has been set up, so again ignore this if you just use the Assistant API.

 Android Things

Android Things is a new Operating system designed for connected devices and is fully compatible with the AIY Projects and other google services for development. This can be used as an alternative OS to Raspbian.

I missed the Voice HAT give away but like to buy one

Where can I buy a Voice HAT kit? If you didn't manage to get a copy of The Magpi with the free AIY Voice HAT kit you can still get your hands on one. At the time of writing they are not for sale yet but you can add your name to a mailing list to be notified when they are available for sale. Buy a AIY Projects Voice HAT for the Raspberry Pi



Well I need to go and try a few things out with this kit and see what projects I can make use of. So I will add to this as I get use to it.


in the meantime googles site has some additional information on how to use it and command options.

AIY Projects Voice Kit

 AIY Voice HAT GPIO Pin Positions

This is the GPIO pin positions. The 'AIY Project Voice Kit' link above also contains a GPIO pin diagram but there is a mistake as it has two 09 pins. The one for SPI-MOSI should be 10.


Raspberry PiZeroW

The Voice Hat is intended to be used with the RPI3 as it is the fastest Pi but I decided to give it a go with a Raspberry Pi Zero W and it works fine. It is a bit slower than a RPi3 to get a response but it does seem to work fine. 


 See a the video on YouTube Here

 Add Voice command to Shutdown or Reboot

As there is no way to shut the Raspberry Pi down if you are not using ssh or a display I have added Shutdown and Reboot commands to the script.

The file is in the src folder and is used to add your own commands.

The full path is /home/pi/voice-recognizer-raspi/src/

Toward the bottom of the file is were you can add your own commands and GPIO controls.

for the shutdown and reboot commands locate the banner:

# =========================================
    # Makers! Add your own voice commands here.
    # =========================================

and add the following commands directly below the banner but above the command "return actor"

    actor.add_keyword(_('system reboot'),SpeakShellCommandOutput(say, "sudo reboot",_("Reboot Failed")))


    actor.add_keyword(_('system shutdown'),SpeakShellCommandOutput(say,"sudo shutdown -h now",_("Shutdown Failed")))

and save the file.

Now when you say the commands "system shutdown" or "system reboot" you will get exacly that.



Shutdown command with feedback

The above version will quietly shut the system down. If you use a class it will respond to your command.

So under the banner

# =========================================
# Makers! Implement your own actions here.
# =========================================

add the OS library with

import os

then add the following class

class systemoff(object):
	"""System shutdown and reboot"""
	def __init__(self,say,command,speak):
		self.say = say
		self.command = command
	def run(self,cmd):
		if self.command=="reboot":
			os.system("sudo shutdown -r now")
		elif self.command=="shutdown":
			os.system("sudo shutdown -h now")

Now at the bottom of the file under # Makers! Add your own voice commands here, enter:

    actor.add_keyword(_('system reboot'),systemoff(say,'reboot','Rebooting, be back soon'))
    actor.add_keyword(_('system shutdown'),systemoff(say,'shutdown','Shutting down, goodbye'))

now when you say "system shutdown" it will respond with "Shutting Down GoodBye" and with "system reboot" it responds with "Rebooting be back soon"


 Get Readings from a Temperature Sensor

I have a Temperature sensor that uses the 1 wire device connection on GPIO 4. The Voice HAT has a bank of connectors under the heading Drivers. Driver 0 uses GPIO 4. If you haven't already you will need to solder the header pins to the Voice HAT using the strip of pins in the kit.


I have connected the 1 wire temperature sensor to driver 0 and then edited the /home/pi/voice-recognizer-raspi/src/ file with the script below.

Under the banner

# =========================================
# Makers! Implement your own actions here.
# =========================================

I added the following code

import os
import glob
import time
def localtemp():
	"""get reading from 1 wire temperature sensor"""
	os.system('modprobe w1-gpio')
	os.system('modprobe w1-therm')
	base_dir = '/sys/bus/w1/devices/'
	dev_folder = glob.glob(base_dir + '28*')[0]
	dev_file = dev_folder + '/w1_slave'
	f = open(dev_file,'r')
	lines = f.readlines()
	while lines[0].strip()[-3:] != 'YES':
		lines = ReadTemp()
	pos = lines[1].find('t=')
	if pos != -1:
		temp_str = lines[1][pos+2:]
		temp_c = float(temp_str)/ 1000
	result = str(int(temp_c)) + "degrees centigrade"
	return result

then under the next banner

    # =========================================
    # Makers! Add your own voice commands here.
    # =========================================

I have added the following code 

    actor.add_keyword('local temperature', SpeakAction(say,localtemp()))

Now when you say "local temperature" it will speak the sensors readings

To test the code stop the background service and run manually so you can see if there are any errors such as tab/space formatting.


sudo systemctl stop voice-recognizer

then run also in src folder.


If your test goes well exit the script with CTRL+c and activate the background service again

sudo systemctl start voice-recognizer

 (note if you remove the Temperature sensor the voice hat will error and not accept commands. I will update this soon to add error checking. I suggest you make a backup of the script before you try this out)


Last modified on Saturday, 03 June 2017 21:46
roboberry SuperUser.  Contactable via the site admin e-mail in the Contact Us link.


Leave a comment

Comments for Guest and account owners. Account owners will need to login to use registered e-mail address.


  • Comment Link roboberry Wednesday, 30 May 2018 21:06 posted by roboberry

    Hi Yahgoo, I knew they were doing a camera based kit I didn't realise the voice kit has been updated. Good to see the Zero supported properly. I will give it a go at the weekend. thanks

  • Comment Link Yahgoo Wednesday, 30 May 2018 08:39 posted by Yahgoo


    Google has a new aiy voice kit that comes with pi zero. Could you please the latest sd image
    I am curious to know whether the response from "Ok Google" can be improved.


  • Comment Link Roboberry Wednesday, 08 November 2017 08:50 posted by Roboberry

    Hi Fred, The latest AIY OS image has a different setup than when it was released on Mag-Pi which does mean the examples in this article are not relevant to the current setup.

    I will be updating the article once I get chance to spend a bit more time playing with the new process. Which looks much easier to integrate with python scripts.

  • Comment Link Fred Boliau Tuesday, 07 November 2017 17:38 posted by Fred Boliau

    I have a recent AIY Assistant Kit, but I can't find the Path, and or the file.
    Is something changed ?


  • Comment Link Roboberry Friday, 29 September 2017 21:17 posted by Roboberry

    Hi Paul

    I haven't see any details about their specific use, it may be for future use. As all the pins are through connections to the standard gpio pins. I would use them with general raspberry pi drivers and incorporate the python code in the script for voice control.


  • Comment Link Paul Maher Wednesday, 27 September 2017 20:39 posted by Paul Maher

    Has anyone got any detail about using the other drivers on the board e.g. servo and what looks like motor drivers? Struggling to find details

  • Comment Link Graeme Wednesday, 05 July 2017 20:05 posted by Graeme

    hi sckam, the temperature sensor is a 1 wire sensor. The only gpio pin on the raspberry pi that can use 1 wire sensors is GPIO 4 under the BCM numbering format (4th one on left)

    On the AIY Voice board it is connected to Drivers 0 pins on the right of the board. From the Temperature sensor this is Yellow under the D in Drivers, Red under V and black under S.
    If you haven't already you need to activate 1 wire mode in the desktop menu - preferences/Raspberry Pi Configuration. Then go to the interfaces tab and change 1 wire to enable.

    Then the code will work. Unfortunately you will need to move the light from GPIO4 to 27 instead.

    My example code expects the sensor to be connected at all times so when it is not connected the AIY Voice software will fail to run.

    Let me know if you need further help.

  • Comment Link sckam Wednesday, 05 July 2017 08:16 posted by sckam

    Hello Roboberry,
    I am have the same DS18B20 temperature sensor as you do. And I try to implement your code into my source code.
    I notice you mentioned you are using GPIO04 to connect the sensor(how is the connection ? any schematic ?). But my GPIO04 and GPIO17 already used for light bulb and fan.
    I am plan to use GPIO27 for the purpose to connect to the temperature sensor.
    May I know how to modify your code to suit that purpose ?
    Thanks in advance.

  • Comment Link Graeme Monday, 08 May 2017 21:55 posted by Graeme

    Hi Rob, I noticed reboot wasn't working earlier this evening but you beat me to updating the site.
    I also found when trying things out it is best to stop it running in the background and running so you get the feed back. Thanks for pointing out the issue.

  • Comment Link Rob Monday, 08 May 2017 21:47 posted by Rob

    OK, looked at it through manually running

    copy from HTML messes up the tab/spacing. Reformatting in IDLE sorts it.

    Also needed to add sudo to the reboot for mine or it hung the assistant waiting for permissions,

  • Comment Link Rob Monday, 08 May 2017 21:37 posted by Rob

    Hi. This all looks correct but am just getting into the syntax.

    When I add those lines it fails to start voice recognition completely.


Additional information