ReSpeaker 2-Mics Pi HAT

ReSpeaker 2-Mics Pi HAT


Google Voice Services

We recently had a look at the excellent Google AIY Voice kit, which can be used to create your very own Google Assistant. However, given the possible limited availability of this kit, we thought we’d take a look at an alternative way of using the Google voice services.

ReSpeaker 2-Mics Pi HAT

The Seeed Studio ReSpeaker 2-Mics Pi HAT is a small form factor board, the same size as a Pi Zero, that offers the following features:

  • Dual microphones
  • 2 Grove style interfaces for I2C and GPIO
  • Programmable button
  • 3 RGB LEDs
  • Onboard audio CODEC
  • 3.5mm audio jack socket
  • JST 2.0 socket for external speaker

ReSpeaker 2-Mics Pi HAT

As with any other HAT, the ReSpeaker 2-Mics Pi HAT can be mounted on a Raspberry Pi, and the Pi then programmed accordingly.

To help with the programming for the ReSpeaker HAT, there is plenty of documentation available online. Having said that, we did find that it was necessary to be a little creative with some of the information that we found.

Initial Set Up

The first step in getting to grips with the ReSpeaker 2-Mics Pi HAT is to mount the HAT onto a Raspberry Pi. Ultimately we would like to use the HAT with a Pi Zero. However, for our initial set up we made use of a Pi 3 Model B, since this is easier to attach to monitor, keyboard and mouse. Once we are happy that everything is working the way we would like, we will swap the Pi 3 for a Pi Zero, using the same SD card.

Once the HAT has been mounted onto the Pi 3, the Raspberry Pi needs to be attached to a keyboard, mouse, and monitor. Following that, the Pi is booted using an SD card that is imaged with the latest version of Raspbian. The process for imaging the SD card can be found at the Raspberry Pi Foundation’s website. We also cover this process in our blog article on Programming SD cards for Raspberry Pi.

Preparing the Raspberry Pi

Having booted the Raspberry Pi, there are a number of bits of software that need to be installed in order for the ReSpeaker 2-Mics Pi HAT to work. Before this is done, however, there is a small amount of configuration to be done. To configure the Raspberry Pi, execute the terminal command:

sudo raspi-config

In the configuration tool, configure the following items as required:

  • User password
  • Network options – hostname
  • Localisation options – current time zone
  • Advanced options – expand the filesystem

Once this configuration is complete, the Pi should be rebooted. Following the reboot, the Pi should next be updated to ensure that all currently installed software is up to date. To update the installed software, execute the following terminal commands:

sudo apt update
sudo apt upgrade

Installing the ReSpeaker Software

The first suite of software that should be installed is the ReSpeaker 2-Mics Pi HAT software. The full installation process for this is described in the ReSpeaker Wiki on the Seeed Studio website. However, in brief, this software may be installed using the following terminal commands:

cd ~
git clone https://github.com/respeaker/seeed-voicecard.git
cd seeed-voicecard
sudo ./install.sh 2mic

Following this, reboot the Raspberry Pi using the terminal command:

sudo reboot

Following the reboot, the following terminal commands may be used to check that the ReSpeaker 2-Mics Pi HAT software is working correctly:

aplay -l

This should list all the playback hardware devices available and should return output similar to:

**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
 Subdevices: 8/8
 Subdevice #0: subdevice #0
 Subdevice #1: subdevice #1
 Subdevice #2: subdevice #2
 Subdevice #3: subdevice #3
 Subdevice #4: subdevice #4
 Subdevice #5: subdevice #5
 Subdevice #6: subdevice #6
 Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 1: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
 Subdevices: 1/1
 Subdevice #0: subdevice #0

Similarly, the terminal command:

arecord -l

should return a list of capture hardware devices, with output similar to:

**** List of CAPTURE Hardware Devices ****
card 1: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
 Subdevices: 1/1
 Subdevice #0: subdevice #0

Modify System Boot file

For our purposes, we only want to make use of the ReSpeaker 2-Mics Pi HAT for our audio input and output. Because of this, we can disable the Raspberry Pi onboard ALSA (Advanced Linux Sound Architecture) audio interface. To do this, the file /boot/config.txt needs to be edited using the command:

sudo nano /boot/config.txt

In this file, modify the line referring to dtparam=audio=on (near the bottom of the file) and set the parameter setting to off. Following this change, the tail end of the config.txt file should look as follows:

