Authorized Online Retailers:

AMAZON
AMAZON-jp
amzuk

Introduction
In this lesson, we’ll introduce how to decode infrared signals with raspberry pi and remote 3 LEDs with IR remote.

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

For python users


Learn more about Python, please go to Raspberry Pi Starter Kit lesson 4

Project  1)  Get the IR code of different keys
Step 1) by typing following command in Pi terminal

cd ~

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

 

Note: You can also use nano editor to copy and paste code into IRtest1.pyIRtest2.pyIRtest3.py by typing command:
sudo nano IRtest1.py
 

 

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.

Python Code Explanation:

irexec executable file is a program which comes with LIRC package. When irexec starts running, it will look up key events handling program in  .lircrc  file.  In this example, .lircrc defined that KEY_1 event will be handled by python program /home/pi/IRtest1.py , KEY_2 by /home/pi/IRtest2.py, and KEY_3 by /home/pi/IRtest3.py  . The details of these 3 python program are as following:

IRtest1.py,

"""
$      If KEY_1 is pressed,this script will be executed,LED1 will turn on(or off)
$      LED1 connect to GPIO5(BCM_GPIO 24)
"""
import RPi.GPIO as GPIO

PIN = 24

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(PIN, GPIO.IN)
GPIO.setup(PIN, GPIO.OUT)

if GPIO.input(PIN) == 0:
     GPIO.output(PIN, GPIO.HIGH)
else:
     GPIO.output(PIN, GPIO.LOW)

IRtest2.py

"""
$      If KEY_2 is pressed,this script will be executed,LED1 will turn on(or off)
$      LED2 connect to GPIO6(BCM_GPIO 25)
"""
import RPi.GPIO as GPIO

PIN = 25

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(PIN, GPIO.IN)
GPIO.setup(PIN, GPIO.OUT)

if GPIO.input(PIN) == 0:
     GPIO.output(PIN, GPIO.HIGH)
else:
     GPIO.output(PIN, GPIO.LOW)

IRtest3.py

"""
$      If KEY_3 is pressed,this script will be executed,LED1 will turn on(or off)
$      LED3 connect to GPIO7(BCM_GPIO 4)
"""
import RPi.GPIO as GPIO

PIN = 4

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(PIN, GPIO.IN)
GPIO.setup(PIN, GPIO.OUT)

if GPIO.input(PIN) == 0:
     GPIO.output(PIN, GPIO.HIGH)
else:
     GPIO.output(PIN, GPIO.LOW)