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
Install LIRC(Linux Infrared remote control),LIRC is a package that allows you to decode infrared signals of many (but not all) commonly used remote controls. LIRC runs as a daemon that will decode IR signals received by the device drivers and provide the information on a socket. We will then write a program in the user space to monitor this socket for input events using the LIRC client library. To learn more about LIRC, please click here.

1) Install LIRC and client libraries by typing following command in terminal
sudo apt-get install lirc liblircclient-dev

2)Set LIRC
a) Run the following command to edit /etc/modules
sudo  nano /etc/modules

Add the following lines in /etc/modules, and then press “ctrl” + “X” and then press “y” to save the file, at last, press “enter” to exit
lirc_dev
lirc_rpi gpio_in_pin=18 gpio_out_pin=17

b) Run the following command to edit /boot/config.txt
sudo  nano  /boot/config.txt

Add the follow line at the end of the file,  and then press “ctrl” + “X” and then press “y” to save the file, at last, press “enter” to exit
dtoverlay=lirc-rpi:gpio_in_pin=18 gpio_out_pin=17

c) Run the following command to edit /etc/lirc/hardware.conf :
sudo nano /etc/lirc/hardware.conf

Please revise the text as the second photo:
The First Photo

The second photo

d) Download (remote configuration file)lircd.conf to specified direction
cd ~
wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/lircd.conf
sudo cp lircd.conf /etc/lirc/

e) Run reboot to reboot raspberry pi to make the software be available
reboot

3) Test IR receiver
a. Run the following command to check whether lirc is loaded or not
ls /dev/li*

If lirc is loaded, the terminal will show as following:

If it isn’t loaded, please check the setups.

b.Run the following command to check whether the keys of remote controller works or not
sudo /etc/init.d/lirc restart
irw


If you get the error as the above photo, please run the following command again.
sudo /etc/init.d/lirc restart

irw

And then press any key of controller and the terminal will show as the following photo:

If it doesn’t show as above, Please check the setup.

Compile program
In this lesson, we’ll provide codes of C language and Python language. New starters can select one according to their interest or habit.

For C language users


Please be sure installation wiringpi. If you have not installed wiringPi, read  installation wiringpi

Step 1) Download IRtest.c by typing following commands in pi terminal:
cd ~
wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/IRtest.c

Note: You can also use also use  nano editor to copy and paste code into IRtest.c by typing command:
sudo nano IRtest.c

Step 2 Compile Code
sudo gcc -Wall -o IRtest IRtest.c -lwiringPi -llirc_client

Step 3 Before running the file, please create a lircrc file at the path of /etc/lirc/, or you will get back error message.
sudo touch /etc/lirc/lircrc

4) Run program
sudo /etc/init.d/lirc restart
sudo ./IRtest

If you get the error as the above photo, please run the following command again.
sudo /etc/init.d/lirc restart
sudo ./IRtest

And then press key 1, key 2, key 3 of controller to turn 3 leds on or off  and the terminal will show as the following photo:
Press ctrl +c to stop the program

5) Test project result
Once the program is running, when you press key 1 of remote controller, the first LED will switch between ON and OFF. When you press key 2, or key 3, the second and third LED will do the same thing.

C language code Analysis

IRtest.c
 
  1. #include < wiringPi.h>
  2. #include < errno.h>
  3. #include < stdio.h>
  4. #include < stdlib.h>
  5. #include < string.h>
  6. #include < lirc/lirc_client.h>
  7. #include < time.h>
  8. void flipLED (int led);
  9. //The WiringPi pin numbers used by our LEDs
  10. #define LED1 4
  11. #define LED2 5
  12. #define LED3 6
  13. #define ON 1
  14. #define OFF 0
  15. int main(int argc, char *argv[])
  16. {
  17. struct lirc_config *config;
  18. //Timer for our buttons
  19. int buttonTimer = millis();
  20. char *code;
  21. char *c;
  22. //Initiate WiringPi and set WiringPi pins 4, 5 & 6 (GPIO 23, 24 & 25) to output. These are the pins the LEDs are connected to.
  23. if (wiringPiSetup () == -1)
  24. exit (1) ;
  25. pinMode (LED1, OUTPUT);
  26. pinMode (LED2, OUTPUT);
  27. pinMode (LED3, OUTPUT);
  28. //Initiate LIRC. Exit on failure
  29. if(lirc_init("lirc",1)==-1)
  30. exit(EXIT_FAILURE);
  31. //Read the default LIRC config at /etc/lirc/lircd.conf This is the config for your remote.
  32. if(lirc_readconfig(NULL,&config,NULL)==0)
  33. {
  34. //Do stuff while LIRC socket is open 0=open -1=closed.
  35. while(lirc_nextcode(&code)==0)
  36. {
  37. //If code = NULL, meaning nothing was returned from LIRC socket,
  38. //then skip lines below and start while loop again.
  39. if(code==NULL) continue;{
  40. //Make sure there is a 400ms gap before detecting button presses.
  41. if (millis() - buttonTimer > 400){
  42. //Check to see if the string "KEY_1" appears anywhere within the string 'code'.
  43. if(strstr (code,"KEY_1")){
  44. printf("MATCH on KEY_1\n");
  45. flipLED(LED1);
  46. buttonTimer = millis();
  47. }
  48. else if(strstr (code,"KEY_2")){
  49. printf("MATCH on KEY_2\n");
  50. flipLED(LED2);
  51. buttonTimer = millis();
  52. }
  53. else if(strstr (code,"KEY_3")){
  54. printf("MATCH on KEY_3\n");
  55. flipLED(LED3);
  56. buttonTimer = millis();
  57. }
  58. }
  59. }
  60. //Need to free up code before the next loop
  61. free(code);
  62. }
  63. //Frees the data structures associated with config.
  64. lirc_freeconfig(config);
  65. }
  66. //lirc_deinit() closes the connection to lircd and does some internal clean-up stuff.
  67. lirc_deinit();
  68. exit(EXIT_SUCCESS);
  69. }
  70. void flipLED (int led)
  71. {
  72. //If LED is on, turn it off. Otherwise it is off, so thefore we need to turn it on.
  73. if(digitalRead(led)==ON)
  74. digitalWrite(led, OFF);
  75. else
  76. digitalWrite(led, ON);
  77. }

