How to Build a DIY Google Home Assistant With Raspberry Pi
Whatsapp Pinterest

Voice controlled assistants have quickly become commonplace. Many homes have an Alexa or Google Home controlling everything from lighting to media, and even timekeeping.

The technology these devices run on is available—at least in part—to everyone. Google’s Assistant SDK allows you to use the service on your own devices. This tutorial covers how to set up the Google Assistant on your Raspberry Pi, and voice activate an LED via GPIO pins.

Hardware Required for a Raspberry Pi Home Assistant

Raspberry Pi Google Assistant Required Equipment

You Will Need:

  1. Raspberry Pi with a fresh Raspbian install on the SD card How To Install An Operating System To Your Raspberry Pi How To Install An Operating System To Your Raspberry Pi Here's how to get a new OS installed and running on your Pi – and how to clone your perfect setup for quick disaster recovery. Read More .
  2. USB webcam or microphone.
  3. External speaker.
  4. Circuit from the Pi LED tutorial How to Program Your Raspberry Pi to Control LED Lights How to Program Your Raspberry Pi to Control LED Lights Looking for an easy Raspberry Pi project to get started with coding and electronics? Try connecting some LEDs and coding them to turn on and off! Read More (optional)
  5. A browser signed into your Google account.

Note: The equipment for this tutorial can vary somewhat. I used a spare USB webcam purely for its microphone. Any compatible webcam or microphone should do fine, and there are extensive lists of Pi-compatible devices to help.

Also, I am using the 3.5mm output for audio. HDMI and other output sources will also work, though it will require modifications to the sound setup below.

Plug in the USB webcam and speaker, and set up the LED circuit if you are using it.

Setting Up the Sound

This tutorial can be followed directly on the Pi or via an SSH connection into the Pi.

Both ways begin in the terminal, to check the sound settings. Use the commands arecord -l and aplay -l to list available devices.

Check the Playback and Recording devices

The image above shows the USB webcam as card 1 and device 0. Take note of the card and device numbers for both microphone and speaker output.

Now, making sure you are in the /home/pi directory, create a new file and open it in nano:

sudo nano .asoundrc

The contents of this directory will differ depending on what card and device numbers you are using. This is where you can choose to favor HDMI output rather than 3.5mm if you wish.

asoundrec config file for microphone and speaker

When you’ve entered your version of the above code, press Ctrl + X to save and exit.

Test your setup by recording and playing back a short clip:

Make a test recording to check your setup

Optional Step: If you want to change the input volume of your microphone, open alsamixer and press F6 to cycle between devices.

That’s it! The sound is set up.

Creating the Google Project

Open the Pi’s browser, alternatively, if you are connected via SSH open a browser locally. Navigate to the Google Action Console and click New Project.

Create a new Google Project

This may take a few moments. When finished, leave the window active and open a new tab—we’ll be coming to this in a moment.

Enabling the Google Assistant API

There are a few online adjustments you’ll need to make to continue. Navigate to the Google Assistant API website and click Enable.

Enable the Google Assistant API

The project also requires activity permissions. Head to your Activity Control panel and make sure the following activities are turned on:

  • Web & App Activity (including Chrome History checkbox)
  • Device Information
  • Voice and Audio Activity

Changing Google Activity settings

Now you can move on to registering the device.

Registering Your Raspberry Pi

Back in the Action Console, select Device registration from the left panel. Under Product create an easy to remember name for your device. The manufacturer name isn’t important (but must be there), and select Auto for the device type.

Register your Pi as a Model

Click Register Model, and on the next screen click Download OAuth 2.0 Credentials. This downloads a JSON file to your computer. If you aren’t familiar with JSON files, don’t worry, but learning how to use JSON JSON Python Parsing: A Simple Guide JSON Python Parsing: A Simple Guide There are libraries and tool-kits available for parsing and generating JSON from almost any language and environment. This article concentrates on methods and issues arising from JSON python parsing. Read More is worth doing for the future!

The official Google guide recommends moving the file to /home/pi, so open the file manager and do this now.

Extra step for SSH users:

If you are using SSH you will have downloaded the JSON file to your local machine instead of the Pi. To transfer it, open a separate terminal window with no SSH connection. From this window, copy over the client secret JSON file using this command:

scp ~/Downloads/client_secret_client-id.json pi@raspberry-pi-ip-address:/home/pi/

Replace “raspberry-pi-ip-address” with your Pi’s ip address, and don’t forget the colon before the path. If you downloaded the JSON file to another location modify your local path to reflect this. Enter your password when prompted, and the file will copy to the Pi’s home directory.

Copy the Secret Cient ID to the PI

Switch back to the SSH terminal, and navigate to /home/pi. Enter ls -l to list the files in the directory. You should see the transferred client secret JSON file.

Installing the SDK

Google recommends working in a Python virtual environment. Create a new virtual environment called env.

If you’ve never done this before, this tutorial will help you learn how to use Python virtual environments.

Install the latest versions of Pip, Setuptools and Wheel and activate your virtual environment:

