The PiRelay V2 HAT is a solder-less solution to drive 4 Relays (30 Volts DC at 10 Amps) from a Raspberry Pi Single Board Computer. I'll even show you how to control the Relays through Spoken Custom Keywords and Utilise a Touchscreen GUI Control.

Transcript

Hey gang, Tim here at Core Electronics and today we're going to set up the Pi Relay version 2 HAT with a Raspberry Pi single board computer. By the end of this guide you're going to know exactly how to control these four relays through scripts, through voice activation, or through a touchscreen GUI.

Simply, a relay is an electronically operated switch. The four on this HAT are strong enough to switch loads up to 30 volts DC at 10 amps. Relays like this and home automation go hand in hand. All these relays are controlled through I2C communication and these HATs can be stacked for even more relay control.

In this big world there is a whole bunch of high current, high voltage devices. Now a Raspberry Pi Single-Board Computer operates at 5 volts. If 30 volts were driven through it, our computer would break. So does that mean we cannot control high current or high voltage devices with our 5 volt computer? Absolutely not! We can use relays. We can send our relay a signal at 5 volts from our Raspberry Pi. That signal will then open or close the electrical flow to our external high voltage system.

Performing on the table is everything you're going to need. Use any Raspberry Pi single board computer. Here I'm using a Raspberry Pi 4 model B. You're going to need a Pi relay version 2 HAT along with the standoffs that come with it. From here on out you're just going to need everything to run this Raspberry Pi as a desktop computer. We will also need a little Phillips head screwdriver too. We also want an externally powered system for these relays to turn on and off. Here I have a little DC motor connected up to a battery. Whenever I complete the circuit the motor will spin. For voice control we will need a USB microphone like this. For touch screen control we will want a mini GPIO screen like this Waveshare 3.5 inch LCD.

Begin assembly by...

Attach the nylon standoffs to the HAT and then attach the HAT to the top of the Raspberry Pi. Line up the headers of the HAT with the GPIO pins of the Raspberry Pi carefully and push both boards firmly together. Make sure the HAT is fully seated and screw in the two nylon screws. Note the yellow jumpers on the top right of the board. If any of these are pulled off, the corresponding relay will no longer be powered.

Attach the external system to the relay 1 outputs on the hat. If you have no experience working with mains powers, do not hook up mains powers to a relay. This is particularly important in Australia, as 240 volts at 50 Hz AC is life threatening. It is necessary to have credentials, experience, knowledge and certificates if you are going to try and do this. For demonstration purposes, a brushed DC motor is connected directly to a 9 volt battery. Push the two wires together to complete the circuit. Connect the black wire to the COM and the red wire to the normally open of relay one. The external system is connected to the NO and the COM screw.

With power into the system and the boot up wizard complete, you will be welcomed by the Raspberry Pi desktop. The Pi Relay version 2 HAT requires I2C communication to work. By default on Raspberry Pi OS, this communication method is turned off. So, the very first software step is to turn that on. Open up the Raspberry Pi configuration menu found using the top left menu and scrolling over preferences. Then, enable I2C connection found under the interfaces tab. While here, double check that one wire is disabled. If enabled, relay 4 will not operate correctly. Also, check the serial port is enabled. Reboot to lock in these changes.

Now, with your system rebooted and connected to the internet, open up a new terminal window by pressing the black button on the top left of the screen. This terminal window will enable us to download from the internet the exact packages we require. Type and enter the following lines into the terminal to get the packages that you will need. You can also copy and paste these terminal lines one by one from the full written up article linked down in the description. If prompted, type and enter Y to continue the installations. Once completed, we have fully set up our Raspberry Pi Single-Board Computer to work with our Pi relay version 2 pack.

The first python script we are interested in is called test.py. We can find it by jumping down terminals. If instead, you wanted to stop the power going to the external system whenever the relay is activated, you're going to attach your positive red wire to the NC port. In scripts, we will refer to and identify the relays by the numbers written on the PCB board. With that complete, insert a microSD card flash with Raspberry Pi OS and hook it up as a desktop computer. Add a mouse, keyboard and HDMI to a monitor. Then, power up the Raspberry Pi system by plugging in a USB-C connector.

Through the directory structure, you will have example scripts in the same location. Let's open up a python interface like Thonny IDE. Click on the applications menu, which is the Raspberry Pi symbol on the top left of the screen. Hover over the programming tab to find Thonny IDE. Copy and paste the following from the section in the written up article straight into your coding area. Then save the script in the same directory location and run it by pressing the big green run button. As soon as we do, our connected external system will activate for a second before switching off. You would have also been able to hear the noise as each electromechanical relay clicks on and off. You also get this really nice LED light that shows up each time one of them is activated.

