English
English
简体中文
日本語
pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

StackChan IR Infrared Communication

Example Program

Compilation Requirements

  • M5Stack Board Manager Version >= 3.2.2
  • Development Board Selection = M5CoreS3
  • M5StackChan Library Version >= 1.0.0
  • IRremote Library Version >= 4.5.0
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include <IRremote.hpp>
#include <M5StackChan.h>

#define IR_SEND_PIN    5      // GPIO pin for IR transmitter
#define IR_RECEIVE_PIN 10      // GPIO pin for IR receiver

// Demo parameters for NEC protocol
uint16_t address = 0x0000;     // Starting device address
uint8_t  command = 0x55;       // Starting command value
uint8_t  repeats = 0;          // Number of repeat transmissions

void setup() {
    M5StackChan.begin();                // Initialize M5Stack device
    Serial.begin(115200);      // Start serial communication at 115200 baud
    delay(200);                // Wait for serial port to stabilize
    
    // Configure Display() settings
    M5StackChan.Display().setTextFont(&fonts::FreeMonoBold9pt7b);
    M5StackChan.Display().clear();
    M5StackChan.Display().setCursor(0,0);
    M5StackChan.Display().printf("IRremote example");
    Serial.println("IRremote example");

    // Initialize IR communication
    IrReceiver.begin(IR_RECEIVE_PIN);     // Start IR receiver
    IrSender.begin(DISABLE_LED_FEEDBACK);  // Initialize IR sender without LED feedback
    IrSender.setSendPin(IR_SEND_PIN);      // Assign transmitter pin

    Serial.printf("IR Send Pin: %d, IR Recv Pin: %d\n", IR_SEND_PIN, IR_RECEIVE_PIN);
    delay(500); // Wait for hardware components to stabilize
}

void loop() {
    // 1. Send infrared signal using NEC protocol
    Serial.printf("Send NEC: addr=0x%04x, cmd=0x%02x\n", address, command);
    IrSender.sendNEC(address, command, repeats);
    
    // Update Display with transmission info
    M5StackChan.Display().fillRect(0, 20, 320, 90, TFT_BLACK);  // Clear previous content
    M5StackChan.Display().setCursor(0, 40);
    M5StackChan.Display().printf("Send NEC:\n addr=0x%04x\n cmd=0x%02x\n", address, command);

    IrReceiver.restartAfterSend();  // Re-enable receiver after transmission

    // 2. Wait for possible reflection (short-range testing)
    delay(20);  // Brief pause to allow signal reception

    // Attempt to decode received IR signal
    if (IrReceiver.decode()) {
        // Print received data to serial monitor
        Serial.printf("Received: protocol=%s, addr=0x%04x, cmd=0x%02x, raw=0x%08lx\n",
                      getProtocolString(IrReceiver.decodedIRData.protocol),
                      IrReceiver.decodedIRData.address,
                      IrReceiver.decodedIRData.command,
                      (unsigned long)IrReceiver.decodedIRData.decodedRawData);
        
        // Display received data on screen
        M5StackChan.Display().fillRect(0, 110, 320, 130, TFT_BLACK);  // Clear previous content
        M5StackChan.Display().setCursor(0, 110);
        M5StackChan.Display().printf("Received:\n protocol=%s\n addr=0x%04x\n cmd=0x%02x\n raw=0x%08lx\n",
                        getProtocolString(IrReceiver.decodedIRData.protocol),
                        IrReceiver.decodedIRData.address,
                        IrReceiver.decodedIRData.command,
                        (unsigned long)IrReceiver.decodedIRData.decodedRawData);
        
        IrReceiver.resume();  // Enable reception of next signal
    } else {
        // Handle case where no signal was received
        Serial.println("No IR received.");
        M5StackChan.Display().fillRect(0, 110, 320, 130, TFT_BLACK);  // Clear previous content
        M5StackChan.Display().setCursor(0, 110);
        M5StackChan.Display().println("No IR received.");
    }

    // Update transmission parameters for next cycle
    address += 0x0001;  // Increment device address
    command += 0x01;     // Increment command code
    repeats  = 0;        // Disable repeat frames (set >0 to test repeats)

    delay(2000);  // Main loop delay (2 seconds)
}

This program controls StackChan to send and receive infrared NEC encoding and displays related NEC encoding information on the screen.

Driver Library

On This Page