How to setup Infrared Remote Control in Orange pi Zero using lircd and python

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 dont 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

https://www.banggood.com/Orange-Pi-Zero-H2-Quad-Core-Open-source-512MB-Development-Board-p-1110210.html?p=W214159476515201703B

Orange PI Zero Interface board

https://www.banggood.com/Orange-Pi-Zero-Expansion-Board-Interface-Board-Development-Board-p-1115982.html?p=W214159476515201703B

IR Remote

https://www.banggood.com/HX1838-Receiver-NEC-Code-Infrared-Remote-Control-Module-IR-Controller-p-937207.html?p=W214159476515201703B

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 tuturial we are focussing 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 defual so we need to load module ourself.

modprobe sunxi_cir

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 habe
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 show 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

irrecord --list-namespace

The above command will list all the key names which lircd can handle and using the following the instruction 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

Python Lirc:
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 trun off and when you press button ‘2’ it becomes 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[0] == "one":
            os.system("echo 1 > /sys/class/leds/red_led/brightness")
            os.system("echo 0 > /sys/class/leds/green_led/brightness")
 
        elif codeIR[0] == "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 source which I listing below
http://www.lirc.org/
https://piface.github.io/pifacecad/lirc.html#lircrc-or-etc-lirc-lircrc

One thought on “How to setup Infrared Remote Control in Orange pi Zero using lircd and python

Leave a Reply

Your email address will not be published. Required fields are marked *