Allow me a second to dive through the script so we can see exactly what is going on. The first two lines import all the necessary packages. Here we need time and our downloaded Pi relay package. Then we create variable names to quickly identify and target each relay that we want to have activated. The very next line is a command to toggle on relay 1 as labelled on the Pi relay version 2. R1.on. This activates the relay. Then the next line is a small time delay. Relay 1 will remain energized during this delay. The next line is the command to toggle off relay 1 as labelled on the Pi relay version 2 and that is R1 off. The pattern is then repeated for all relays. Note that all relays upon the end of the Python script are de-energized. A relay left on for a long time will get warm. If your relay is getting hot, the service life of your relay is likely coming to an end. Let's now activate these relays.

Using only our voice, any word or phrase can be spoken to get our external system started. We just need to code it in. For voice control to work, your system will need an internet connection and a connected USB microphone. There is a default example script, named pirelayvoicecontrol.py, located in certain folders. To correctly run this script, we will need to adjust a line so that the script can focus on our specific microphone. The fastest way to figure out what to write here is to run a two-line Python script called miclist.py. As soon as it is run in a phony IDE, it will print out a list of all the connected audio inputs. With my connected USB microphone, the text string that we want starts with USB and can be copied and pasted into our voice control script. After saving and running the Python script, the output will say "Say something". With clear dictation, speak out loud and directly to your Raspberry Pi the phrase "Turn on relay one". As soon as the Google speech recognition has figured out what you said, relay one will turn on. To turn off relay one, say "Turn off relay one". To stop all the relays from being energized, say "Turn off all relays". Now, let's look at the script and create a custom phrase. can use to control your Raspberry Pi you can use a touchscreen like this one.

The first section of the script is very similar to before except we are also importing speech recognition functionality. Variables are created for the relays and along with the connected microphone and sampling rates are decided for the voice recognition. Then an infinite loop is created to constantly listen to our voices. Looking here we can see that the relays are activated only when the correct phrase is said understood accurately and the phrase contains enough trigger words to get deep enough into the if statement trigger. Once it does this it will hit a relay one dot on or a similar statement. Once we reach that statement the Raspberry Pi is going to activate relay one.

To add our custom keyword, let's jump into the script and locate the try section found within the while true infinite loop. This section breaks down the words it has identified from our speech and searches for trigger words. These words are organized in layers of if statements. This format is what I will mimic for our unique keyword. Nosy down to the bottom of these if trees and add a custom one to it. I also will pay close attention to get the correct index. Now let's look at the words let also search for the words it and if it finds those two words then it will turn our relay one on.

So let's save the script and run. Ah indentation error. If I tab in this like so I should have fixed the problem. Having fixed some of those indentations we'll give it another go. Let it rip. Now whenever I say let it rip the Raspberry Pi will recognize the two trigger words let and it, spit out a message to the shell output, energize the relay and most importantly start our DC motor.

Now if you just want a touchscreen that you can use to control your Raspberry Pi you can use a touchscreen like this one. The end of the script has some exceptions that will print out information to the shell to aid in troubleshooting. That will activate our relay.

If you just want a touchscreen that you can click on to easily turn on or off your relays, this is how you're going to do it. Start by attaching a GPIO connected mini touchscreen, like the Waveshare 3.5 inch LCD, to your system. Simply, the GPIO screen can mount directly on top of the SB Components Relay V2 hat, which is mounted onto the Raspberry Pi single board computer. Now, the GUI script can be found from the full written up article and is named PirelayGUITim.py. Open it up and run it in Thonny IDE just like before. The GUI will open up as a full screen window and display on your little screen. We can now simply tap on the screen to activate relays in the system. I also included a shutdown and reboot button. With a connected keyboard, use the keystroke combination ALT F4 to close the GUI window.

The final step is to run this GUI script automatically on boot. We can do this with CronTab. We have a guide on CronTab linked to it down in the description. With that completed, whenever you reboot the Raspberry Pi, it's going to provide this full screen interface. Check the full written up guide if you want to know exactly how to do this.

You now have the ability to turn on and off these relays through scripts, voice and a touch screen GUI. So feel free to plug in a smorgasbord of devices that will all have their power toggled by your Raspberry Pi. Another worthwhile pursuit for relay control would be a Raspberry Pi locally hosted GUI website. This web server would have toggle buttons just like our GUI which we could turn on and off on any locally connected device. The realm of home automation opens up to us when we venture down the path of relay controls and automation.

With Raspberry Pi, it has never been easier. We are full-time makers and always keen to help. Until next time, stay cozy!

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.