fbpx

Most orders for IN STOCK items placed by 12PM CST M-F ship SAME DAY. Orders for custom items and prints may take additional time to process.

Sensor Wiki: KY-022 Infrared Sensor

This wiki article covers the KY-022 infrared sensor. Included are wiring diagrams, code examples, pinouts, and technical data. This sensor is designed to receive and decode IR signals transmitted from other devices, and remote controls such as those used for televisions and AV gear.

Content

  •  Sensor/Module Image Gallery
  •  Description and Technical Data
  •  Device Pinout
  •  Projects that use this Sensor/Module
  • Code Examples
    • Code example for Arduino
    • Code example for Raspberry Pi

 Description and Technical Data

The KY-022 has three pins and responds to a carrier frequency of 38kHz at 940nm.  This signal is sent to the digital output.  An LED module on the board will flash if an IR signal is detected.

This sensor module is perfect for decoding remote controls for home audio/visual equipment (home theater) and other types of remote controls that use IR.

Tech Specs for the KY-022 Infrared Sensor:

  • Chip type: VS1838B
  • Operating Voltage: 3.3V to 5V
  • Current Consumption: 1.5mA peak
  • Reception distance: 17m
  • Carrier Frequency: 38kHz
  • Infrared Wavelength: 940 nm
  • Pulse Duration: 400µs to 800µs
  • Dimensions: .87in X .35in (22mm x 9mm)

 Device Pinout & Schematics

This module has three pins: Signal, Vcc+, and Ground.

The KY-022 infrared sensor pinout is as follows:

The KY-022 schematic is as follows:

 Our Projects that Use this Sensor

The following Geek Pub projects use the KY-022 infrared sensor module:

 Code Examples

You’ll find below code examples of using the KY-022 infrared sensor module with both the Arduino and the Raspberry Pi (Python).

KY-022 Infrared Sensor Code Example for Arduino

The following code example is for the Arduino. This code will look for infrared codes from a remote control or other IR device and print the received code on the serial console.

Arduino Wiring:

  • KY-022 Sensor GND to Arduino GND
  • KY-022 Sensor Vcc+ to Arduino +5V
  • KY-022 Sensor Signal to Arduino PIN 10
// Print IR Buttons to Serial Monitor
// TheGeekPub.com
 
#include 
 
// Define the pin for the IR Receiver
const int IR_RECV_PIN = 10;
 
// Create the IR object
IRrecv gpirrecv(IR_RECV_PIN);
decode_results results;
 
void setup(){
  Serial.begin(9600);
  gpirrecv.enableIRIn();
  gpirrecv.blink13(true);
  Serial.println("READY.");
}
 
void loop(){
  if (gpirrecv.decode(&results)){
  Serial.println(results.value, HEX);
  gpirrecv.resume();
}
}

KY-022 Infrared Sensor Code Example for Raspberry Pi

The following code example is for the Raspberry Pi using the Python programming language. This code will listen for codes received by the infrared sensor and display them on the terminal window.

Raspberry Pi Wiring:

  • KY-022 Sensor GND to Raspbery Pi GND
  • KY-022 Sensor Vcc+to Raspberry Pi PIN 2
  • KY-022 Sensor Signal to Raspberry Pi PIN 11
import RPi.GPIO as GPIO
from time import time

def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def binary_aquire(pin, duration):
# aquires data as quickly as possible
t0 = time()
results = []
while (time() - t0) < duration:
results.append(GPIO.input(pin))
return results

def on_ir_receive(pinNo, bouncetime=150):
# when edge detect is called (which requires less CPU than constant
# data acquisition), we acquire data as quickly as possible
data = binary_aquire(pinNo, bouncetime/1000.0)
if len(data) < bouncetime:
return
rate = len(data) / (bouncetime / 1000.0)
pulses = []
i_break = 0
# detect run lengths using the acquisition rate to turn the times in to microseconds
for i in range(1, len(data)):
if (data[i] != data[i-1]) or (i == len(data)-1):
pulses.append((data[i-1], int((i-i_break)/rate*1e6)))
i_break = i
# decode ( < 1 ms "1" pulse is a 1, > 1 ms "1" pulse is a 1, longer than 2 ms pulse is something else)
# does not decode channel, which may be a piece of the information after the long 1 pulse in the middle
outbin = ""
for val, us in pulses:
if val != 1:
continue
if outbin and us > 2000:
break
elif us < 1000:
outbin += "0"
elif 1000 < us < 2000:
outbin += "1"
try:
return int(outbin, 2)
except ValueError:
# probably an empty code
return None

def destroy():
GPIO.cleanup()

if __name__ == "__main__":
setup()
try:
print("Starting IR Listener")
while True:
print("Waiting for signal")
GPIO.wait_for_edge(11, GPIO.FALLING)
code = on_ir_receive(11)
if code:
print(str(hex(code)))
else:
print("Invalid code")
except KeyboardInterrupt:
pass
except RuntimeError:
# this gets thrown when control C gets pressed
# because wait_for_edge doesn't properly pass this on
pass
print("Quitting")
destroy()

We hope this wiki article has been helpful to you. Please leave a comment below if you have any questions or comments, as we try to keep these articles constantly up to date.

 Back to List of Arduino Sensors and Modules

Upgrade to Premium

If you like our content maybe consider upgrading to Premium. You’ll get access to

  • Free access to all plans
  • Member only videos
  • Early access to content
  • Ad free Experience
  • Discounts on store merch
  • Direct hotline contact form

Leave a Reply