Control the GPIO on a Raspberry Pi with an IR Remote

Raspberry projects

Control the GPIO on a Raspberry Pi with an IR Remote

Messaggio da leggereda root » 09/01/2019, 18:33

Immagine

Connecting the IR sensor to a Raspberry Pi is very easy as there are only 3 pins on the sensor, GND, 3v and Output. We will connect the output to GPIO 18. You can choose another pin, just take note of it as you will need to specify this pin when installing LIRC.

We need to install LIRC and client libraries:

sudo apt-get install lirc liblircclient-dev

Then add the two lines below to /etc/modules . This will start the modules up on boot. Pin 18 bellow will be used to take the output from the IR sensor:
Codice: Seleziona tutto
lirc_dev
lirc_rpi gpio_in_pin=18


Also edit your /boot/config.txt file and add:

Codice: Seleziona tutto
dtoverlay=lirc-rpi,gpio_in_pin=18


Edit /etc/lirc/hardware.conf and have it appear exactly as shown below:

Codice: Seleziona tutto
# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"
 
# Don't start lircmd even if there seems to be a good config file
# START_LIRCMD=false
 
# Don't start irexec, even if a good config file seems to exist.
# START_IREXEC=false
 
# Try to load appropriate kernel modules
LOAD_MODULES=true
 
# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"
 
# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""


reboot
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 428
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Control the GPIO on a Raspberry Pi with an IR Remote

Messaggio da leggereda root » 09/01/2019, 18:34

A quick test

To perform a quick test to see if LIRC is working, we need to stop the LIRC daemon and start mode2. mode2 shows the the pulse/space length of infrared signals:

sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0


When buttons are pressed on your remote, mode2 will give a similar output to what is shown below:

Codice: Seleziona tutto
pulse 627
space 514
pulse 624
space 513
pulse 599
space 521
pulse 618
space 1668
pulse 589
space 532
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 428
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Control the GPIO on a Raspberry Pi with an IR Remote

Messaggio da leggereda root » 09/01/2019, 18:35

Record IR codes from your remote

irrecord will help us discover the IR codes used by your remote and assist with creating a conf file which will be used by LIRC:

irrecord -d /dev/lirc0 ~/lircd.conf

Once started, irrecord will show detailed instructions on how to setup your remote. However, here is summary;
1.When asked, press all buttons on your remote until asked to stop.
2.Assign a pre-defined name to each button. running irrecord --list-namespace will display a list of available names.

Now replace the existing conf file (which is most likely empty) with the new one you just created:

sudo cp lircd.conf /etc/lirc/lircd.conf

Restart LIRC:

sudo /etc/init.d/lirc restart

Test the new remote conf file
You can use irw to test the new config. irw sends data from Unix domain socket to stdout
You should get a similar output as below.... This was me pushing the 1, 2 and 3 keys on the remote:

irw

Codice: Seleziona tutto
pi@raspberrypi ~ $ irw
0000000000fd08f7 00 KEY_1
0000000000fd08f7 01 KEY_1
0000000000fd08f7 02 KEY_1
0000000000fd8877 00 KEY_2
0000000000fd8877 01 KEY_2
0000000000fd8877 02 KEY_2
0000000000fd48b7 00 KEY_3
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 428
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Control the GPIO on a Raspberry Pi with an IR Remote

Messaggio da leggereda root » 09/01/2019, 18:39

Creating your program to control the GPIO

We will use C to write a simple program that will be used to control three LEDs connected to the GPIO.
We will also require WiringPi to control the PINs and use the LIRC client library to get data from the IR sensor.

First, install WiringPi:

sudo apt-get update
git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
./build


The code
The code below will allow you to control three LEDs connected to the GPIO of a Raspberry Pi. We will use the 1,2 & 3 buttons on the numerical pad of the Adafruit mini remote to turn these LEDs off and on.
The code includes alot of comments which will help you understand how it works:

Codice: Seleziona tutto
#include <wiringPi.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lirc/lirc_client.h>
#include <time.h>
 
void flipLED (int led);
 
//The WiringPi pin numbers used by our LEDs
#define LED1 4
#define LED2 5
#define LED3 6
 
#define ON 1
#define OFF 0
 
int main(int argc, char *argv[])
{
        struct lirc_config *config;
 
        //Timer for our buttons
        int buttonTimer = millis();
 
        char *code;
        char *c;
 
        //Initiate WiringPi and set WiringPi pins 4, 5 & 6 (GPIO 23, 24 & 25) to output. These are the pins the LEDs are connected to.
        if (wiringPiSetup () == -1)
            exit (1) ;
 
        pinMode (LED1, OUTPUT);
        pinMode (LED2, OUTPUT);
        pinMode (LED3, OUTPUT);
 
        //Initiate LIRC. Exit on failure
        if(lirc_init("lirc",1)==-1)
                exit(EXIT_FAILURE);
 
        //Read the default LIRC config at /etc/lirc/lircd.conf  This is the config for your remote.
        if(lirc_readconfig(NULL,&config,NULL)==0)
        {
                //Do stuff while LIRC socket is open  0=open  -1=closed.
                while(lirc_nextcode(&code)==0)
                {
                        //If code = NULL, meaning nothing was returned from LIRC socket,
                        //then skip lines below and start while loop again.
                        if(code==NULL) continue;{
                                //Make sure there is a 400ms gap before detecting button presses.
                                if (millis() - buttonTimer  > 400){
                                        //Check to see if the string "KEY_1" appears anywhere within the string 'code'.
                                        if(strstr (code,"KEY_1")){
                                                printf("MATCH on KEY_1\n");
                                                flipLED(LED1);
                                                buttonTimer = millis();
                                        }
                                        else if(strstr (code,"KEY_2")){
                                                printf("MATCH on KEY_2\n");
                                                flipLED(LED2);
                                                buttonTimer = millis();
                                        }
                                        else if(strstr (code,"KEY_3")){
                                                printf("MATCH on KEY_3\n");
                                                flipLED(LED3);
                                                buttonTimer = millis();
                                        }
                                }
                        }
                        //Need to free up code before the next loop
                        free(code);
                }
                //Frees the data structures associated with config.
                lirc_freeconfig(config);
        }
        //lirc_deinit() closes the connection to lircd and does some internal clean-up stuff.
        lirc_deinit();
        exit(EXIT_SUCCESS);
}
 
void flipLED (int led)
{
        //If LED is on, turn it off. Otherwise it is off, so thefore we need to turn it on.
        if(digitalRead(led)==ON)
                digitalWrite(led, OFF);
        else
                digitalWrite(led, ON);
}


Compile the above code with;

gcc -o example example.c -lwiringPi -llirc_client

Create an empty lirc file, otherwise lirc will complain;

touch /etc/lirc/lircrc

Start it with;

sudo ./example
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 428
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Control the GPIO on a Raspberry Pi with an IR Remote

Messaggio da leggereda root » 09/01/2019, 18:54

Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 428
Iscritto il: 10/04/2012, 10:55
Località: Turin


Torna a Raspberry

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite