KY-022 Infrared receiver module

From SensorKit X40 Wiki

Revision as of 18:07, 20 February 2017 by Webmaster Wiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Picture

none

Technical data / Short description

Carrier frequency: 38kHz Can receive infrared signals and gives it as a digital signal out.

Additional to that the LED of the module will short flash up if a infrared signal was detected.

Pinout

none

Code example Arduino

With help from the sensor modules KY-005 and KY-022 you can build up an infrared-remote + infrared-receiver system. For this, you need additional to the two modules, two Arduinos. The first one will send the signal and the other one will receive the signal and shows it at the serial terminal.


An Additional library will be needed for the following code example:

- [Arduino-IRremote] from Ken Shirriff | published under LGPL

The library is in the package and needs to be coopied to the arduino lirbrary folder before you start the Arduino IDE.

You will find it standartly under the following path of your windows installation.

C:\User\[Username]\documents\Arduino\libraries



Infrared send systems have different protocolls to send data. In this example we use the RC5 protocol, the used library "Arduino-IRremote" converts the data independently. The library has additional protocolls, they are marked in this documentation.


Code for the receiver:

// Arduino-IRremote library will be added
#include 

// You can declare the input pin for the signal output of the KY-022 here
int RECV_PIN = 11;

// Arduino-IRremote library will be initialized
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Infrared receiver will be started
}

// main program loop
void loop() {
  
  // It will be checked if the receiver has gotten a signal.
  if (irrecv.decode(&results)) {
    // At signal input, the received and decoded signal will show via serial console.
    Serial.println(results.value, HEX);
    irrecv.resume(); 
  }
}


Code for the transmitter:

//Arduino-IRremote library will be added
#include 

//...and here initialized
IRsend irsend;
<br />// The configuration of the output pin will be made by the library<br />// The output pin is a different one for different arduinos<br />// Arduino UNO:  Output = D3<br />// Arduino MEGA: Output = D9<br />// You will find a full list of output pins on the website;
// http://z3t0.github.io/Arduino-IRremote/
void setup()
{
}

// main program loop
void loop() {
        // The transmitter sends in this example the signal A90 (hex. dezimal form) in the endcoding "RC5"
        // It will be transmitted 3 times, after that it will make a 5 second break.
	for (int i = 0; i < 3; i++) {
		irsend.sendRC5(0xA90, 12); // [0xA90] signal | [12] Bit-length signal (hex A90=1010 1001 0000)
		delay(40);
	}
	delay(5000); // 5 second break between the sending impulses
}

Example program download:

Arduino_Fernbedienung.zip


Connections Arduino 1 [Receiver]:

KY-022

Signal = [Pin 11]
+V = [Pin 5V]
GND = [Pin GND]

Connections Arduino 2 [Transmitter]:

KY-005

Signal = [Pin 3 (Arduino Uno) | Pin 9 (Arduino Mega)]
GND+Widerstand = [Pin GND*]
GND = [Pin GND]
  • *Only if a resistor was soldered to the circuit board.

Codebeispiel Raspberry Pi

Because of its progressive processor architecture the Raspberry Pi has a big advantage to the Arduino.

It can run a full Linux OS by itself. With help of an infrared-receiver, it could not only transmit simple data signals, furthermore it can control complete programs via remote.

To setup an infrared control system you should use the Linux software "lirc" ( published under the LGPL-Website ). In the following section, we show how you can teach lirc a remote and how the remote sends the learned signals via infrared ( for example to make the Raspberry Pi to a software controlled remote ).


On this purpose, the module KY-005 will be used as an infrared-transmitter and the Ky-022 will be used as infrared-receiver.


Connections Raspberry Pi:

KY-005

Signal = GPIO17 [Pin 11]
GND+Widerstand = GND* [Pin 9]
GND = GND [Pin 6]
  • *Only if a resistor was soldered to the module


KY-022

Signal = GPI18 [Pin 12]
+V = 3,3V [Pin 17]
GND = GND [Pin 25]

Lirc Installation

Open a terminal at the desktop or use SSH to login to your Raspberry Pi. To install lirc use following command:

sudo apt-get install lirc -y

[For this the Raspberry Pi has to be connected to the internet]

