๐ฆ 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(), andcode[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 viaany()andcode[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.