As part of Orange Pi zero tutorials I have been writing about getting a rootfilesystem, flashing it to the sd card, remote access to the Orange Pi, LED control from sys interface etc. We all know that the AllWinner chip which the Orange Pi Zero uses has an inbuilt IR controller, but did you know that the Orange Pi Zero Interface board has an IR receiver on board? Yes, you do not need to get your own IR receiver here, everything is there already and all you got to do is get the software part ready and then you are done. For setting up this you need the following hardware.
Orange Pi Zero
Orange PI Zero Interface board
Infrared control in Linux is handled by project LIRC . LIRC allows you to decode and send infra-red signals of many commonly used remote controls. The most important part of LIRC is the lircd daemon, which decodes IR signals received by the device drivers and provides the information on a socket. It also accepts commands for IR signals to be sent if the hardware supports this. The second daemon program called lircmd, will connect to lircd and translate the decoded IR signals to mouse movements. You can e.g., configure X11 to use your remote control as an input device. In this tutorial we are focusing only on the lircd daemon .
root@orangepizero:~# cat /etc/armbian-release BOARD=orangepizero BOARD_NAME="Orange Pi Zero" VERSION=5.25 LINUXFAMILY=sun8i BRANCH=default ARCH=arm IMAGE_TYPE=stable
In the Armbian version mentioned above the IR kernel module for Orange Pi is not loaded default so we need to load the module ourselves.
After loading the module check if the device node is created and if that exist you can test the incoming IR signal.
ls /dev/lirc0 # if the above command runs and waits then press any key in your IR remote pointing it to the OPi IR Receiver mode2 -d /dev/lirc0 space 16777215 pulse 9008 space 4480 pulse 608 space 552 pulse 584 space 544 pulse 584 space 552 pulse 584 space 544 pulse 584
Now change the /etc/lirc/hardware.conf as follows, to get the driver loaded automatically during start.
REMOTE_MODULES="sunxi_cir" REMOTE_DEVICE="/dev/lirc0" REMOTE_LIRCD_CONF="" REMOTE_LIRCD_ARGS="" #Enable lircd START_LIRCD="true" #Try to load appropriate kernel modules LOAD_MODULES="true" DRIVER="default" DEVICE="/dev/lirc0" MODULES="sunxi-cir" # Default configuration files for your hardware if any LIRCMD_CONF="" #to leave this set to "false" FORCE_NONINTERACTIVE_RECONFIGURATION="false" START_LIRCMD=""
Now to reboot the orange Pi and check the lsmod output it should have loaded the kernel module sunxi-cir.
Continuing further we need to configure our remote control keys, you can either copy and paste the configuration as shown below if you have the same remote as I have.
begin remote name /root/lircd.conf.conf bits 16 flags SPACE_ENC|CONST_LENGTH eps 30 aeps 100 header 9037 4486 one 598 1636 zero 598 542 ptrail 608 repeat 9038 2214 pre_data_bits 16 pre_data 0xFF gap 140796 toggle_bit_mask 0x7878 begin codes KEY_CHANNELDOWN 0xA25D KEY_CHANNELUP 0xE21D KEY_PREVIOUS 0x22DD KEY_NEXT 0x02FD KEY_PLAYPAUSE 0xC23D KEY_VOLUMEDOWN 0xE01F KEY_VOLUMEUP 0xA857 KEY_EQUAL 0x906F KEY_0 0x6897 KEY_1 0x30CF KEY_2 0x18E7 KEY_3 0x7A85 KEY_4 0x10EF KEY_5 0x38C7 KEY_6 0x5AA5 KEY_7 0x42BD KEY_8 0x4AB5 KEY_9 0x52AD end codes end remote
Or if you have a different IR remote then you can generate your own conf with the help of below combination.
The above command will list all the key names which lircd can handle and using the following instructions provided by the below command you can create your own conf.
irrecord -d /dev/lirc0 ~/lircd.conf
I am explaining it in this video.
Now restart the lircd as follows.
/etc/init.d/lirc stop /etc/init.d/lirc start
To get the python script working with the lircd get the debian package for python irc from here, https://github.com/tompreston/python-lirc/releases .
wget https://github.com/tompreston/python-lirc/releases/download/v1.2.1/python-lirc_1.2.1-1_armhf.deb dpkg -i python-lirc_1.2.1-1_armhf.deb
And the create .lircrc in your home directory.
begin prog = myprogram button = KEY_1 config = one end begin prog = myprogram button = KEY_2 config = two end
I have just created only for two keys.
Blink LED Demo python script:
In this demo, if you press the button ‘1’ then the red led will turn on and green will turn off and when you press button ‘2’ it is vice versa.
#!/usr/bin/env python #coding: utf8 import lirc import time import os sockid=lirc.init("myprogram") while True: codeIR = lirc.nextcode() print codeIR if codeIR != : if codeIR == "one": os.system("echo 1 > /sys/class/leds/red_led/brightness") os.system("echo 0 > /sys/class/leds/green_led/brightness") elif codeIR == "two": os.system("echo 0 > /sys/class/leds/red_led/brightness") os.system("echo 1 > /sys/class/leds/green_led/brightness")
I got the information from various sources which I am listing below,
- https://piface.github.io/pifacecad/lirc.html#lircrc-or-etc-lirc-lircrc .