To use the lirc module immediately after the OS boot, you have to add the following string to the end of the file "/boot/config.txt":

    dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17,gpio_in_pull=up

The "gpio_in_pin=18" will be defined as input pin of the IR-receiver and the "gpio_out_pin=17" as output pin of the IR-transmitter.

The file could be editted with the command:

sudo nano /boot/config.txt

You can save and close the file with the key sequence [ctrl+x -> y -> enter]


You also have to modify the file "/etc/lirc/hardware.conf" with the command:

sudo nano /etc/lirc/hardware.conf

in this file you have to change following lines:

    DRIVER="UNCONFIGURED" 
    --->>  
    DRIVER="default"
    DEVICE=""
    --->>
    DEVICE="/dev/lirc0"
    MODULES=""
    --->>  
    MODULES="lirc_rpi"

The modified file should now look like:

# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS=""

#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=""

After that, we reboot the Raspberry Pi with the following command:

sudo reboot

IR-Receiver Test

To test the connected receiver, you have to close the lirc first with the following command:

sudo /etc/init.d/lirc stop

After that, you can test if signals could be detected on the Raspberry Pi by using the following command:

mode2 -d /dev/lirc0

and by pressing random keys on an infrared remote. Now you should see numbers in the following form:

space 95253
pulse 9022
space 2210
pulse 604
space 95246
pulse 9019
space 2211
pulse 601
space 95252
pulse 9019
space 2210
pulse 603
space 95239
pulse 9020
space 2208
pulse 603
...

You can restart lirc with the following command:

sudo /etc/init.d/lirc start

Teaching Remote

To register an infrared remote in the "lirc" system you have to configure the file "/etc/lirc/lircd.conf". The infrared codes for receiving are saved in this file.

You can use the lirc-software assistant to create a lircd.conf which will create the file automatically for you. To use the assistant follow these steps:


First you have to stop the lirc service

sudo /etc/init.d/lirc stop

With the next command you will start the assistant:

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

The assistant will first initialize the remote, for that you have to press multiple keys after another so that the lirc system could learn the coding of the remote. Please follow the instructions of the assistant for that. The assistant will ask you for the key assignments which have to get a new infrared code after the end of the initializing. For this you can choose the key assignments from the following file: 

FernbedienungsCodes.txt


After you gave these to the assistant and confirmed it with enter, the recording of the infrared code for the chosen key will start.

Example:  type in [KEY_0]  -> confirm with enter -> press key "0" from the remote -> waiting for the assistant to confirm the recording.

You can end the assistant with the enter key if you don't want to assign another infrared code to a key. After this the configuration file will be created but you have to choose a name for the learned remote. For this we will open the file with the editor:

sudo nano ~/MeineFernbedienung.conf

Here you can change the line 17 from

    name  /home/pi/MeineFernbedienung.conf

to

    name  MeineFernbedienung

Please keep in mind to not use any special characters within the name. You can save and close the file with the key sequence [ctrl+x -> y -> enter].


After creating the configuration file you can make a backup from the original lircd.conf with the following command:

sudo mv /etc/lirc/lircd.conf /etc/lirc/lircd.conf.bak

You can start to use the before created configuration file with the command:

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

Now you can start the lirc system again with the following command:

sudo /etc/init.d/lirc start

Now the new learned remote is registered at the system and can used with compatible software like Mediencenter OpenElec. Alternatively you can use the following command to test the key assignments and function of the remote.

irw

It is possible that you have to enable the remote in the settings of OpenElec before you can use it.


 

Transmitting command via infrared transmitter

If you want to control devices like TV with the Raspberry Pi via infrared, you can now transmit the before learned commands. You can build up a software controlled infrared control system or you can start and stop single devices via network/internet.

First we check the key assignment with the following command:

irsend LIST MeineFernbedienung "" 

Now we can send the command [KEY_0] with the following command line:

irsend SEND_ONCE MeineFernbedienung KEY_0

You should now see a reaction at the device (TV or something you wanted to control). You can have another variation of the command above which starts a loop and constantly repeats the sending of the signal.

irsend SEND_START MeineFernbedienung KEY_0

The code will be constantly sended until you use the command:

irsend SEND_STOP MeineFernbedienung KEY_0

This will normally used for increasing and decreasing the volume.