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 their own speech recognition systems which can interact with humans. All these systems had developed a lot in the past decade but still needs lots of developments. 

In this article, we’ll not discuss on 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

  • Raspberry Pi

  • PiRelay

  • USB Mic

Things You Will Learn

  • Raspberry Pi

  • Python 

  • Relay

  • Embedded Systems

Getting Started with the Hardware

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. 

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.


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 a 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.



  • 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 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 SpeechRecognition 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 number 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 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 in 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 you voice-controlled home automation system without depending on any paid services. 

Author Biography.

SB Components
SB Components

SB Components Ltd is a specialist UK manufacturer of protective cases for single board computers and micro controller boards. The company is a team of tech enthusiasts whose dedication and sincerity results in top-notch and class products. Our designers are experts in producing robust, functional and elegant cases that protect and augment technology platforms.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.


CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.


CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.


CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.


News & Blogs


Managing Your PDF Files: 3 Best Converters That...

Managing their PDFs is a hard task for many people, especially if you don’t have any idea w...


PDF Files: 3 Best PDF Converters to Handle Your...

PDF Files: 3 Best PDF Converters to Handle Your PDFs Computers are an essential part of our dail...


3 Reliable and Secure PDF Converters to Convert...

It’s undeniable that computers play an important in many people’s lives. Computers ar...

Top Authors

Hey, I am Suraj - a full-time blogger and a social media expert currently working on the Growth H...

Suraj Kumar

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

Aditya Sehgal is a recognised financial adviser, tech and gadget writer and blogger. Still he has...

Aditya Sehgal

Our Client Says

WhatsApp Chat with Our Support Team