# Enable audio (loads snd_bcm2835)
dtparam=audio=off
dtoverlay=i2s-mmap
dtoverlay=seeed-2mic-voicecard

Having modified this config file, reboot the Pi.

Following the reboot, the aplay-l and arecord -l commands should now only return the seeed-2mic-voicecard devices:

aplay -l
**** List of PLAYBACK Hardware Devices **** 
card 0: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
 Subdevices: 1/1
 Subdevice #0: subdevice #0
arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
 Subdevices: 1/1
 Subdevice #0: subdevice #0

Enable Google Assistant API

The next stage in the set up of the Google Assistant for the ReSpeaker 2-Mics Pi HAT is to enable and install the Google Assistant API software. The relevant steps for enabling the API can be found at the Google AIY Voice Kit website, under the ‘Setting up your device’ section. These instructions include a step to download and rename your credentials file. In order to rename your downloaded credentials file, execute the following terminal commands:

cd ~/Downloads
mv client_secret_XXXX /home/pi/assistant.json

Note – for the second of these commands you will need to replace the XXXX with the long ID string of the file. A shortcut for doing this is to type:

mv client_secret

followed by the <tab> key. This will automagically fill in the complete file name for you. You can then type in the rest of the required command.

Install Google Assistant API

In order to install the required Google Assistant API software and the required software dependencies, execute the following terminal commands:-

cd ~
git clone -b voicekit https://github.com/google/aiyprojects-raspbian.git ~/voice-recognizer-raspi
cd ~/voice-recognizer-raspi
scripts/install-deps.sh

Installation of the dependencies will take some time, and once complete the Pi should again be rebooted.

Check the Sound Card

Once the Google Assistant API has been installed, you can check that the ReSpeaker HAT is working correctly by connecting a pair of headphones to the headphone jack on the ReSpeaker HAT, and executing the following terminal commands:

speaker-test -t wav

This should result in speech being heard from the headphones.

Also, the terminal command:

arecord -f cd -Dhw:0 | aplay -Dhw:0

should result in any sound captured by the microphone being played back through the headphones (with a slight delay).

If these commands do not work correctly, it may be necessary to carry out some configuration of the ALSA sound settings to ensure that the capture and playback device are correctly configured.

Modify The ALSA Sound Configuration File

If the sound capture or playback is not working correctly, it may be necessary to configure these devices. This is done by editing the .asoundrc file in the /home/pi folder. To edit this file, execute the following terminal command:

sudo nano /home/pi/.asoundrc

If this file does not exist, this command will cause a new empty file to be created. This configuration file will define the characteristics of the playback and capture devices of the ReSpeaker HAT. The contents of this file should be as follows:

pcm.!default {
   type asym
   capture.pcm "mic"
   playback.pcm "speaker"
}
pcm.mic {
   type plug
   slave {
      pcm "hw:0,0"
   }
}
pcm.speaker {
   type plug
   slave {
      pcm "hw:0,0"
   }
}

The pcm “hw:0,0” lines define the card and device numbers for the capture and playback device; the values 0 and 0 being the card number and the device number. These should match the numbers provided by the output of the aplay -l and arecord -l commands.

aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
 Subdevices: 1/1
 Subdevice #0: subdevice #0

arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
 Subdevices: 0/1
 Subdevice #0: subdevice #0

After editing the .asoundrc file, again check that everything is working correctly by connecting a pair of headphones to the headphone jack on the ReSpeaker HAT, and executing the following terminal commands:

speaker-test -t wav

which should result in speech being heard from the headphones.

And the terminal command:

arecord -f cd -Dhw:0 | aplay -Dhw:0

which should result in any sound captured by the microphone being played back through the headphones (with a slight delay).

Ready to Rock

In theory, you should now be ready to try out the Google Assistant demonstration programs. To run these programs, you need to first navigate to the correct folder and activate the Python virtual environment that is created as part of the Google Assistant API installation process. To do this, execute the following terminal commands:

cd ~/voice-recognizer-raspi
source env/bin/activate

Having activated the Python virtual environment, try running the assistant_grpc_demo using the terminal command:

python3 src/assistant_grpc_demo.py

Note, we did have an issue when we tried running the assistant_grpc_demo program. We found that when this program was run, we got the following error:

(env) pi@raspberrypi:~/voice-recognizer-raspi $ src/assistant_grpc_demo.py
Failed to import google.cloud.speech. Try:
    env/bin/pip install -r requirements.txt

