AZ-Delivery KY-022 Manual

Infrared receiver module

Advertisement

Quick Links

Welcome!
Thank you for purchasing our AZ-Delivery KY-022 Infrared Receiver Module.
On the following pages, you will be introduced to how to use and set up this
handy device.
Have fun!

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the KY-022 and is the answer not in the manual?

Questions and answers

Summary of Contents for AZ-Delivery KY-022

  • Page 1 Welcome! Thank you for purchasing our AZ-Delivery KY-022 Infrared Receiver Module. On the following pages, you will be introduced to how to use and set up this handy device. Have fun!
  • Page 2 Areas of application Education and teaching: Use in schools, universities and training institutions to teach the basics of electronics, programming and embedded systems. Research and development: Use in research and development projects to create prototypes and experiments in the fields of electronics and computer science. Prototype development: Use in the development and testing of new electronic circuits and devices.
  • Page 3 consult a doctor. Caution: Keep the product out of the reach of children and pets to avoid accidental contact and swallowing of small parts. Note: Store the product in a safe, closed container when not in use. Attention: Avoid contact of the product with food and drinks.
  • Page 4: Table Of Contents

    Table of Contents Introduction......................3 Specifications....................4 The pinout......................4 Working principle....................5 How to set-up Arduino IDE................7 How to set-up the Raspberry Pi and Python..........11 Connecting the module with Atmega328p.............12 Library for Arduino IDE................13 Sketch code for detecting hex code............14 Sketch code for controlling an LED............17 Emulating the TV remote with the KY-005.............18 Sketch code....................19 Connecting the module with Raspberry Pi.............22...
  • Page 5: Introduction

    Introduction The KY-022 Infrared receiver sensor module consists of an 1838 infrared receiver, a 1kΩ resistor and a red LED. Infrared receiver reacts to 38kHz infrared light. The infrared receiver is a photodiode and pre-amplifier that converts the infrared light into an electrical signal.
  • Page 6: Specifications

    Infrared wavelength: 940nm » Pulse duration: from 400µs to 2000µs » Dimensions: 9 x 22mm [0.9 X 0.35in] The pinout The KY-022 infrared receiver module has three pins. The pinout diagram is shown on the following image: - 4 -...
  • Page 7: Working Principle

    Working principle Infrared light is a light that is emitted by the sun, light bulbs, or anything else that produces heat. That means there is a lot of infrared light noise all around us. To prevent this noise from interfering with infrared signals, a signal modulation technique is developed.
  • Page 8 There are many infrared transmission protocols: Sony, Matsushita, NEC, and RC5 are some of the more common protocols. The NEC protocol is also the most common type in embedded projects, so it is used in this eBook as an example to show how the receiver converts the modulated infrared signal to a binary one.
  • Page 9: How To Set-Up Arduino Ide

    How to set-up Arduino IDE If the Arduino IDE is not installed, follow the link and download the installation file for the operating system of choice. For Windows users, double click on the downloaded .exe file and follow the instructions in the installation window. - 7 -...
  • Page 10 For Linux users, download a file with the extension .tar.xz, which has to be extracted. When it is extracted, go to the extracted directory and open the terminal in that directory. Two .sh scripts have to be executed, the first called arduino-linux-setup.sh and the second called install.sh.
  • Page 11 Almost all operating systems come with a text editor preinstalled (for example, Windows comes with Notepad, Linux Ubuntu comes with Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text editors are perfectly fine for the purpose of the eBook. Next thing is to check if your PC can detect an Atmega328p board.
  • Page 12 If the Arduino IDE is used on Windows, port names are as follows: For Linux users, for example port name is /dev/ttyUSBx, where x represents integer number between 0 and 9. - 10 -...
  • Page 13: How To Set-Up The Raspberry Pi And Python

    How to set-up the Raspberry Pi and Python For the Raspberry Pi, first the operating system has to be installed, then everything has to be set-up so that it can be used in the Headless mode. The Headless mode enables remote connection to the Raspberry Pi, without the need for a PC screen Monitor, mouse or keyboard.
  • Page 14: Connecting The Module With Atmega328P

    Connecting the module with Atmega328p Connect the KY-022 module with the Atmega328p as shown on the following connection diagram: KY-022 pin > Mc pin > Blue wire Middle pin (VCC) > Red wire - (GND) > Black wire - 12 -...
  • Page 15: Library For Arduino Ide

    Library for Arduino IDE To use the KY-022 module with Atmega328p it is recommended to install an external library. Open Arduino IDE and go to: Tools > Manage Libraries When a new window opens, type IRremote (two R's) in the search box and...
  • Page 16: Sketch Code For Detecting Hex Code

    Sketch code for detecting hex code #include <IRremote.h> #define SIGNAL_PIN 4 IRrecv receiver(SIGNAL_PIN); decode_results output; void setup(){ Serial.begin(9600); receiver.enableIRIn(); void loop() { (receiver.decode(&output)) { Serial.println(output.value, HEX); switch (output.decode_type) { case NEC: Serial.println("NEC"); break; case SONY: Serial.println("SONY"); break; case RC5: Serial.println("RC5"); break; case RC6: Serial.println("RC6");...
  • Page 17 // two tabs case MITSUBISHI: Serial.println("MITSUBISHI"); break; case SAMSUNG: Serial.println("SAMSUNG"); break; case Serial.println("LG"); break; case WHYNTER: Serial.println("WHYNTER"); break; case AIWA_RC_T501: Serial.println("AIWA_RC_T501"); break; case PANASONIC: Serial.println("PANASONIC"); break; case DENON: Serial.println("DENON"); break; default: case UNKNOWN: Serial.println("UNKNOWN"); break; receiver.resume(); - 15 -...
  • Page 18 Upload the sketch to the Atmega328p and open Serial Monitor (Tools > Serial Monitor). The result should look like the output on the following image: As it can be seen on the image above, the remote with NEC code specification is used. Press the specific key several times to get its code hexadecimal number.
  • Page 19: Sketch Code For Controlling An Led

    Sketch code for contolling an LED The following sketch is used to toggle the state of the LED (the on-board of Atmega328p) when a specific button is pressed on the remote. #include <IRremote.h> #define SIGNAL_PIN = 4; uint8_t toggle_state = 0; IRrecv receiver(SIGNAL_PIN);...
  • Page 20: Emulating The Tv Remote With The Ky-005

    Emulating the TV remote with the KY-005 In this chapter the TV remote is simulated using the KY-005 infrared LED module. Connect the KY-005 module with the Atmega328p as shown on the following connection diagram: KY-005 pin > Mc pin >...
  • Page 21: Sketch Code

    Sketch code #include <IRremote.h> #define BUTTON_PIN 2 uint8_t button_state = 0; uint8_t toggle_state = 0; IRsend transmitter; void setup() { Serial.begin(9600); pinMode(LED_BUILTIN, OUTPUT); pinMode(BUTTON_PIN, INPUT); void loop() { button_state = digitalRead(BUTTON_PIN); Serial.println(button_state); (button_state == HIGH) { (toggle_state == 0){ transmitter.sendNEC(0x807F00FF, 32); digitalWrite(LED_BUILTIN, HIGH);...
  • Page 22 At the beginning of the sketch the IRremote library is included. Next, one macro and two variables are created, where the macro represents the digital I/O pin number to which the push button is connected (2), and the variables are used to detect the state of the push button. After that, the transmitter object is created.
  • Page 23 With the sketch from the chapter Emulating the TV remote with the KY-005 the TV remote can be emulated. With the sketch from the chapter Connecting the module with Uno the TV receiver can be emulated. Combining these two sketches and modules, an infrared interface can be created for sending and receiving data in one direction (from infrared LED to infrared receiver) - 21 -...
  • Page 24: Connecting The Module With Raspberry Pi

    Connecting the module with Raspberry Pi Connect the KY-022 module with the Raspberry Pi as shown on the following connection diagram: KY-022 pin > Raspberry Pi pin Middle pin (VCC) > [pin 1] Red wire - (GND) > [pin 8] Black wire >...
  • Page 25: Python Script

    Python script import RPi.GPIO GPIO from time import time, sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) Signal_Pin GPIO.setup(Signal_Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) code None CODES 0xe0e040bf: "ON/OFF", 0xe0e08877: "0", 0xe0e020df: "1", 0xe0e0a05d: "2", 0xe0e0609f: "3", 0xe0e010ef: "4", 0xe0e0906f: "5", 0xe0e050ad: "6", 0xe0e030cf: "7", 0xe0e0b04f: "8", 0xe0e0708f: "9"...
  • Page 26 rec(pinNo, bouncetime=150): global code, CODES data binary_aquire(pinNo, bouncetime/1000.0) # 150us len(data) < bouncetime: return rate len(data) (bouncetime 1000.0) pulses i_break range(1, len(data)): (data[i] data[i-1]) len(data) pulses.append((data[i-1], int((i-i_break)/rate*1e6))) i_break outbin "" val, us pulses: continue outbin > 2000: break elif < 1000: outbin "0"...
  • Page 27 GPIO.add_event_detect(17,GPIO.FALLING,callback=rec,bouncetime=100) print('[Press CTRL + C to end the script!]') try: print('Starting IR Listener') print('Waiting for signal') while True: sleep(0.000033) # 33 microseconds except KeyboardInterrupt: print('\nScript end!') finally: GPIO.cleanup() - 25 -...
  • Page 28 Save the script by the name ky022.py. To run the script open terminal in the directory where the script is saved and run the following command: python3 ky022.py The result should look like the output on the following image: To stop the script press CTRL + C on the keyboard. - 26 -...
  • Page 29 The script starts with the import of two libraries, RPi.GPIO and time, where the first is used for GPIO names, and second for timing functions. Then the GPIO naming is set to BCM and any warnings related to GPIO interfaces are disabled. After thatm the mode of GPIO pin 17 is set to INPUT with internal PULL DOWN resistor, with the following line of code: GPIO.setup(Signal_Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  • Page 30 Inside the rec() function, the binary_aqurie() function is executed in order to get data from the infrared sensor. The duration of reading is in microseconds because infrared signals from the infrared transmitter are in the kHz range (38kHz, mentioned at the beginning of this chapter). Then received data is split into pulses.
  • Page 31 The outbin is the temporary variable used to build up a code. The val variable represents the state of the pulse, HIGH or LOW, and the us represents microseconds. If the value in the val variable is different than HIGH (logic “1”) which means the pulse is in a LOW state, that is not a point of interest for decoding algorithm, so it is dismissed.
  • Page 32 At the end of the script the try-except-finally block of code is executed. In the try block of code the indefinite loop (while True:) is created to keep the script running. To end the script press CTRL + C on the keyboard. This is called keyboard interrupt, and when this interrupt happens, the except block of code is exeuted (except KeyboardInterrupt:) displaying message Script end! in the terminal.
  • Page 33 If you are looking for the high quality microelectronics and accessories, AZ-Delivery Vertriebs GmbH is the right company to get them from. You will be provided with numerous application examples, full installation guides, eBooks, libraries and assistance from our technical experts.

Table of Contents