How to use an IR-Infrared Receiver with a Raspberry Pi board
Hello and welcome back, in this tutorial, we will learn how to use an IR receiver with the Raspberry Pi board. For that, I used the Raspberry Pi 2 Model B board. You can use any other Raspberry Pi board Model. Also, I used the IR Remote kit and two-channel relay module for this project. But, you can test this project without a relay module. We can try this project on any kind of IR transmitter remote, such as TV remotes, radio remotes, fan remotes, or any other compatible models you may have on hand.
What’s the IR receiver?
The IR receiver is designed to capture signals transmitted by an IR transmitter. This little component includes a 38 KHz demodulator, which is like its secret decoder. Therefore, we can receive signals well within 10 meters. It’s like having a super-sensitive ear for remote control signals. If you want to know more information, please use this link for that.
- If you want to do this project with Raspberry PI Pico board – Click on me
OK, let’s do this project step by step. The required components are given below.
- Raspberry Pi 2 Model B board x 1 — Our store / Amazon
- IR Receiver and Transmitter x 1 — Our store / Amazon
- 2 channel Relay module x 1 — Our store / Amazon
- Jumper wires — Our store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Secondly, connect the relay module and IR receiver to the Raspberry Pi board. In this case, you can use the Raspberry Pi GPIO extension board and cable. I didn’t use these components.
Step 3
Thirdly, connect the mouse, keyboard, monitor, internet cable, and SD card(OS installed) to the Raspberry Pi board. And then, connect a suitable 5V power supply.
Step 4
Next, open the terminal on your Raspberry Pi computer and update it. For that, run the following commands one by one. In this case, you have to turn on the internet connection.
- sudo apt-get update
- sudo apt-get upgrade
Step 5
Now, edit the config file. For that, run the following command on the terminal.
- sudo nano /boot/config.txt
- Next, uncomment this line to enable infrared communication.
- dtoverlay=gpio-ir,gpio_pin=17
- Now, save this file and reboot your computer.
- sudo reboot
Step 6
OK, let’s install the ir-keytable library file and temporarily enable all protocols. For that, open the terminal and run the following commands one by one.
- sudo apt-get install ir-keytable
- sudo ir-keytable -p all
Step 7
Next, install the python3 interface.
- sudo apt-get install python3-pip
Step 8
Now, install the evdev library file and evtest package.
- sudo pip3 install evdev
- sudo apt-get install evtest
Step 9
Next, run the following command on the terminal and press number 4. Now you can test your circuit. For that, click any button on the IR remote.
- sudo evtest
Step 10
Now, let’s receive the IR remote values. For that, copy and paste the following script on the Thorny IDE.
- Scripts and circuit diagram — Download
import RPi.GPIO as GPIO
import time
import evdev
def get_ir_values():
devices =[evdev.InputDevice(path) for path in evdev.list_devices()]
for device in devices:
if(device.name == "gpio_ir_recv"):
print("Using device",device.path,"\n")
return device
print("No device found!")
dev = get_ir_values()
time.sleep(1)
events = dev.read()
try:
event_list = [event.value for event in events]
print("Receved command:",event_list)
except BlockingIOError:
print("No commands received. \n")
while True:
event = dev.read_one()
if event:
print(event.value)
- Next, run this script and press two buttons as you like.
Step 11
Now, open the main script and copy and paste the IR values(You got earlier) into the if condition. Finally, click the run button.
import RPi.GPIO as GPIO
import time
import evdev
Relay1 = 23
Relay2 = 24
button1 = True
button2 = True
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Relay1,GPIO.OUT)
GPIO.setup(Relay2,GPIO.OUT)
GPIO.output(Relay1,GPIO.HIGH)
GPIO.output(Relay2,GPIO.HIGH)
def get_ir_values():
devices =[evdev.InputDevice(path) for path in evdev.list_devices()]
for device in devices:
if(device.name == "gpio_ir_recv"):
print("Using device",device.path,"\n")
return device
print("No device found!")
dev = get_ir_values()
time.sleep(1)
events = dev.read()
try:
event_list = [event.value for event in events]
print("Receved command:",event_list)
except BlockingIOError:
print("No commands received. \n")
while True:
event = dev.read_one()
if event:
print(event.value)
if(event.value == ****):
if button1:
GPIO.output(Relay1,GPIO.LOW)
button1 = False
elif (button1 == False):
GPIO.output(Relay1,GPIO.HIGH)
button1 = True
elif(event.value == ****):
if button2:
GPIO.output(Relay2,GPIO.LOW)
button2 = False
elif (button2 == False):
GPIO.output(Relay2,GPIO.HIGH)
button2 = True
Now, you can test this project. The full video guide is below. So, we hope to see you in the next tutorial or project.
Troubleshoot
- Check the jumper wires.
- Check your hardware components.
- Check your script again.
How to use an IR-Infrared Receiver with a Raspberry Pi board