Voice Control Home Automation using PiRelay

Voice Control Home Automation using PiRelay

Speech recognition for computers is not as simple as it is for humans. We have seen devices interacting with humans. Android, Apple, Windows, etc have developed there own speech recognition systems that can interact with humans. All these systems had developed a lot in the past decade but still need lots of developments. 

In this article, we’ll not discuss how to make a speech recognition system but we’ll use an existing system to command our Raspberry Pi. 

We’ll implement the speech recognition on Raspberry Pi to control PiRelay Shield. The reason for opting PiRelay shield is that it can be directly used for controlling home appliances.

Things You Need

Things You Will Learn

  • Raspberry Pi
  • Python 
  • Relay
  • Embedded Systems

Getting Started with the Hardware

Setting Up Mic for Raspberry Pi

You can use any USB mic for interfacing with the Raspberry Pi. USB microphones are easy to plug and configure. Simply insert the USB cable into any of the Raspberry Pi port and you are ready.

Mic for Raspberry Pi

If you are familiar with PiRelay you are good to skip this section. 

Still here? PiRelay is a 4 channel relay shield for Raspberry Pi with maximum output contact of 250VAC 7A| 120VAC 15A and 30VDC 10A.


Relays are electromechanical switches that open and close circuits. Relays control one electrical circuit by opening and closing contacts in another circuit. Relays are commonly used where one needs to control a high power electrical or electronic circuits using a low power circuit. For example, we can control an LED operating at 5 volts directly with the Raspberry Pi pins, but if you want to control the fan in your room with the Raspberry Pi you will need a relay because Raspberry Pi operates at DC supply whereas fan is operating at high AC supply. Relay works on the principle of electromagnetic attraction which avoids the direct contact of two devices and controls them without the problem of getting damaged.

Relays have normally 5 pins Common, Normally Open(NO), Normally Closed(NC) and 2 control pins. When the control pin is not connected the contact is connected with Normally Open.



PiRelay is a relay shield for Raspberry Pi. It makes interacting with Relays easier by avoiding the complex wiring. It can be easily mounted on all 40 pin Raspberry Pis.

PiRelay Connections

  • It has 4 relay connectors all providing Comm, NO, and NC for each relay on the board. 
  • The LED indicators are used to indicate the status of the relay, you can say that if an LED for the relay is on its contact is connected to NO. 
  • Relay jumpers are used to customize the GPIO pins in case you are not using the same GPIOs as on PiRelay.
  • GPIO header is an extension of Raspberry Pi GPIOs so that you can interface with other sensors or I/O devices while using PiRelay.

PiRelay on Raspberry Pi

The PiRelay is an easily stackable board for Raspberry Pi. It is very easy to mount PiRelay on the top of Raspberry Pi. You just need to make sure that 40 pin female header of PiRelay mounts directly on the top of 40 pins of Raspberry Pi.


After mounting the PiRelay over Raspberry Pi, you are ready to connect devices to the PiRelay. You can connect anything below the max output contact range of 250VAC 7A| 120VAC 15A and 30VDC 10A.


Software Setup

For setting up the mic for Raspberry Pi we need to install several tools. These tools can be downloaded using the apt package, as-

sudo apt-get install alsa-utils mpg321

Now we need to load the drivers using-

sudo modprobe snd_bcm2835

Now that our audio setup is complete we will install the Speech Recognition module for Python3. You can install this module using pip, as-

pip3 install SpeechRecognition


The program we are using is available on the Github repository of PiRelay. You can go to this link https://github.com/sbcshop/PiRelay/blob/master/PiRelay_Voice_Control.py and use the code to develop your own PiRelay applications.

For the programming part, you will need to import the speech_recognition module, as

     import speech_recognition as sr

You can import other required modules as per your project, we have imported-

     import PiRelay

     import time, datetime

     import re

Setting up the speech_recognition module for speech capture and synthesis. We need to define the sample rate and chunk size of speech recorded by the module.

     sample_rate = 48000

     chunk_size = 2048

Instantiate the speech_recognition module to use its functions-

     r = sr.Recognizer()

Find the device ID of the mic you have connected. For this step, you need to know the mic name of your USB mike mine was 'Yeti Stereo Microphone: USB Audio (hw:0,0)' you can find the name of your mic by printing mic_list below.

     device_id = 0

     mic_list = sr.Microphone.list_microphone_names()

     for mic in mic_list:

         if mic == 'Yeti Stereo Microphone: USB Audio (hw:0,0)':

             device_id = mic_list.index(mic)


 Initialize the PiRelay module-

     relay1 = PiRelay.Relay("RELAY1")

     relay2 = PiRelay.Relay("RELAY2")

     relay3 = PiRelay.Relay("RELAY3")

     relay4 = PiRelay.Relay("RELAY4")

Use microphone function to listen to sound, and -

     with sr.Microphone(device_index = device_id, sample_rate = sample_rate,

                        chunk_size = chunk_size) as source:


        print("Say Something")

        audio = r.listen(source)

This is the most important part of speech recognition. In this, we specify which speech recognition service we want to use. In the article we are using google speech recognition APIs integrated with SpeechRecognition module. 

text = r.recognize_google(audio)

Go to https://realpython.com/python-speech-recognition/ for documentation of speech recognition module. For voice recognition you can use any module from the list below.


After the recognition is complete the voice is converted into text and we store it in a text variable. We’ll use this variable to extract the command user wants the Pi to perform. For extractions, we are using re(REGEX) module to extract the data.

If we receive a sentence from the user which contains the keyword “rel”, we consider that the user wanted to say “Relay”. And proceed to find what relay operation user wanted.

     if re.search("rel", text): 

After we extract our keyword, we find the operation user wanted to perform. Here we are checking for “On” and “Off” keywords in the text. We can use more such keywords to make our system better.

     if re.search('on', text):

After we find what operation user wanted, we fetch the relay number on which the operation will be performed. For this, we simply find relay numbers 1-4 in the text.

     if re.search("1", text):

The last step is to switch the relay On/Off to operate our appliances. This is done by simply using on and off functions in the PiRelay module.



All this repeats in a loop until the user doesn’t interrupt. Please note that you will need to clone the PiRelay repository and copy the PiRelay file into the working directory to use relay functions.


This project might be not the way googles “OK” or apples “Siri” does your task but it is a good way to start, and develop your voice-controlled home automation system without depending on any paid services.


1 thought on “Voice Control Home Automation using PiRelay

Anil Kumar HP,

Thank you for the Nice exploration. Can we use more number of relays, may be 25. For different voice. I am planning for an application very much useful for the society

September 9, 2020 at 11:21am

Leave a comment

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

Please note, comments must be approved before they are published