loading

I wanted get an IR remote input to Raspberry Pi. I manged to get LIRC installed and tested. Everything was ok, except the very last step. When I wanted pass the IR remote Key value to Python program it doesn't pass it correctly. It passes null value for anykey. I couldn't figure out what is wrong. I gave up and then I try to write a python code to capture IR remote without using LIRC.

After some reading about how IR remote communicate the info revealed that uses UART serial communication. I used IR remote DIY Kit HX1838. The IR sensor decodes the IR waves and passes the data serially. What I did was to read the data value coming out of IR sensor serially. This is a crude but a simple way of reading IR remote for simple applications that can be used in Raspberry Pi.

Preparing Raspberry Pi for UART serial communication.

1. Need to remove ttyAMA0 entries in cmdline.txt.

  • First make a backup of the file containing kernel parameters cmdline.txt as cmdline_bp.txt

sudo cp /boot/cmdline.txt /boot/cmdline_bp.txt

  • Edit the file cmdline.txt by removing the parameters containing ‘ttyAMA0‘. ie. ‘console=ttyAMA0,115200′ and ‘kgdboc=ttyAMA0,115200′.

sudo nano /boot/cmdline.txt

The remaining file looks like,

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p6 rootfstype=ext4 elevator=deadline rootwait

Then save and close the editor. Save the file, Ctrl + O. Close the editor, Ctrl + X


2. Update the inittab file to mask the ttyAMA0

sudo nano /etc/inittab

Comment out the line
‘X:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100′

#X:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Then save and close the editor. Save the file, Ctrl + O. Close the editor, Ctrl + X

Step 1: Getting Started


Installing pySerial

  • To get the serial (UART) communication working need to install the Serial module.

sudo apt-get install python-serial

Once this is installed Python code can use it by doing import serial.

  • Next, need to wire GPIO 14 (TX) and GPIO 15 (RX). Since my aim is to receive the IR signals I wired only GPIO 15 (RX).
  • The IR sensor require 5V & GND connection. Then output signal of IR sensor connected to GPIO 15.

The Python code to read the IR signal found out to be very very simple. As follows.

import serial
ser = serial.Serial ("/dev/ttyAMA0")
ser.baudrate  = 2400

for i in range (0,15):     # usually IR signal for a key is about 12-16 bytes
	data = ser.read(1) # read 1 byte at a time
	print ord(data)	   # the data read in character, ord will convert to ASCII value

Now this code will read IR signal 1 byte at a time and prints out the value.

I tried the baud rates by trial and error and settled down for 2400 BPS. Though serial communication support upto 115KBPS it is interesting why IR using a lower speed. My guess is it would be more reliable to use lower speed, since less possibility IR signal loose 1 or 2 bits over the air.


Decode IR remote keys

Now the next step is to decode the key values. I used a standard Samsung TV IR remote for this effort.

First important point is to figure out how many bytes of data for each key. It may vary 12-16 bytes. (the ones I tried). Usually byte length is same for all keys. Those bytes have header bytes, data bytes (to identify key) and tail bytes. The header bytes will have a signature for the model of the IR remote. I used an excel sheet to collect the key data values following Antzy Carmasaic page

https://www.instructables.com/id/How-To-Useemulate-...

Deep diving into the captured key values, it shows byte 0-5 consists of header, repeated for all keys. Byte 6 to 11 data values represent the Key value. There could be some tail values. Byte 12 is tail for the samsung remote.

Mapping keys

The exact way for this remote is to store bytes 6-11 in an array and compare it with a new incoming key. Instead, I did a simple algorithm as follows.

keyidentity = byte[6]+2*byte[7]+3*byte[8]+4*byte[9]+5*byte[10]+6*byte[11]

It gives almost a unique value for every key. You can figure out a better algorithm than this.

I extended the Python code to capture Samsung remote key information. Once I calculated mapped key value then I stored it the python program itself.

File is attached. name - ir_serial3samsung.py. Samsung remote sends 2 sets of data. So I capture 24 bytes in order to flush the Raspberry Pi serial data capture buffer. But I use only 1st set to decode.

When you run this code it correctly identifies the keys pressed. You can decode the rest of the keys in the remote by looking at value "keyidentity" that the program prints out. Then append the program to include them.

Conclusion

This is a very simple and effective way to use a remote control with Raspberry Pi with Python. You need to figure how many total bytes for a key, how long the header bytes, data bytes and tail bytes. Since you would know from A to Z of this process you can easily modify it to suit your application. Since these are small python codes it is very easy to debug if you hit any problem.

<p>Hello, thanks for the feedback. I stepped into one of Instructable page when I looked web for info on raspberry pi. That info was useful. So I thought of sharing this.</p>
<p>So simple, so great! Thanks for sharing and welcome to the community! </p>

About This Instructable

2,626views

27favorites

License:

More by ziath:Raspberry PI remote controlled car Using IR Remote with Raspberry Pi without LIRC 
Add instructable to: