Do you have five, 10, or even 15-year-old consumer electronic devices that still work but are not connected to the internet and don't get software updates from the manufacturer? Should you just swap those tired, old devices for new Internet of Things-enabled versions—even though they still work? Of course not! The rise of the open source hardware movement and the availability of free and open source software allows us to bring new life into our retro electronic devices. With off-the-shelf components, we can reduce electronic waste and bring our old TV, stereo receiver system, or air conditioner into the IoT era.
Converting a Raspberry Pi into a smart remote control using the open source add-on board ANAVI Infrared pHAT and open source software is easy.
I created ANAVI Infrared pHAT as a hobby project using the free and open source software KiCAD. The board supports scanning remote controls and sending commands using Linux Infrared Remote Control (LIRC). Furthermore, various external sensors can be attached easily. All the schematics are available at GitHub under the CC BY-SA 3.0 license. Thanks to a couple of crowdfunding campaigns with IndieGoGo and Crowd Supply, I can mass produce the board in a local factory in my hometown Plovdiv, Bulgaria, one of the oldest and most beautiful cities in Europe.
Understanding infrared remote controls
Most remote controls send digitally coded pulses of infrared radiation to control functions of consumer electronic devices. The infrared's wavelength is longer than visible light's. The infrared LED emits pulses that are received and recognized by an infrared sensor in the target device. Although infrared is invisible to the human eye, a remote control still needs direct line of sight to operate.
The remote controls of TVs, set-top boxes, DVD players, and stereo systems send simple codes to activate certain device functions. Remote controls for air conditioning systems are far more complex, as they store and send the whole state. Even if the user presses a single key, for example to change the temperature of the air conditioner from 20° to 21°C, the remote control sends a stream of digitally coded pulses for all settings.
In this article, I'll describe how to use a Raspberry Pi Zero W, an ANAVI Infrared pHAT, and the popular open source software LIRC to create a remote control system that can turn my retro stereo system on and off.
I still use my Sony MHC-GR7, a well-known mini Hi-Fi component system from the mid-1990s, to listen to FM radio, plus I am personally attached to it. Now is the perfect time to modernize and bring life back into it.
Assembling the hardware
Raspberry Pi has neither an infrared receiver nor a transmitter, therefore it needs additional hardware to make this work. The ANAVI Infrared pHAT was specifically designed to transform a Raspberry Pi into a smart remote control.
ANAVI Infrared pHAT features two 5mm infrared LEDs for transmitting signals and an infrared receiver from Vishay Semiconductors. There are also slots for up to three plug-and-play sensor modules, UART pins for debugging, EEPROM with board manufacturer information, and a device tree fragment.
The infrared LEDs are connected to pin 11 of the Raspberry Pi. Although most remote controls use a single emitting diode, ANAVI Infrared pHAT has two infrared LEDs to create a stronger signal. The infrared receiver is connected to pin 12.
ANAVI Infrared pHAT works with any model and version of Raspberry Pi with a 40-pin header, including Raspberry Pi 3, Raspberry Pi 2, Raspberry Pi A+, Raspberry Pi B+, Raspberry Pi 0, and Raspberry Pi 0 W. Assembly is very easy. No soldering is required; just plug the ANAVI Infrared pHAT on the top of a Raspberry Pi with your bare hands.
Installing LIRC
LIRC is handy free and open source software that has been around for almost 20 years. It works pretty well on popular GNU/Linux distributions, including Raspbian for Raspberry Pi.
In August 2017, the Raspberry Pi Foundation announced Stretch, the next major release of Raspbian. Raspbian is based on Debian, so it follows its two-year release cycle. Debian releases are named after characters from Disney Pixar's Toy Story films, and Stretch is a purple octopus. I've tested the instructions in this article on Raspbian Stretch with LIRC 0.9.4c. If you are using another version of LIRC or Raspbian, some configurations might need additional adjustments.
The latest version of Raspbian is available for download at raspberrypi.org. After booting Raspbian, install LIRC by opening a terminal and executing these two commands:
sudo apt-get update
sudo apt-get install -y lirc
After LIRC is installed, it's time to configure it. The process is a little bit annoying, but luckily it has to be done just once. Open the file /etc/modules with a text editor and add the following lines to the end of the file to configure the pins on Raspberry Pi that control the infrared receiver and transmitter:
lirc_dev
lirc_rpi gpio_in_pin=18 gpio_out_pin=17
The next step is to create file etc/lirc/hardware.conf and add the following content:
# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"
#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD=false
#Don't start irexec, even if a good config file seems to exist.
#START_IREXEC=false
#Try to load appropriate kernel modules
LOAD_MODULES=true
# Run "lircd --driver=help" for a list of supported drivers.
#DRIVER="UNCONFIGURED"
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"
# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""
To make ANAVI Infrared pHAT work on Raspbian, another file needs to be modified. Open /etc/lirc/lirc_options.conf and make sure that driver and the device are set to the following values:
driver = default
device = /dev/lirc0
It's almost done! The penultimate step is add a line to the end of file /boot/config.txt to configure the device tree overlay:
dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17
Finally, just reboot Raspberry Pi. That's it! Now it's time to scan various remote controls so the new remote learns their commands.
Scanning a remote control
Different manufacturers use different pulses of the remote control for each function, so each remote control needs to be scanned. LIRC provides a command-line interface for reading and configuring signals from a remote control.
For the sake of simplicity, I will just describe scanning my retro Sony stereo system remote control's power key. First, I need to stop the LIRC systemd service and create a new LIRC control configuration file by executing the following two commands:
sudo systemctl stop lircd
irrecord -d /dev/lirc0 ~/lircd.conf
Following the onscreen instructions, I have to set the name of the remote control's configuration file:
Enter name of remote (only ascii, no spaces) :hifi
Using hifi.lircd.conf as output filename
The next step is to scan a button. In this example, I am interested in the Power button, which I will save under the name KEY_POWER:
Please enter the name for the next button (press <ENTER> to finish recording)
KEY_POWER
Now hold down button "KEY_POWER".
At the end, irrecord should save the scanned key in a file with the name that we set and display a message. For example:
Successfully written config file hifi.lircd.conf
Next, back up the original LIRC configuration file and replace it with the file that was just created. Finally, restart the LIRC systemd service. All these operations can be performed with the following three commands:
sudo mv /etc/lirc/lircd.conf /etc/lirc/lircd-backup.conf
sudo mv hifi.lircd.conf /etc/lirc/lircd.conf
sudo systemctl start lircd
Sending infrared commands
Unlike scanning a remote control, sending infrared signals with LIRC is very easy. First list all available commands for a saved remote control. For the sake of simplicity in this demo, in the previous step a single remote control button was saved as a configuration named hifi. To turn the stereo system on and off with the remote control, execute the following command in a terminal:
irsend SEND_ONCE hifi KEY_POWER
What's next?
Typing commands in a terminal is definitely not user friendly. Open source software for home automation comes to the rescue! Home Assistant and OpenHAB 2 are among the popular open source solutions that provide a good user experience and a responsible user interface that works well on both smartphones and personal computers. Both can be installed directly on a Raspberry Pi.
To learn more about the open source ANAVI Infrared pHAT hardware project, visit the project website and look for more examples and details in the user's manual and GitHub page.
Learn more in Leon Anavi's talk, Free and Open Source Software Tools for Making Open Source Hardware, at Open Source Summit EU, which will be held October 23-26 in Prague.
2 Comments