Fundamentals 12 min read

Control RGB LEDs with an NEC Infrared Codec Module and STM32

This tutorial explains how to use the YS‑IRTM NEC infrared codec module with an STM32F103C8T6 MCU to decode remote‑control signals, map them to UART commands, and drive a three‑color LED, covering hardware wiring, protocol details, UART configuration, and full source code.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Control RGB LEDs with an NEC Infrared Codec Module and STM32

1. Infrared Codec Module Overview

The YS‑IRTM module implements the NEC infrared protocol, commonly used in TV and air‑conditioner remote controls. It provides an infrared emitter, receiver, and an expansion header for multiple emitters.

Emitter: 940 nm wavelength, 38 kHz carrier, NEC‑encoded signal.

Receiver: Receives NEC signals for MCU decoding.

Expansion header: Allows connecting additional emitters.

2. NEC Encoding Principle

NEC frames consist of a leader code (9 ms pulse + 4.5 ms space), a 16‑bit address (high and low bytes), an 8‑bit command, and an 8‑bit inverse command. The carrier is 38 kHz. Logical "0" is a 0.56 ms carrier followed by 0.565 ms space (1.125 ms period); logical "1" is a 0.56 ms carrier followed by 1.69 ms space (2.25 ms period). The frame ends with a 0.56 ms carrier.

NEC timing diagram
NEC timing diagram

3. Communication Diagram

The goal is to control a three‑color LED (green, yellow, red) using infrared commands. When the remote sends the code for a specific color, the corresponding LED toggles.

LED control diagram
LED control diagram

4. Programming Preparation

NEC frames contain a 16‑bit address (high/low), an 8‑bit command, and its inverse. The UART default baud rate is 9600 bps. By connecting the module to a USB‑TTL adapter, you can view raw codes in a serial monitor.

4.1 Hardware Wiring

YS‑IRTM module

Infrared remote (any NEC remote)

USB‑TTL converter

Typical connections (module ↔ USB‑TTL): VCC‑5 V, GND‑GND, RXD‑TX, TXD‑RX.

Wiring diagram
Wiring diagram

4.2 Receiving Codes

After wiring, open a serial terminal at 9600 bps, enable timestamp and hex view, then press remote keys. Example codes: 1 → 00 FF 16 (green), 2 → 00 FF 19 (yellow), 3 → 00 FF 0D (red).

Remote codes
Remote codes

4.3 Transmitting Commands

Command format: Address Operation Data1 Data2 Data3. For NEC, the address is A1 (default) or FA (fixed). Operation bits:

F1 – emit infrared.

F2 – change UART address.

F3 – change baud rate (01‑4800, 02‑9600, 03‑19200, 04‑57600).

Examples:

Emit A1 F1 1C 2F 33 → send NEC code 1C 2F 33.

Set UART address to 0xA5: A1 F2 A5 00 00.

Set baud rate to 9600 bps: A1 F3 02 00 00.

5. Infrared Pair Experiment

Two YS‑IRTM modules are connected to separate USB‑TTL adapters and exchange codes, demonstrating bidirectional communication.

Pair experiment wiring
Pair experiment wiring

6. Implementation on STM32

6.1 Hardware Connections

STM32F103C8T6

YS‑IRTM module

Infrared remote

Three‑color LED (connected to PA5‑PA7)

USB‑TTL for UART2 (module) and UART1 (debug)

ST‑LINK V2 for programming

Pin mapping (module ↔ STM32): GND‑GND, RXD‑A2 (USART2), TXD‑A3 (USART2), 5V‑5V.

6.2 UART Driver (excerpt)

UART_HandleTypeDef ys_uart_handle;
void ys_init(uint32_t baudrate) {
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

embeddedSTM32UARTinfraredLED controlNEC protocol
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.