1.10 Testing and Using the Infrared Sensor on Smart Pi One¶
Introduction¶
This guide explains how to activate and test the infrared sensor on the Smart Pi One board running Armbian. We will also see how to use this sensor with Python and a more engaging C program.
Activating the Infrared Sensor¶
Step 1: Activate in Armbian-Config¶
-
Open
Armbian-Config
via an SSH interface or a terminal window: -
Choose
System
: -
Select
Hardware
: -
Then
cir
,Save
andBack
: -
To finish:
Reboot
to apply the changes:
Step 2: Install ir-keytable¶
Start by installing ir-keytable
, a tool that allows you to manage the infrared sensor:
Step 3: Configure Infrared Protocols¶
To activate the infrared protocols, use the following command:
echo "+rc-5 +nec +rc-6 +jvc +sony +rc-5-sz +sanyo +sharp +mce_kbd +xmp" | sudo tee /sys/class/rc/rc0/protocols
Testing the Infrared Sensor¶
To test the infrared sensor using a TV remote or a LED strip remote, follow these steps:
-
Ensure that the infrared sensor is properly connected to the Smart Pi One.
-
Open a terminal and run the following command:
-
Point the infrared remote (like your TV or LED strip remote) at the sensor and press a button.
-
Each time a button is pressed, events will appear in the terminal, indicating that the sensor is detecting the infrared signals.
-
You will see different codes for each button press, confirming that the sensor is working properly.
-
To stop the test, press
CTRL-C
.
Using Python¶
Prerequisites¶
Before starting, ensure you have installed the python-evdev
module, which allows you to read events from input devices:
Example Python Script¶
Here’s a complete script to read infrared events from the sensor:
import subprocess
import evdev
from evdev import InputDevice
import time
# Set the IR protocols
subprocess.run("echo '+rc-5 +nec +rc-6 +jvc +sony +rc-5-sz +sanyo +sharp +mce_kbd +xmp' | sudo tee /sys/class/rc/rc0/protocols", shell=True)
# Replace '/dev/input/eventX' with the correct path for your infrared sensor
device_path = '/dev/input/event1' # Update this line
# Attempt to access the device
device = InputDevice(device_path)
print("Waiting for infrared events. Press CTRL-C to quit.")
try:
while True:
event = device.read_one() # Read a single event
if event:
print(event) # Print the raw event data
except KeyboardInterrupt:
print("\nScript stopped.")
except Exception as e:
print(f"An error occurred: {e}")
Creating and Running the Script¶
-
Open a text editor, such as
nano
, and create the Python file: -
Paste the script into the file and save it.
-
Execute the script with the necessary permissions (you may need to use
sudo
):
Using a C Program¶
Example C Program¶
Here’s a more engaging example in C that will listen for infrared events and print a message when specific keys are pressed:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
char command[100];
snprintf(command, sizeof(command), "ir-keytable -t");
printf("Listening for infrared remote control events. Press CTRL-C to exit.\n");
// Use a system call to execute ir-keytable in a loop
while (1) {
system(command);
sleep(1); // Add a small delay to avoid overwhelming the output
}
return 0;
}
Compilation and Execution¶
-
Open a text editor, such as
nano
, to create the C file: -
Paste the C program into the file and save it.
-
Compile the program with:
-
Run the program:
You now have all the information needed to activate, test, and use the infrared sensor on the Smart Pi One board. Whether using Python or C, you can easily interact with your remote control.