Capturing IR Remote Commands on Raspberry Pi without LIRC: A Python UART Approach
Integrating an IR remote with a Raspberry Pi is straightforward when you bypass LIRC and tap directly into the UART serial interface. The following guide walks through the entire process—from hardware setup to Python decoding—so you can reliably capture remote signals with minimal code.
Why Skip LIRC?
LIRC provides a generic framework for IR devices, but it can be overkill for simple projects and sometimes introduces latency or compatibility issues. By using the built‑in UART pins (TX/RX) and a low‑speed serial protocol, you achieve a lightweight, deterministic solution that’s easier to debug.
Hardware Overview
- IR Sensor: HX1838 (or any 5V IR receiver) that outputs serial data.
- Raspberry Pi GPIO: Connect the sensor’s output to GPIO 15 (RX). Tie the sensor’s VCC to 5V and GND to a ground pin.
- UART Pin Configuration: Disable the default console on ttyAMA0 so the UART can be used solely for sensor input.
Step 1 – Prepare Raspberry Pi for UART
1. Backup the kernel command line:
sudo cp /boot/cmdline.txt /boot/cmdline_bp.txt
2. Remove ttyAMA0 parameters (e.g., console=ttyAMA0,115200 and kgdboc=ttyAMA0,115200) from /boot/cmdline.txt:
sudo nano /boot/cmdline.txt
Resulting line should look similar to:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p6 rootfstype=ext4 elevator=deadline rootwait
3. Mask the ttyAMA0 login prompt in /etc/inittab:
sudo nano /etc/inittab
Comment out:
#X:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Step 2 – Install pySerial
Install the Python serial library, which provides the serial module for UART communication:
sudo apt-get install python-serial
After installation, you can import it in your script:
import serial
Step 3 – Wiring the IR Sensor
- Connect the sensor’s output pin to GPIO 15 (RX).
- Supply the sensor with 5 V and ground.
- Since the sensor only outputs data, TX (GPIO 14) is unused for this project.
Step 4 – Basic Python Script to Read IR Bytes
The HX1838 transmits a serial stream at 2400 bps. The following snippet reads 12‑16 bytes per key press and prints their ASCII codes.
import serial
ser = serial.Serial('/dev/ttyAMA0')
ser.baudrate = 2400
for _ in range(15): # capture up to 15 bytes
byte = ser.read(1)
print(ord(byte))
Trial and error showed 2400 bps offers the most reliable capture, likely because lower speeds reduce bit‑error probability over the air.
Step 5 – Decoding Samsung Remote Keys
Using a Samsung TV remote, we discovered that each key press yields 12‑16 bytes:
- Bytes 0‑5: Header – constant across all keys.
- Bytes 6‑11: Payload – uniquely identifies the pressed button.
- Byte 12: Tail (specific to this remote).
To map a key, we compute a simple weighted sum of bytes 6‑11:
keyidentity = (byte[6] + 2*byte[7] + 3*byte[8] + 4*byte[9] + 5*byte[10] + 6*byte[11])
While this is not the most robust method, it yields a unique value for each button, making comparison trivial. Feel free to replace it with a hash or XOR if you need stricter collision resistance.
Complete Example: ir_serial3samsung.py
Below is a streamlined script that:
- Captures 24 bytes (two full frames) to flush the serial buffer.
- Uses only the first 12 bytes to decode the key.
- Prints the calculated
keyidentityfor quick reference.
import serial
ser = serial.Serial('/dev/ttyAMA0', 2400)
while True:
# Read 24 bytes to clear buffer
raw = ser.read(24)
# First 12 bytes correspond to a single key press
key_bytes = raw[:12]
key_id = sum((i+1)*b for i, b in enumerate(key_bytes[6:12]))
print(f'Key ID: {key_id}')
Run the script and press buttons on the Samsung remote. Each button press prints a unique Key ID. Store these IDs in a lookup table to map to actions in your application.
Conclusion
Using UART and a simple Python script to capture IR remote signals eliminates the need for LIRC, reduces latency, and offers full control over the decoding logic. Because the code is minimal and transparent, debugging is straightforward. You can adapt this pattern for any IR remote that outputs serial data, making it a versatile solution for Raspberry Pi projects.
For more in‑depth instructions, see the original post: How to Use and Emulate…
Manufacturing process
- How to Read Temperature with a DS18B20 on Raspberry Pi 2
- Measuring Temperature on Raspberry Pi with Maxim 1‑Wire Sensors and DS2482 I2C Bridge
- Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
- Remote Weather Monitoring with Raspberry Pi 3 and PubNub
- Integrate TI SensorTag with Blynk via Node‑RED on Raspberry Pi Zero
- Build a Professional Raspberry Pi Universal Remote with LIRC
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Integrating the Acconeer A111 Pulsed Radar with a Raspberry Pi: A Practical Guide
- Build a Smart Robot with the Bridge Shield – Raspberry Pi & Arduino Integration
- Build a Self‑Balancing Segway with Raspberry Pi