I have previously published the first part of this topic which is about interfacing with a RF-based home security system. Later on, I found a relatively cheap 220V relay that works on the same 433 MHz frequency and decided to purchase one from AliExpress to try out. It was a single-channel relay which means it has only one switch. These could be controlled with a key fob, or for advanced use cases, a Raspberry Pi computer that can emit RF signals.
Note that there could be different relays in the market which look and behave different to the one I’ve used and this project may not work for all. The one I bought came with a black plastic housing which is not pictured here.
On one side, it can be connected to AC current and on the other side any electrical appliance can be connected. I decided to use a simple lamp (had to chop off the perfectly good power cable for it though).
The RF relay can be programmed by using the push button that’s on it. Now be ⚠ VERY CAREFUL ⚠ and practice caution when working with AC current. I used a plastic barrel from a pen to push the button so that I won’t touch the electronic components with my finger accidentally.
The relay operates in various modes such as momentary, toggle and latched.
- Momentary: Switch is ON as long as the signal is being received.
- Toggle: Switch can be set to ON and OFF by sending the same signal once for each state.
- Latched: One signal is used to switch ON and another to switch OFF.
It could be possible to program the relay to use all three modes as long as you’re using four different signals, but I didn’t try that out.
I decided to go for the latched mode to use two signals so it’s more reliable. If I used toggle mode, then for some reason if the signal was sent twice unintentionally, then the switch will turn on and then off.
The below video that I found proved to be very useful despite having zero spoken words and the fact that it uses a DC relay.
To reset the relay, the button has to be pressed 8 times, and the LED will blink rapidly for a few seconds to confirm the erasure. To get into the latched mode programming, the button has to be pressed thrice afterwards. The LED will light up and the ON signal can then be sent from the RPi. It will blink rapidly for a second and light up again. Now the OFF signal must be sent. Once that’s done, the LED will go off and the relay is now programmed.
Deciding what signal to send out is entirely up to you. I have used two codes as below but you can use whatever that works.
Pulse length: 300, Protocol: 1 and code: 4096 to switch ON.
Pulse length: 300, Protocol: 1 and code: 8192 to switch OFF.
The signals are sent via the rpi-rf library like how it’s done in part 1 of this project.
python3 /home/xxxxxx/send.py -p 300 -t 1 4096
python3 /home/xxxxxx/send.py -p 300 -t 1 8192
When the relay is in listening mode, send the ON signal followed by the OFF signal. If all goes well, the relay will be programmed with these codes. Try a few times sending out the signals to see if it activates and deactivates correctly. These two commands can be saved as shell scripts for easier usage and gives the possibility to be scheduled using cron.
This will be the starting point in exploring how household appliances that run on AC current can be automated by using an RPi. With the previous experiments done on receiving RF signals on the RPi, there are many simple and advanced projects that could be tried out. For example, you can set it up to switch on the lights in a room when the door opens or if motion is detected by a sensor. Or you can build a system where you can switch on or off things like fans/ACs from anywhere in the world!
Hope you learned something from this article and thanks for reading!