Difference between revisions of "KY-005 Infrared Transmitter module"

From SensorKit X40 Wiki

Jump to: navigation, search
(Code example remote)
(No summary)
Line 1: Line 1:
==Technical data / Short description==
==Technical data / Short description==
Line 29: Line 29:
Line 80: Line 80:
<pre class="brush:cpp">// Arduino-IRremote library will be added
<pre class="brush:cpp">// Arduino-IRremote library will be added
#include <IRremote.h>
// You can declare the input pin for the signal output of the KY-022 here
// You can declare the input pin for the signal output of the KY-022 here
Line 112: Line 112:
<pre class="brush:cpp">//Arduino-IRremote library will be added
<pre class="brush:cpp">//Arduino-IRremote library will be added
#include <IRremote.h>
//...and here initialized
//...and here initialized

Revision as of 16:23, 6 March 2017



Technical data / Short description

A LED which shines in the infrared area. A resistor will be needed for some input voltage.

Vf= 1,1V

If= 20mA

emitted wavelength: 940nm (not visible light)


Rf (3,3V) = 120Ω

[used with ARM CPU-Core based mocrocontroller like Raspberry-Pi]

Rf (5V) = 220Ω 

[used with Atmel Atmega based microcontroller like Arduino]


3 G V+R S.png

  • You can directly solder a resistor to the circuit board. In that case, the central pin (2), which includes the resistor, can now be used.

Code example Arduino

Code example ON/OFF

This code example will make a LED light up for 4 seconds and then let it go off for 2 seconds and this via defined output pin.

int Led = 13;

void setup ()
  pinMode (Led, OUTPUT); // Initialization output pin for the LED

void loop () //main program loop
  digitalWrite (Led, HIGH); // LED will be switched on
  delay (4000); // Wait for 4 seconds
  digitalWrite (Led, LOW); // LED will be switched off
  delay (2000); // Wait for another 2 seconds in which the LED is off

Example program download


Code example remote

With help from the both sensor modules KY-005 and KY-022 you can build an infrared remote + infrared receiver system. For that, you need the two modules and two additional arduinos.
The first one sends the signal and the second one receives it and shows it on the console.

An additional library is needed for the next code example:

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

The library is in the package and has to be copied before the start into the library folder.

You will find it normally under the following path:


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

// 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()
  irrecv.enableIRIn(); // Infrared receiver will start

// 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);

Code for the transmitter:

//Arduino-IRremote library will be added

//...and here initialized
IRsend irsend;

// The configuration of the output pin will be made by the library
// The output pin is a different one for different arduinos
// Arduino UNO:  Output = D3
// Arduino MEGA: Output = D9
// 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 encoding "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(5000); // 5 second break between the sending impulses

Example program download:


Connections Arduino 1 [Receiver]:


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

Connections Arduino 2 [Transmitter]:


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

Code example Raspberry Pi

We will give you two code examples for this sensor module here. One which starts the infrared transmitter LED for a moment and then turns it off ( emitted light not visible - can be seen by a smartphone camera for example ), and the other example which is an direct application example for the Raspberry Pi, where the Pi will be used as a receiver for remotes to control Mediencenter-software OpenElec or as infrared-trasmitter where it can be programed to be a software controlled remote.

Code example ON/OFF

Program example in the language python.

# needed modules will be imported and configured
import RPi.GPIO as GPIO
import time
# Declaration of the input pin on which the sensor is connected.
LED_PIN = 24
GPIO.setup(LED_PIN, GPIO.OUT, initial= GPIO.LOW)
print "LED-Test [press ctrl+c to end]"

# main program loop
        while True:
                print("LED on for 4 seconds")
                GPIO.output(LED_PIN,GPIO.HIGH) #LED on
                time.sleep(4) #wait mode for 4 seconds
                print("LED off for 2 seconds") 
                GPIO.output(LED_PIN,GPIO.LOW) #LED off
                time.sleep(2) #wait mode for another 2 seconds, in this seconds the LED will be off
# Scavenging work after the program has ended.
except KeyboardInterrupt:

Connections Raspberry Pi:

Sensor Signal = GPIO24 [Pin 18]
GND+resistor = GND* [Pin 9]
Sensor GND = GND [Pin 6]
  • *Only if resistor was soldered to the circuid board

Example program download:


To start use the following command line:

sudo python LedTest_RPi_4On_2Off.py

Code example remote

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 copntrol 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:


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


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":


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 via the command:

sudo nano /boot/config.txt

You can save and close the file via 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:


The modified file should now look like:

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

#Don't start lircmd even if there seems to be a good config file

#Don't start irexec, even if a good config file seems to exist.

#Try to load appropriate kernel modules

# Run "lircd --driver=help" for a list of supported drivers.
# usually /dev/lirc0 is the correct setting for systems using udev

# Default configuration files for your hardware if any

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 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

Remote teach

To register an infrared-remote at the lirc system, you have to configure the file "/etc/lirc"lircd.conf".

In this file are the command assignments of the infrared codes saved.

To get a good formatted lircd.conf, use the lirc assistant software which creates the file automatically.

To start this process you have first to stop lirc with the command:

sudo /etc/init.d/lirc stop

and now with the following command, we can start the assistant:

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

The assistant will start an initialization of the remote, in this initialization you have to press a few keys after another, that the lirc system can learn the encoding of the remote. For that please follow the instructions of the assistant. After the initialization, the assistant asks for the name of the key which should get a new infrared code. You can choose your key from the following file:


You have to type these into the assistant and have to be confirmed with enter. After this, the recording of the infrared code for the picked key will start.

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

If no more keys should be configured, you can close the assistant by pressing the enter key. After this, the configuration file is created, but you have to choose a name for the recorded remote. For this we have to open the file with the editor:

sudo nano ~/MeineFernbedienung.conf

Here you have to change the line:

    name  /home/pi/MeineFernbedienung.conf


    name  MeineFernbedienung

Please don't use any spaces or additional characters in the name.

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

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

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

With the command 

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

you can use the before created file for the lirc system.

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

sudo /etc/init.d/lirc start

From now on, the remote is known and could be used with the right software, like Mediencenter OpenElec for example. Alternative you can use the following command to test the functions:


If you want to use the remote with OpenElec, it could be possible that you have to activate lirc in the options of OpenElec.

Sending command via Infrared Transmitter

If you want to control devices like your Television via Raspberry Pi, you can now send the learned commands with the infrared transmitter. With that you can build a software controlled infrared controller or you can use the internet or the network to switch single devices on and off.

First we check with the following command:

irsend LIST MeineFernbedienung ""

which assigments are available for the remote.

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

irsend SEND_ONCE MeineFernbedienung KEY_0

On your Television or the receiver-end-device should show up a reaction. You can have the example above in other variations like , instead of sending the signal only once , it will be send multiple times.

irsend SEND_START MeineFernbedienung KEY_0

After this, the code [KEY_0] will be repeatly send out until we end it with the following command:

irsend SEND_STOP MeineFernbedienung KEY_0