Controlling your Pi with an infrared remote

Controlling with Multiple Remotes

If you want to use multiple remotes with your Rasp Pi, you can create a new configuration file (being careful not to overwrite the old one) and then copy the code from that configuration file into the bottom of the existing configuration file /etc/lirc/lircd.conf (if you are using Raspbian) or /home/pi/lircd.conf if you are using Raspbmc.

Custom Coding

It is, of course, possible to control things other than XBMC with your remote control. For example, use the following instructions (for which you will need 3x5mm LEDs and 3x330-ohm resistors in addition to the "Parts List" in the sidebar) to control some LEDs with your remote control.

To start, lay out your circuit as shown in Figure 8, where the longer (bent) leg of the LED is positive. Next, you will need to install wiringPi, which the following code requires as a prerequisite. You can install wiringPi with the following commands:

cd ~
git clone git://
cd wiringPi
git pull origin
Figure 8: Circuit for testing remote with LED lights.

Once wiringPi is installed, create a new C file using the nano text editor:

nano ledcontrol.c

You can then copy and paste the code shown in Listing 4. See the comments in the listing if you would like to know more about what the code is doing.

Listing 4


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

Once you have entered all of the code, you can then save and close the file by pressing Ctrl-X and then Y. You now need to compile the code by typing the following command:

gcc -o ledcontrol ledcontrol.c -lwiringPi -llirc_client

You will then be able to run this program using the command:

sudo ./ledcontrol

Once the program is started, you should be able to press buttons 1, 2, and 3 on your remote and watch as the LEDs blink!

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content