The suggested method of fixing this issue did not appear to work. However, tracing through the Python code, we discovered that in the Python module aiy._apis._speech there was a try/except statement which checked for the Google cloud speech module (a module that is not actually required for the GRPC demo). We found that commenting out this block of code allowed the assistant_grpc_demo program to run successfully. To remove the offending code, modify the file:-

/home/pi/voice-recognizer-raspi/src/aiy/_apis/_speech.py

and comment out the following lines of code:

#try:
#    from google.cloud import speech
#    from google.cloud.speech import enums
#    from google.cloud.speech import types
#except ImportError:
#    print("Failed to import google.cloud.speech. Try:")
#    print(" env/bin/pip install -r requirements.txt")
#    sys.exit(1)

Following this change to this code module, we found that the assistant_grpc_demo program would run without a problem.

When this program is run initially, you will find that there is no way to trigger the Voice Assistant since the GRPC demo does not support hot word detection. Additionally, because this demo is written for use with the Google AIY Voice kit, the GPIO pin of the button on the ReSpeaker HAT does not correspond with that on the Google Voice HAT. However, there is a really simple solution to this.

Modifying the Google Assistant Code

In order to get the Google Assistant demo code to trigger as required, it will be necessary to make some minor modifications to the Google Assistant code. The code module that defines the GPIO pins for the button and LED is aiy.voicehat which can be found in the file:

/home/pi/voice-recognizer-raspi/src/aiy/voicehat.py

This file needs to be modified in the section that defines the GPIO pin definitions for the button and the LED. At this stage, we simply want to modify the pin number for the button, so the line:

_GPIO_BUTTON = 23

should be modified to:

_GPIO_BUTTON = 17

since the button on the ReSpeaker 2-Mics Pi HAT is connected to GPIO pin 17.

Having modified this code module, running the assistant_grpc_demo program should now trigger the Google assistant when the button on the ReSpeaker HAT is pressed. Try pressing the button and asking a question.

Running Voice Assistant as a Service

In order for the required Voice Assistant program to start automatically each time the Pi is rebooted, it is necessary to set it up as a service.

To do this, first create a file called my_assistant.service, and edit it to contain the following:

[Service]
ExecStart=/bin/bash -c '/home/pi/voice-recognizer-raspi/env/bin/python3 -u src/assistant_grpc_demo.py'
WorkingDirectory=/home/pi/voice-recognizer.raspi
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Having saved the file, it will need to be moved to the folder used by systemd, which controls system services. To move the file, execute the terminal command:

sudo mv my_assistant.service /lib/systemd/system/

Finally, to enable the service, execute the terminal command:

sudo systemctl enable my_assistant.service

If the Pi is now rebooted, the Voice Assistant should automatically start on reboot. The status of the service may be checked using the terminal command:

sudo service my_assistant status

Connecting an External Button and LED

Our next step in using the ReSpeaker 2-Mics Pi HAT was to add an external button and LED, as this opens up further possibilities in terms of how the HAT may be used.

In order to add the external button and LED, we connected a standard Seeed Studio Grove connector to the GPIO JST socket on the ReSpeaker HAT. This GPIO JST socket breaks out pins 12 and 13 of the Raspberry Pi, as well as a +3.3v and ground pin. This allows the connection of a button and LED. The circuit diagram for our connections to the GPIO socket is shown in the diagram below. The circuit includes a 560Ω pulldown resistor for the button and a 100Ω current limiter resistor for the LED. The button is connected to GPIO pin 12 and the LED to pin 13.

Respeaker Button and LED

In order for this circuit to work with our code, we again need to modify the file:

/home/pi/voice-recognizer-raspi/src/aiy/voicehat.py

This time, the lines of code relating to GPIO pins need to be modified to:

_GPIO_BUTTON = 12
_GPIO_LED = 13

Button and LED

Connecting an External Speaker

Finally, we decided to try adding an external speaker, as again this would provide us with a little more flexibility as to how the ReSpeaker 2-Mics Pi HAT may be used. For our purposes, we happened to have a small 8Ω speaker taken from an electronic toy. In order to connect this speaker to the ReSpeaker HAT, the speaker had to be soldered to a JST plug. For this, we butchered a battery cage by snipping off the wire as close as possible to the battery cage. The wires were then stripped to about 5mm and soldered to the speaker terminals.

Battery Cage

Miniature Speaker

 

 

Leave a comment