Authorized Online Retailers:

AMAZON
AMAZON
amzuk

Introduction
In this lesson, we’ll introduce how to decode infrared signals with raspberry pi and    turn ON/OFF 3 LEDs remotely.

In many Raspberry Pi blogs, people often use a so-called LIRC library to handle IR signal. However, LIRC library installation is a little bit complicated and sometimes doesn’t work well after Raspbian OS upgrades to new version.

To help beginners to program Infrared Remote signal smoothly, in this tutorial, we only use Raspberry Pi basic GPIO functions instead of LIRC.  So you no need worry about LIRC headache, simply download and run our code file in Python or C++, you will get nice result.

Hardware Preparation
1 * Raspberry Pi
1 * Breadboard
1 * IR Receiver(VS1838B)
1 * Infrared Remote Controller
3 * LED
3* Resistor(200Ω)
Several jumper wires
1 * T-Extension Board with 40-Pin Cable(Optional)

Software Preparation

Note:

1. In this lesson, we remotely control raspberry pi via PuTTy on PC. To learn how to config raspberry pi, please visit lesson 1: getting started with raspberry pi.

2. When the remote controller can’t work, please connect your raspberry Pi with Ethernet cable

Experimental Principle
In this lesson, we use VS1838B IR receiver which can convert incoming IR signal into digital data output. as photo shows:

Schematic diagram of VS1838B with raspberry pi:
Note: Learn more about GPIO of raspberry pi, please review our lesson 2: Introduction Of Raspberry Pi GPIO

Hardware Setup

Software in Python


We will use Python to program the IR receiver. Learn more about Python, please go to Raspberry Pi Starter Kit lesson 4

Step 1) If you have not enabled I2C, please run following command to enable I2C.

sudo raspi-config

Then select Interfacing Options->I2C->Yes->Ok->Finish

Step 2) Download and  by typing following two commands in Pi terminal

wget -q http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/irtest.py -O irtest.py

Step 3) run the python program by typing following command

python irtest.py

Step 4) Program result
Once the program is running, when you press 1 of remote controller at IR receiver, the first LED will turn off if it is on, or the first LED will turn on if it is off. when you press 2, and 3, you will see the second LED and third LED as the same.

Step 5) Stop the program by typing Ctrl-C in your terminal. After Ctrl-C , you also need to press any key in the IR remote, then your Pi Terminal will show :

Ctrl-C pressed!
Quitting

Python Code explanation( irtest.py):

When a key in IR remote is pressed, it will send a unique Hex code to IR receiver which connected to the Pi board.
In line 11,12,13 of irtest.py, we defined 3 variables KEY_1,KEY_2,KEY_3 as following:

KEY_1=0xff30cf
KEY_2=0xff18e7
KEY_3=0xff7a85

These variables store the Hex value of each Number key 1,2,3.
In line 76 to 99 of irtest.py, program read Hexcode from IR receiver and take proper action accordingly as following:

            code = on_ir_receive(IR_PIN)
            if (code==KEY_1):
                if (status1==0):
                   status1=1
                   GPIO.output(LED1, GPIO.HIGH)
                else:
                   status1=0
                   GPIO.output(LED1, GPIO.LOW)
            elif (code==KEY_2):
                if (status2==0):
                   status2=1
                   GPIO.output(LED2, GPIO.HIGH)
                else:
                   status2=0
                   GPIO.output(LED2, GPIO.LOW)
            elif (code==KEY_3):
                if (status3==0):
                   status3=1
                   GPIO.output(LED3, GPIO.HIGH)
                else:
                   status3=0
                   GPIO.output(LED3, GPIO.LOW)
            else:
                print("Invalid code")

Can I use my TV or air conditioner remote to control the LED?

Yes. You can definitely do this.
First you have to know the key Hex code in your TV remote, please download and run an ircode.py program by following command:

wget -q http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/ircode.py -O ircode.py
python ircode.py

After running above command, you will see:
Starting IR Listener
Waiting for signal
After you press a KEY in IR remote, you will see the Hex code (starting with 0x) of that key. For example, when you press Num 1 key, you will see:

0xff30cf

Press your TV remote and find the correct TV key hexcode, use these code to replace the hexcode in line 11-13 in irtest.py, then your program will react to your TV keys(to edit irtest.py, you can use nano irtest.py command).

Software in C++

You can use following three commands to implement same project in C++ language. The circuit connection and test methods are same as Python program.

wget -q http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/irtest.cpp -O irtest.cpp
g++ -Wall -o irtest irtest.cpp -lwiringPi
./irtest