IR receiver (VS1838B / KY-022): pinout and wiring
The IR receiver picks up the 38 kHz modulated signal from remote controls and hands the demodulated pulses to the microcontroller. See the pinout, wiring, the code to read keys and the NEC frame format in the simulator.
Specifications
- Carrier
- 38 kHz
- Supply
- 2.7 V to 5.5 V
- Output
- Digital, HIGH at rest (inverted relative to the remote LED)
- Range
- ≈ 8 to 10 m, line of sight
- Angle
- ≈ 90°
- Most common protocol
- NEC (32 bits)
- Current
- ≈ 1.5 mA
Pinout
On the bare part (VS1838B), with the dome facing you, the order is usually OUT, GND and VCC. The KY-022 module is silkscreened S, + and −: check your board, because the order varies between makers.
VS1838B / KY-022 (3 pins)
| 1 | OUT (S) | Demodulated digital output |
| 2 | GND (−) | Ground |
| 3 | VCC (+) | Supply, 3.3 V or 5 V |
NEC remote simulator
Pick a key on the most common 21-key remote and see the NEC frame the receiver delivers to the Arduino.
Key pressedEach byte is sent least significant bit first. A 0 bit lasts 1.125 ms and a 1 bit lasts 2.25 ms, so the frame duration changes from key to key. Values are for the 21-key remote with address 0x00.
How to wire it
Connect VCC to 5 V (or 3.3 V on the ESP32) and GND to ground. Connect OUT to a digital pin, for example D11 on the Arduino Uno. The module already has its resistor, so no extra parts are needed.
The receiver only sees light modulated at 38 kHz. A lamp or the sun produces no signal, but strong direct light can saturate the sensor.
Install the IRremote library. It decodes the NEC protocol and gives you the address and command of each key pressed.
When you hold a key, an NEC remote sends a short repeat frame. The library flags it, so you can ignore it or use it to repeat the action.
Aim the remote LED at the receiver dome. To check that the remote works, look at its LED through a phone camera: it flashes purple when you press a key.
Example code (Arduino)
Sketch that prints the command of each key pressed on the serial monitor (IRremote library 4.x).
#include <IRremote.hpp>
#define IR_PIN 11
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK);
}
void loop() {
if (IrReceiver.decode()) {
if (!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)) {
Serial.print("Command: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
}
IrReceiver.resume();
}
}
Common mistakes
- Swapping VCC and GND: the receiver heats up and burns out within seconds. Check the silkscreen before powering it.
- Using a pin the IRremote library needs for its timer: the decoder does not work. Use D11 or the pin listed in the library documentation.
- Forgetting to call resume() after decode(): the receiver reads only the first key.
- Reading the output with pulseIn() or slow polling: pulses last 562 µs and get lost. Use the library, which works by interrupt.
- Testing with a plain infrared LED instead of a remote: without the 38 kHz carrier the receiver ignores the light.
Always check the manufacturer's datasheet for your exact model: pin order can vary between modules.
Frequently asked questions
How does an infrared receiver work?
A photodiode picks up the IR light, a filter isolates the 38 kHz carrier and the digital output repeats the remote pulse pattern, inverted: HIGH at rest and LOW during each mark.
What is the difference between the VS1838B and the KY-022?
The VS1838B is the bare three-leg receiver. The KY-022 is a module with a VS1838B on a small board with labeled pins.
What is the NEC protocol?
It is the protocol of most cheap remotes: a 9 ms leading pulse, a 4.5 ms gap and 32 bits (address, inverted address, command, inverted command), each sent least significant bit first.
Does the IR receiver work with ESP32?
Yes. Power it from 3.3 V, connect OUT to an input GPIO and use the IRremote library for ESP32.