🔦 Level 2 – Sensors & IR Control

Project 2.1: "IR Remote Control"

 

🚀 Project 2.1 – IR Remote Control


🎯 What You’ll Learn

  • ✅ Goal 1: Detect IR button codes.
  • ✅ Goal 2: Control LEDs with IR remote.
  • ✅ Goal 3: Create ON/OFF logic.
  • ✅ Goal 4: Control multiple devices.
  • ✅ Goal 5: Program macro commands.

Key Ideas

  • IR Receiver: Captures remote signals.
  • Hex codes: Identify each button.
  • Logic: Decide actions based on code.
  • Outputs: LEDs or motors respond.

🧱 Blocks Glossary (used in this project)

  • import irremote → Load IR library.
  • ir_rx = irremote.NEC_RX(pin, buffer) → Setup IR receiver.
  • ir_rx.code[0] → Read received code.
  • ir_rx.any() → Check if signal exists.
  • ir_tx = irremote.NEC_TX(pin, repeat, freq) → Setup IR transmitter.
  • ir_tx.transmit(a,b,c) → Send IR command.

🧰 What You Need

PartHow many?Pin connection
D1 R321USB cable
IR Receiver1Pin 26
Remote Control1Standard NEC remote
LED1Pin 5

✅ Before You Start

  • USB cable connected
  • IR receiver wired correctly
  • Test print shows:
print("Ready!")  # Confirm serial is working

🎮 Microprojects (5 Mini Missions)


🎮 Microproject 2.1.1 – IR button code detection

Blocks used: IR input, Serial print
Block sequence:

  1. Setup IR receiver
  2. Detect button press
  3. Print hex code

MicroPython Code:

import irremote, time                         # Import IR library and time

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver on pin 26, buffer 8

while True:                                   # Infinite loop
    if ir_rx.any():                           # If any IR signal detected
        code = ir_rx.code[0]                  # Read first code
        print("IR code:", hex(code))          # Print hex code
    time.sleep(0.2)                           # Delay for stability

Reflection: Remote button codes are detected and printed.
Challenge: Press different buttons and note their hex codes.


🎮 Microproject 2.1.2 – IR LED Control

Blocks used: IR input, Digital output, Logic
Block sequence:

  1. Setup IR receiver + LED
  2. If button “1” pressed → LED ON
  3. If button “0” pressed → LED OFF

MicroPython Code:

import irremote, machine, time                # Import IR, machine, time

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver setup
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED on pin 5

while True:                                   # Infinite loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read code
        print("Received:", hex(code))         # Serial log
        if code == 0x45:                      # Button "1" hex-command
            pin5.value(1)                     # LED ON
            print("LED ON")                   # Serial log
        elif code == 0x19:                    # Button "0" hex-command
            pin5.value(0)                     # LED OFF
            print("LED OFF")                  # Serial log
    time.sleep(0.2)                           # Delay

Reflection: LED responds to remote buttons.
Challenge: Add button “2” for blinking LED.


🎮 Microproject 2.1.3 – Remote ON/OFF

Blocks used: IR input, Logic, Toggle
Block sequence:

  1. Setup IR receiver + LED
  2. Button “OK” toggles LED state

MicroPython Code:

import irremote, machine, time                # Import modules

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output

state = 0                                     # LED state variable

while True:                                   # Infinite loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read code
        print("Received:", hex(code))         # Serial log
        if code == 0x1c:                      # Button "OK"
            state = 1 - state                 # Toggle state
            pin5.value(state)                 # Apply state
            print("LED state:", state)        # Serial log
    time.sleep(0.2)                           # Delay

Reflection: LED toggles ON/OFF with “OK” button.
Challenge: Add “#” button to reset OFF.


🎮 Microproject 2.1.4 – Control of multiple LEDs

Blocks used: IR input, Digital outputs, Logic
Block sequence:

  1. Setup IR receiver + 2 LEDs
  2. Button “2” → LED1 ON
  3. Button “3” → LED2 ON

MicroPython Code:

import irremote, machine, time                # Import modules

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED1 output
pin4 = machine.Pin(4, machine.Pin.OUT)        # LED2 output

while True:                                   # Infinite loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read code
        print("Received:", hex(code))         # Serial log
        if code == 0x46:                      # Button "2"
            pin5.value(1)                     # LED1 ON
            print("LED1 ON")
        elif code == 0x47:                    # Button "3"
            pin4.value(1)                     # LED2 ON
            print("LED2 ON")
        elif code == 0x19:                    # Button "0"
            pin5.value(0); pin4.value(0)      # Both OFF
            print("LEDs OFF")
    time.sleep(0.2)                           # Delay

Reflection: Multiple LEDs controlled by remote.
Challenge: Add button “4” to turn both ON.


🎮 Microproject 2.1.5 – IR Macro Commands

Blocks used: IR input, Logic, Sequence
Block sequence:

  1. Setup IR receiver + LED
  2. Button “Up” triggers sequence