env/bin/python -m pip install --upgrade pip setuptools wheel
source env/bin/activate

Google Assistant has some dependencies which you should install into the virtual environment now.

sudo apt-get install portaudio19-dev libffi-dev libssl-dev libmpg123-dev

Finally, install the Google Assistant SDK, Samples, and OAuth tool.

python -m pip install --upgrade google-assistant-library
python -m pip install --upgrade google-assistant-sdk[samples]
python -m pip install --upgrade google-auth-oauthlib[tool]

That is everything needed to get up and running. If any of the installations fail, check the spelling and spacing thoroughly.

Authenticating the Raspberry Pi

Use the google-auth-oauthlib[tool] with the credential JSON file downloaded earlier to authenticate your Raspberry Pi.

google-oauthlib-tool --scope \
      --scope \
      --save --headless --client-secrets /home/pi/YOUR_CLIENT_SECRET_ID.json

You’ll need to replace YOUR_CLIENT_SECRET_ID with the downloaded file, so it is worth copying the filename first. This client ID must be correct. Do not change the filename!

You should get a message with a link, asking you to paste in an authorization code.

Generating the Authorization link from the command line.

Clicking the link opens the browser. You’ll be prompted to enable the device on your Google account. Copy the authorization code which follows, and paste it back into your terminal window.

You should receive a confirmation reading Credentials Saved: /home/pi…, meaning the Pi has been authorized successfully with your Google account.

Testing It Out

Now that everything is in place, it’s time to test your Pi Google Assistant. Run the assistant with this command:

googlesamples-assistant-hotword --project-id my-dev-project --device-model-id my-model

You will need to replace my-dev-project with your Project ID (found under the Settings cogwheel of the Action Console). Your device-model-id is listed under the Device registration section of the Action Console.

Try it out! Say “OK Google” and ask a question. You can see the program output in the terminal as you hear the response:

Terminal output for Google Assistant

That’s it! Google Assistant is now running on your Raspberry Pi. Note that if the output volume is a little low, you can change it by saying “Hey Google, turn your volume up to 80%.”

Bonus: Voice Activated GPIO

It is possible to control lights with an Arduino and Siri, but there is a simpler method. If you set up an LED, you can use the Google Assistant to control it with your voice.

Setting up Google Assistant to work with the GPIO pins is relatively simple, but requires some extra steps. Head to the Google Action Console, and find your device under Device registration. Click on it, and open the traits menu:

Activating the OnOff Assistant trait

Turn the OnOff trait on, and click Save.

Now making sure you are inside the env virtual environment, clone a version of the SDK to your Pi using git:

git clone

Since this is a virtual environment, you’ll need to install RPi.GPIO before moving forward.

pip install rpi.gpio

Now, navigate to the folder containing the script.

cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library

Modifying the Script

You’ll need to add a few lines to the script, so open it in the nano editor:


Under the import statements, add your own for RPi.GPIO.

import RPi.GPIO as GPIO

Look for the process_event method. At line 66, remove or comment out the print statement, and add an if statement to control the LED.

#print('Do command', command, 'with params', str(params))
if command == "action.devices.commands.OnOff":
    if params['on']:
            print('Led turned on')

            GPIO.output(18, GPIO.HIGH)
                print('Led turned off')

                GPIO.output(18, GPIO.LOW)

This logic controls the LED, but so far it isn’t configured to output. Set it up in the main() function before the process_event method gets called.

GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)

Now the GPIO pin is set to output and initializes in a low state. Save and quit. You can run your modified script passing your model-id number (found in the Action Console) as an argument.

python --device-model-id YOUR-MODEL-ID-HERE

The terminal output is the same as before, and the assistant will work as standard. Now however, when you say “OK Google, turn on” you will see a new output:

Terminal Message showing "LED turning on" print statement

Note: The above image has been cropped, showing only the assistant hearing the request, and the print statement added to the script.

You should see your LED light up too!

Voice activated LED.

Your Own DIY Raspberry Pi Google Home Assistant

This project is a good introduction to using Google API services. Modifying Google’s example code also helps with learning the basics of Python.

With this tutorial as a basis, you can link the PI Assistant to other DIY Raspberry Pi projects Raspberry Pi Projects for Beginners Raspberry Pi Projects for Beginners These 10 Raspberry Pi projects for beginners are great for getting an introduction to the hardware and software capabilities of the Pi, and will help you get up and running in no time! Read More and build yourself a voice-activated smart home!

Explore more about: DIY Project Tutorials, Google Assistant, LED Lights, Raspberry Pi, Smart Hubs.

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Isaiah Stephens
    November 30, 2018 at 10:06 am

    Does this work for rpi zero w and the new 3a+ ?

    • Ian Buckley
      December 1, 2018 at 1:11 pm

      As far as I know it is the same process for the new 3A+ but not the Zero. There is an in depth walkthrough for getting it working on the pi zero on the Mikey's Lab YouTube channel which I haven't had time to check out yet.

      Might be a good place to start. Let me know if you get it going!