๐Ÿ”ฆ Level 2 โ€“ Sensors & IR Control

Project 2.3: "IR Controlled RGB"

Project 2.3 โ€“ IR Controlled RGB

ย 

๐ŸŽฏ What youโ€™ll learn

  • Detect IR remote button codes.
  • Control RGB LED channels (Red, Green, Blue).
  • Mix colors by combining channels.
  • Trigger automatic color sequences.

๐Ÿงฑ Blocks glossary used

  • import irremote โ†’ Load IR library.
  • ir_rx = irremote.NEC_RX(26,8) โ†’ Setup IR receiver (pin 26, buffer 8).
  • ir_rx.any() โ†’ Check if signal exists.
  • ir_rx.code[0] โ†’ Read received code.
  • pinX = machine.Pin(X, machine.Pin.OUT) โ†’ Setup LED channel.
  • pinX.value(1) โ†’ Turn ON channel.
  • pinX.value(0) โ†’ Turn OFF channel.
  • time.sleep() โ†’ Delay.

๐Ÿงฐ What you need

  • D1 R32 board (USB connected).
  • IR receiver on pin 26.
  • RGB LED with resistors (R=5, G=4, B=2).

โœ… Before you start

print("Ready!")  # Confirm serial monitor works

๐ŸŽฎ Microprojects


๐ŸŽฎ Microproject 2.3.1 โ€“ IR Red color control

Idea: Button โ€œ1โ€ turns Red ON. Button โ€œ#โ€ turns all OFF.
Buttons: โ€œ1โ€ = 0x45, โ€œ#โ€ = 0x0d.

# IR Red control: 1 โ†’ Red ON, # โ†’ RGB OFF

import irremote, machine, time                # Import IR library, pin control, and time

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver on pin 26, buffer size 8
pin5 = machine.Pin(5, machine.Pin.OUT)        # Red channel on pin 5
pin4 = machine.Pin(4, machine.Pin.OUT)        # Green channel on pin 4
pin2 = machine.Pin(2, machine.Pin.OUT)        # Blue channel on pin 2

print("IR Red control ready")                 # Serial start message

while True:                                   # Main loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read first code
        print("Code:", hex(code))             # Print code in hex

        if code == 0x45:                      # Button "1"
            pin5.value(1)                     # Red ON
            print("Red ON")                   # Serial confirmation

        elif code == 0x0d:                    # Button "#"
            pin5.value(0); pin4.value(0); pin2.value(0)  # All OFF
            print("RGB OFF")                  # Serial confirmation

    time.sleep(0.2)                           # Delay
  • Reflection: You can now control Red with a single button.
  • Challenge: Add button โ€œ0โ€ (0x19) to turn Red OFF only, without affecting Green or Blue.

๐ŸŽฎ Microproject 2.3.2 โ€“ IR Green color control

Idea: Button โ€œ2โ€ turns Green ON. Button โ€œ#โ€ turns all OFF.
Buttons: โ€œ2โ€ = 0x46, โ€œ#โ€ = 0x0d.

# IR Green control: 2 โ†’ Green ON, # โ†’ RGB OFF

import irremote, machine, time

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver setup
pin5 = machine.Pin(5, machine.Pin.OUT)        # Red channel
pin4 = machine.Pin(4, machine.Pin.OUT)        # Green channel
pin2 = machine.Pin(2, machine.Pin.OUT)        # Blue channel

print("IR Green control ready")

while True:
    if ir_rx.any():
        code = ir_rx.code[0]
        print("Code:", hex(code))

        if code == 0x46:                      # Button "2"
            pin4.value(1)                     # Green ON
            print("Green ON")

        elif code == 0x0d:                    # Button "#"
            pin5.value(0); pin4.value(0); pin2.value(0)
            print("RGB OFF")

    time.sleep(0.2)
  • Reflection: Green is now mapped to button โ€œ2โ€.
  • Challenge: Add button โ€œ*โ€ (0x16) to blink Green ON/OFF quickly.

๐ŸŽฎ Microproject 2.3.3 โ€“ IR Blue color control

Idea: Button โ€œ3โ€ turns Blue ON. Button โ€œ#โ€ turns all OFF.
Buttons: โ€œ3โ€ = 0x47, โ€œ#โ€ = 0x0d.

# IR Blue control: 3 โ†’ Blue ON, # โ†’ RGB OFF

import irremote, machine, time

