Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 14

IR Receiver Module

Print the supplied remote’s button names and repeat indications.

Decode NEC infrared messages using IRremote 4.1.2.

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • An infrared receiver carrier converts a modulated remote-control light signal into digital pulse timing. Black or blue PCB with a three-lead IR detector and header. Receiver housings and three-pin packages have different pin orders between models.
  • An IR handheld remote transmits button commands as timed infrared bursts to a compatible receiver. Handheld button pad with a dark/clear IR emitter window at the front and battery compartment on the rear.
  • Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.

Assembly

  1. Unplug USB and connect the labeled three-pin module; do not substitute a bare receiver’s lead order.
  2. Remove the remote’s battery isolation tab if present.
  3. Reconnect USB and point the remote toward the receiver.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗
Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: 5VIR receiver module: + / VCC
Mega: GNDIR receiver module: - / GND
Mega: D11IR receiver module: S / OUT
Open vector connection diagram ↗

Code

Mandatory configuration: Using this platformio.ini file is mandatory because it declares the required Arduino-IRremote v4.1.2 library via its GitHub URL in lib_deps. If PlatformIO in VS Code does not download the library automatically, follow the link below to download the ZIP yourself, extract it into your project's lib/ folder, and point lib_deps to it. Download Arduino-IRremote v4.1.2 ZIP ↗

PlatformIO configuration
platformio.ini
[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
monitor_speed = 9600

lib_deps =
  https://github.com/Arduino-IRremote/Arduino-IRremote/archive/refs/tags/v4.1.2.zip

Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.

main.cpp
#include <Arduino.h>
// Enable support for the NEC infrared protocol before including the IRremote library.
#define DECODE_NEC
// IRremote.hpp supplies the receiver object and decode functions.
#include <IRremote.hpp>

const uint8_t RECEIVER_PIN = 11;
// Keep the last full button code so repeat messages can reuse it.
uint32_t previousCode = 0;
bool havePrevious = false;

// This function receives a 32-bit infrared code and prints a familiar button name.
void printButton(uint32_t code) {
  // switch compares code with each case; break exits the chosen case.
  switch (code) {
    case 0xBA45FF00: Serial.println("POWER"); break;
    case 0xB847FF00: Serial.println("FUNC/STOP"); break;
    case 0xB946FF00: Serial.println("VOL+"); break;
    case 0xBB44FF00: Serial.println("FAST BACK"); break;
    case 0xBF40FF00: Serial.println("PAUSE"); break;
    case 0xBC43FF00: Serial.println("FAST FORWARD"); break;
    case 0xF807FF00: Serial.println("DOWN"); break;
    case 0xEA15FF00: Serial.println("VOL-"); break;
    case 0xF609FF00: Serial.println("UP"); break;
    case 0xE619FF00: Serial.println("EQ"); break;
    case 0xF20DFF00: Serial.println("ST/REPT"); break;
    case 0xE916FF00: Serial.println("0"); break;
    case 0xF30CFF00: Serial.println("1"); break;
    case 0xE718FF00: Serial.println("2"); break;
    case 0xA15EFF00: Serial.println("3"); break;
    case 0xF708FF00: Serial.println("4"); break;
    case 0xE31CFF00: Serial.println("5"); break;
    case 0xA55AFF00: Serial.println("6"); break;
    case 0xBD42FF00: Serial.println("7"); break;
    case 0xAD52FF00: Serial.println("8"); break;
    case 0xB54AFF00: Serial.println("9"); break;
    // Handle any button code that is not listed above.
    default: Serial.println("other button"); break;
  }
}

// Start Serial and initialize the IR receiver on the selected pin.
void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  // Start receiving pulses, without blinking a feedback LED.
  IrReceiver.begin(RECEIVER_PIN, DISABLE_LED_FEEDBACK);
}

// Process a command only when the receiver has completed a message.
void loop() {
  // ! means NOT; return leaves this loop() call early when there is no message.
  if (!IrReceiver.decode()) return;
  uint32_t code = IrReceiver.decodedIRData.decodedRawData;
  // & tests a bit flag to see whether the remote sent a held-button repeat.
  const bool repeat = (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) != 0;
  // Tell the receiver it may begin collecting the next message.
  IrReceiver.resume();
  // A repeat event reuses the last known key, if there is one.
  if (repeat) {
    // Ignore a repeat when we have not seen any complete key yet.
    if (!havePrevious) return;
    code = previousCode;
    Serial.println("REPEAT");
  } else {
    // Save the latest complete button code for future repeats.
    previousCode = code;
    havePrevious = true;
    Serial.print("IR code:0x");
    Serial.println(code, HEX);
  }
  // Translate the final code into a readable button label.
  printButton(code);
  delay(500);
}

Expected behavior

At 9600 baud, the supplied 21-key NEC remote prints the decoded raw code and button name; holding a key prints REPEAT and its previous name.

Experiments & Challenges

  • Press a different NEC remote and compare the reported codes without changing the wiring.

Troubleshooting

  • Unknown button: another remote can transmit different codes; this map matches the supplied 21-key remote.
  • Compile errors for IRrecv/decode_results or missing symbols: keep this project’s pinned4.1.2 snapshot and IRremote.hpp include.