Do you allow this site to use Cookies?

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)


Add comment
Enter any text in "Title"

I have very limited time currently for the comments, so you will probably get a very slow response for questions I know the answer to. If I need to set up a Pi to check your issue or request then I am unlikely to have chance to spend time on this.
I will continue support once I can spare the time again.
Thank you for your interest