For python users


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

Step 1) Download IRtest1.pyIRtest2.py and IRtest3 .py sample code by typing following command in Pi terminal
cd ~
wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/IRtest1.py
wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/IRtest2.py
wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_19/IRtest3.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
sudo nano IRtest2.py
sudo nano IRtest3.py

2) please create a .lircrc file at the path of /home/pi
cd ~
sudo nano ./.lircrc

Copy the following code and paste in .lircrc, and then press “ctrl” + “x”, and then press “y” to save the file, and then press “enter” to exit.

.lircrc
 
  1. #terminal runs irexec command when you press button 1 /home/pi/IRtest1 will be executed
  2. begin
  3. prog = irexec
  4. button = KEY_1
  5. config = sudo python /home/pi/IRtest1.py
  6. end
  7. begin #terminal runs irexec command when you press button 2 /home/pi/IRtest2 will be executed
  8. prog = irexec
  9. button = KEY_2
  10. config = sudo python /home/pi/IRtest2.py
  11. end
  12. begin #terminal runs irexec command when you press button 3 /home/pi/IRtest3 will be executed
  13. prog = irexec
  14. button = KEY_3
  15. config = sudo python /home/pi/IRtest3.py
  16. end

Or run the following command to download the .lircrc file

wget http://osoyoo/driver/pi3_start_learning_kit_lesson_19/.lircrc

3) Run program
sudo /etc/init.d/lirc restart
irexec

If you get the error as the above photo, please run the following command again.
sudo /etc/init.d/lirc restart
irexec

And then press key 1, key 2, key 3 of controller to turn 3 leds on or off  but the terminal will show nothing:

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,

IRtest1.py
 
  1. """
  2. $ If KEY_1 is pressed,this script will be executed,LED1 will turn on(or off)
  3. $ LED1 connect to GPIO5(BCM_GPIO 24)
  4. """
  5. import RPi.GPIO as GPIO
  6. PIN = 24
  7. GPIO.setmode(GPIO.BCM)
  8. GPIO.setwarnings(False)
  9. GPIO.setup(PIN, GPIO.IN)
  10. GPIO.setup(PIN, GPIO.OUT)
  11. if GPIO.input(PIN) == 0:
  12. GPIO.output(PIN, GPIO.HIGH)
  13. else:
  14. GPIO.output(PIN, GPIO.LOW)

IRtest2.py

IRtest2.py
 
  1. """
  2. $ If KEY_2 is pressed,this script will be executed,LED1 will turn on(or off)
  3. $ LED2 connect to GPIO6(BCM_GPIO 25)
  4. """
  5. import RPi.GPIO as GPIO
  6. PIN = 25
  7. GPIO.setmode(GPIO.BCM)
  8. GPIO.setwarnings(False)
  9. GPIO.setup(PIN, GPIO.IN)
  10. GPIO.setup(PIN, GPIO.OUT)
  11. if GPIO.input(PIN) == 0:
  12. GPIO.output(PIN, GPIO.HIGH)
  13. else:
  14. GPIO.output(PIN, GPIO.LOW)

IRtest3.py

IRtest3.py
 
  1. """
  2. $ If KEY_3 is pressed,this script will be executed,LED1 will turn on(or off)
  3. $ LED3 connect to GPIO7(BCM_GPIO 4)
  4. """
  5. import RPi.GPIO as GPIO
  6. PIN = 4
  7. GPIO.setmode(GPIO.BCM)
  8. GPIO.setwarnings(False)
  9. GPIO.setup(PIN, GPIO.IN)
  10. GPIO.setup(PIN, GPIO.OUT)
  11. if GPIO.input(PIN) == 0:
  12. GPIO.output(PIN, GPIO.HIGH)
  13. else:
  14. GPIO.output(PIN, GPIO.LOW)

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx
"