MicroPython Code:

import irremote, machine, time                # Import modules

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED output

while True:                                   # Infinite loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read code
        print("Received:", hex(code))         # Serial log
        if code == 0x18:                      # Button "Up"
            print("Macro sequence start")     # Serial log
            pin5.value(1); time.sleep(1)      # LED ON 1s
            pin5.value(0); time.sleep(1)      # LED OFF 1s
            pin5.value(1); time.sleep(2)      # LED ON 2s
            pin5.value(0)                     # LED OFF
            print("Macro sequence end")       # Serial log
    time.sleep(0.2)                           # Delay

Reflection: Remote triggers macro sequence.
Challenge: Add button “Down” for reverse sequence.


 

✨ Main Project – IR Remote Control

🔧 Blocks Steps (with glossary)

  • IR input: Detect button codes.
  • Logic: Map codes to actions.
  • Digital outputs: LEDs respond to remote.
  • Macro: Sequences triggered by special buttons.

Block sequence:

  1. Setup IR receiver.
  2. Setup LEDs.
  3. Detect button codes.
  4. Control LEDs ON/OFF.
  5. Add macro sequence.

🐍 MicroPython Code (mirroring blocks)

# Project 2.1 – IR Remote Control

import irremote, machine, time                # Import IR, machine, time

# Setup IR receiver
ir_rx = irremote.NEC_RX(26,8)                 # IR receiver on pin 26, buffer 8

# Setup LEDs
pin5 = machine.Pin(5, machine.Pin.OUT)        # LED1 on pin 5
pin4 = machine.Pin(4, machine.Pin.OUT)        # LED2 on pin 4

# State variables
led1_state = 0                                # LED1 initial state OFF
led2_state = 0                                # LED2 initial state OFF

while True:                                   # Infinite loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read first code
        print("Received:", hex(code))         # Serial log

        # Button "1" → LED1 ON
        if code == 0x45:                      # Hex-command for button 1
            pin5.value(1)                     # LED1 ON
            led1_state = 1
            print("LED1 ON")

        # Button "2" → LED2 ON
        elif code == 0x46:                    # Hex-command for button 2
            pin4.value(1)                     # LED2 ON
            led2_state = 1
            print("LED2 ON")

        # Button "0" → All OFF
        elif code == 0x19:                    # Hex-command for button 0
            pin5.value(0); pin4.value(0)      # Both LEDs OFF
            led1_state = 0; led2_state = 0
            print("LEDs OFF")

        # Button "OK" → Toggle LED1
        elif code == 0x1c:                    # Hex-command for OK
            led1_state = 1 - led1_state       # Toggle state
            pin5.value(led1_state)            # Apply state
            print("LED1 toggled:", led1_state)

        # Button "Up" → Macro sequence
        elif code == 0x18:                    # Hex-command for Up
            print("Macro sequence start")
            pin5.value(1); time.sleep(1)      # LED1 ON 1s
            pin5.value(0); time.sleep(1)      # LED1 OFF 1s
            pin4.value(1); time.sleep(2)      # LED2 ON 2s
            pin4.value(0)                     # LED2 OFF
            print("Macro sequence end")

    time.sleep(0.2)                           # Delay for stability

📖 External Explanation

This project shows how to use an IR remote to control LEDs.

  • Button codes are detected with the IR receiver.
  • Hex commands map each button to an action.
  • Logic decides ON/OFF, toggle, or macro sequence.
  • Outputs (LEDs) respond to remote control.

✨ Story Time

Imagine your robot as a smart TV 📺. Each button on the remote becomes a command: turn lights ON, toggle them OFF, or run a special sequence. With IR, you are the director of your robot’s show.


🕵️ Debugging (2 Common Problems)

🐞 Debugging 2.1.A – No IR signal detected

Problem: Remote button press not detected.
Clues: Wrong pin or buffer size.
Broken code:

ir_rx = irremote.NEC_RX(21,4)   # Wrong pin and buffer

Fixed code:

ir_rx = irremote.NEC_RX(26,8)   # Correct pin and buffer

Why it works: Correct wiring and buffer ensure detection.


🐞 Debugging 2.1.B – Incorrect codes

Problem: Button press shows wrong hex code.
Clues: Using raw codes instead of hex-command.
Broken code:

if code == 0xba45ff00:   # Raw code

Fixed code:

if code == 0x45:         # Hex-command

Why it works: Hex-command is simpler and consistent.


✅ Final Checklist

  • IR receiver setup correct.
  • Button codes detected.
  • LED ON/OFF control works.
  • Toggle logic implemented.
  • Macro sequence programmed.

📚 Extras

  • 🧠 Student tip: Try mapping buttons to different devices.
  • 🧑‍🏫 Instructor tip: Show raw vs hex codes to explain simplification.
  • 📖 Glossary: IR, hex-command, toggle, macro.
  • 💡 Mini tip: Always test with print(hex(code)) before mapping actions.
On this page