ir_rx = irremote.NEC_RX(26,8)
pin5 = machine.Pin(5, machine.Pin.OUT)        # Red
pin4 = machine.Pin(4, machine.Pin.OUT)        # Green
pin2 = machine.Pin(2, machine.Pin.OUT)        # Blue

print("IR Blue control ready")

while True:
    if ir_rx.any():
        code = ir_rx.code[0]
        print("Code:", hex(code))

        if code == 0x47:                      # Button "3"
            pin2.value(1)                     # Blue ON
            print("Blue ON")

        elif code == 0x0d:                    # Button "#"
            pin5.value(0); pin4.value(0); pin2.value(0)
            print("RGB OFF")

    time.sleep(0.2)
  • Reflection: Blue is now controlled by button โ€œ3โ€.
  • Challenge: Add button โ€œDownโ€ (0x52) to turn Blue OFF only.

๐ŸŽฎ Microproject 2.3.4 โ€“ Custom color mix

Idea: Buttons โ€œ1โ€, โ€œ2โ€, โ€œ3โ€ activate R/G/B. โ€œ#โ€ clears all.
Buttons: โ€œ1โ€=0x45, โ€œ2โ€=0x46, โ€œ3โ€=0x47, โ€œ#โ€=0x0d.

# IR Color mix: 1 โ†’ R ON, 2 โ†’ G ON, 3 โ†’ B ON, # โ†’ RGB OFF

import irremote, machine, time

ir_rx = irremote.NEC_RX(26,8)
pin5 = machine.Pin(5, machine.Pin.OUT)        # Red
pin4 = machine.Pin(4, machine.Pin.OUT)        # Green
pin2 = machine.Pin(2, machine.Pin.OUT)        # Blue

print("IR Color mix ready")

while True:
    if ir_rx.any():
        code = ir_rx.code[0]
        print("Code:", hex(code))

        if code == 0x45:                      # Button "1"
            pin5.value(1)                     # Red ON
            print("R ON")

        elif code == 0x46:                    # Button "2"
            pin4.value(1)                     # Green ON
            print("G ON")

        elif code == 0x47:                    # Button "3"
            pin2.value(1)                     # Blue ON
            print("B ON")

        elif code == 0x0d:                    # Button "#"
            pin5.value(0); pin4.value(0); pin2.value(0)
            print("RGB OFF")

    time.sleep(0.2)
  • Reflection: Combining channels creates new colors (R+G=Yellow, G+B=Cyan, R+B=Magenta).
  • Challenge: Add button โ€œOKโ€ (0x1c) to turn ON all three channels (White).

๐ŸŽฎ Microproject 2.3.5 โ€“ Automatic color effects

Idea: Button โ€œUpโ€ runs a short RGB sequence.
Button: โ€œUpโ€ = 0x18.

# IR Auto color effects: Up โ†’ run RGB sequence

import irremote, machine, time                # Import IR, pin control, and time

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver on pin 26
pin5 = machine.Pin(5, machine.Pin.OUT)        # Red channel
pin4 = machine.Pin(4, machine.Pin.OUT)        # Green channel
pin2 = machine.Pin(2, machine.Pin.OUT)        # Blue channel

print("IR Auto effects ready")                # Serial start message

while True:                                   # Main loop
    if ir_rx.any():                           # If IR signal detected
        code = ir_rx.code[0]                  # Read first code
        print("Code:", hex(code))             # Show code in hex

        if code == 0x18:                      # If code is "Up"
            print("Sequence start")           # Serial sequence start

            pin5.value(1); time.sleep(0.5)    # Red ON
            pin5.value(0); time.sleep(0.2)    # Red OFF
            pin4.value(1); time.sleep(0.5)    # Green ON
            pin4.value(0); time.sleep(0.2)    # Green OFF
            pin2.value(1); time.sleep(0.5)    # Blue ON
            pin2.value(0); time.sleep(0.2)    # Blue OFF

            pin5.value(1); pin4.value(1); time.sleep(0.5)  # Yellow (R+G) ON
            pin5.value(0); pin4.value(0)                   # Yellow OFF

            pin4.value(1); pin2.value(1); time.sleep(0.5)  # Cyan (G+B) ON
            pin4.value(0); pin2.value(0)                   # Cyan OFF

            print("Sequence end")             # Serial sequence end

    time.sleep(0.2)                           # Small delay
  • Reflection: A single โ€œUpโ€ press runs a full RGB show.
  • Challenge: Add button โ€œDownโ€ (0x52) to run the sequence in reverse order.

โœจ Main Project โ€“ IR Controlled RGB

๐Ÿ”ง Blocks steps

  • IR input: Read codes with NEC_RX(26,8), any(), and code[0].
  • RGB outputs: Control pins Red=5, Green=4, Blue=2.
  • Logic mapping: Buttons โ†’ color actions and a sequence.
  • Reset: Use โ€œ#โ€ to turn all channels OFF.

๐Ÿ MicroPython code (lineโ€‘byโ€‘line comments)

# Project 2.3 โ€“ IR Controlled RGB (Main Project)

import irremote, machine, time                # Import IR, pin control, and time

ir_rx = irremote.NEC_RX(26,8)                 # IR receiver on pin 26 with buffer size 8
pin5 = machine.Pin(5, machine.Pin.OUT)        # Red channel on pin 5
pin4 = machine.Pin(4, machine.Pin.OUT)        # Green channel on pin 4
pin2 = machine.Pin(2, machine.Pin.OUT)        # Blue channel on pin 2

print("IR RGB Main ready")                    # Serial start message

while True:                                   # Main loop
    if ir_rx.any():                           # Check if IR signal is available
        code = ir_rx.code[0]                  # Read the first code
        print("Code:", hex(code))             # Show the code in hex

        if code == 0x45:                      # Button "1"
            pin5.value(1)                     # Red ON
            print("R ON")

        elif code == 0x46:                    # Button "2"
            pin4.value(1)                     # Green ON
            print("G ON")

        elif code == 0x47:                    # Button "3"
            pin2.value(1)                     # Blue ON
            print("B ON")

        elif code == 0x0d:                    # Button "#"
            pin5.value(0); pin4.value(0); pin2.value(0)  # All OFF
            print("RGB OFF")

        elif code == 0x18:                    # Button "Up"
            print("Macro start")

            pin5.value(1); time.sleep(0.5)    # Red ON
            pin5.value(0); time.sleep(0.2)    # Red OFF
            pin4.value(1); time.sleep(0.5)    # Green ON
            pin4.value(0); time.sleep(0.2)    # Green OFF
            pin2.value(1); time.sleep(0.5)    # Blue ON
            pin2.value(0); time.sleep(0.2)    # Blue OFF

            pin5.value(1); pin4.value(1); time.sleep(0.5)  # Yellow ON
            pin5.value(0); pin4.value(0)                   # Yellow OFF

            pin4.value(1); pin2.value(1); time.sleep(0.5)  # Cyan ON
            pin4.value(0); pin2.value(0)                   # Cyan OFF

            print("Macro end")

    time.sleep(0.2)                           # Delay for stability
  • Reflection: This final program lets you turn each color ON with โ€œ1โ€“3โ€, clear with โ€œ#โ€, and run a short RGB effect with โ€œUpโ€.
  • Challenge: Add button โ€œOKโ€ (0x1c) to turn ON all three channels (White).

๐Ÿ•ต๏ธ Debugging

๐Ÿž Debugging 2.3.A โ€“ Incorrect mixed colors

  • Symptom: You expect yellow, but you only see red or green.
  • Cause: One channel didnโ€™t turn ON, or wiring is wrong.
  • Fix:
pin5.value(1)   # Red ON
pin4.value(1)   # Green ON
# Check wiring: Red=5, Green=4, Blue=2, with proper resistors
  • Why it works: Yellow is made by Red + Green. Both must be ON and wired correctly.

๐Ÿž Debugging 2.3.B โ€“ Unequal intensity

  • Symptom: One color looks much brighter than the others.
  • Cause: LED hardware differences or resistor values.
  • Fix (digital only):
# Use similar resistor values on R, G, and B for balanced brightness.
  • Why it works: Balanced resistors equalize current and brightness.

โœ… Final checklist

  • IR receiver: NEC_RX(26,8) created, codes read via any() and code[0].
  • Buttons mapped: 1โ†’R, 2โ†’G, 3โ†’B, #โ†’OFF, Upโ†’macro.
  • RGB wiring: R=5, G=4, B=2 connected with resistors.
  • Serial feedback: Every action prints a clear message.
  • Stable loop: Short delay keeps inputs readable and smooth.

๐Ÿ“š Extras

  • Student tip: Write down your favorite hex codes with print(hex(code)) before mapping actions.
  • Instructor tip: Demonstrate additive color mixing by turning ON two channels at the same time.
  • Glossary: IR code, hex, digital output, macro, RGB.
  • Mini tip: If nothing happens, check the IR receiverโ€™s signal pin on 26 and make sure all grounds are connected together.
On this page