Project 2.7: "Advanced Remote Control"
๐ Project 2.7 โ Advanced Remote Control
ย
๐ฏ What Youโll Learn
- โ Goal 1: Use an IR remote to send commands to the robot.
- โ Goal 2: Control RGB lights and buzzer with remote signals.
- โ Goal 3: Create multiple operating modes and master control.
Key Ideas
- Short definition: IR remotes send coded signals that the robot can read.
- Realโworld link: TVs, drones, and robots use IR remotes for wireless control.
๐งฑ Blocks Glossary
- IR receive:
ir_rx = irremote.NEC_RX(pin, buffer)โ Read IR remote codes. - IR code:
ir_rx.code[0]โ Value of pressed button. - RGB control:
pwm2 = machine.PWM(machine.Pin(2))โ Control LED colors. - Buzzer:
midi = music.MIDI(26)โ Play tones. - Serial print:
print("...")โ Show feedback. - Loop:
while True:โ Continuous monitoring. - if/else: Decide action based on IR code.
๐งฐ What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable |
| IR receiver | 1 | Pin 18 |
| RGB LED | 1 | Pins 2, 4, 5 |
| Buzzer | 1 | Pin 26 |
๐ Wiring tip: Connect IR receiver signal to pin 18, RGB LED to pins 2/4/5, buzzer to pin 26.
๐ Pin map snapshot: IR=18, RGB=2/4/5, Buzzer=26.
โ Before You Start
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 2.7.1 โ Multiple device commands
Goal: Control RGB and buzzer with different IR buttons.
MicroPython Code:
# Multiple device commands
# This program uses IR remote buttons to control RGB LEDs and buzzer
import machine, time, music, irremote # Import libraries for pins, time, buzzer, and IR
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver on pin 18 with buffer size 8
pwm2 = machine.PWM(machine.Pin(2)) # Setup Red LED on pin 2 with PWM
pwm4 = machine.PWM(machine.Pin(4)) # Setup Green LED on pin 4 with PWM
pwm5 = machine.PWM(machine.Pin(5)) # Setup Blue LED on pin 5 with PWM
midi = music.MIDI(26) # Setup buzzer on pin 26
print("IR multiple commands ready") # Serial start message
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read the first code from buffer
print("Code:", code) # Print code to serial
if code == 0xA90: # Example button code
pwm2.duty(512) # Turn Red LED ON
elif code == 0xA91: # Another button code
pwm4.duty(512) # Turn Green LED ON
elif code == 0xA92: # Another button code
midi.pitch_time(440, 200) # Play buzzer beep
time.sleep(0.1) # Small delay for stability
Reflection: Each button controls a different device.
Challenge: Add button for Blue LED.
๐ฎ Microproject 2.7.2 โ Remote operating modes
Goal: Switch between โLight modeโ and โSound modeโ.
MicroPython Code:
# Remote operating modes
# This program switches between Light mode and Sound mode using IR remote
import machine, time, music, irremote # Import libraries
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver
pwm2 = machine.PWM(machine.Pin(2)) # Red LED setup
midi = music.MIDI(26) # Buzzer setup
mode = 0 # Start in mode 0 (Light mode)
print("Remote modes ready") # Serial start message
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read code
print("Code:", code) # Print code
if code == 0xA93: # Button toggles mode
mode = 1 - mode # Switch between 0 and 1
print("Mode:", mode) # Print current mode
if mode == 0: # Light mode
pwm2.duty(512) # Red LED ON
else: # Sound mode
midi.pitch_time(523, 200) # Play buzzer tone
time.sleep(0.1) # Delay
Reflection: Remote can change operating modes.
Challenge: Add third mode for both light + sound.
๐ฎ Microproject 2.7.3 โ IR Configuration
Goal: Print codes of all buttons to configure remote.
MicroPython Code:
# IR Configuration
# This program prints IR remote button codes to serial monitor
import irremote, time # Import IR library and time
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver on pin 18
print("IR configuration ready") # Serial start message
while True: # Infinite loop
if ir_rx.any(): # If IR signal detected
code = ir_rx.code[0] # Read first code from buffer
print("Button code:", hex(code)) # Print code in hex format
time.sleep(0.1) # Delay for stability
Reflection: You can discover codes of each button.
Challenge: Write down codes for at least 5 buttons.
๐ฎ Microproject 2.7.4 โ Programmed sequences
Goal: Remote button triggers RGB + buzzer sequence.
MicroPython Code:
# Programmed sequences
# This program runs a sequence of RGB lights and buzzer when IR button pressed
import machine, time, music, irremote # Import libraries
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver
pwm2 = machine.PWM(machine.Pin(2)) # Red LED
pwm4 = machine.PWM(machine.Pin(4)) # Green LED
pwm5 = machine.PWM(machine.Pin(5)) # Blue LED
midi = music.MIDI(26) # Buzzer
print("Programmed sequence ready") # Serial start message
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read code
if code == 0xA94: # Example button triggers sequence
pwm2.duty(512); time.sleep(0.2) # Red ON
pwm4.duty(512); time.sleep(0.2) # Green ON
pwm5.duty(512); time.sleep(0.2) # Blue ON
midi.pitch_time(440, 200) # Play buzzer
print("Sequence executed") # Serial confirmation
time.sleep(0.1) # Delay
Reflection: Remote can trigger a full sequence.
Challenge: Add second button for reverse sequence.
ย
๐ฎ Microproject 2.7.5 โ Remote Master Control (continuaciรณn y completo)
Goal: One button turns everything OFF.
Blocks used: IR receive, PWM outputs, buzzer, if/else.
MicroPython Code:
# Remote Master Control
# This program turns all devices OFF with one IR button
import machine, time, music, irremote # Import libraries for pins, time, buzzer, and IR
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver on pin 18 with buffer size 8
pwm2 = machine.PWM(machine.Pin(2)) # Setup Red LED on pin 2
pwm4 = machine.PWM(machine.Pin(4)) # Setup Green LED on pin 4
pwm5 = machine.PWM(machine.Pin(5)) # Setup Blue LED on pin 5
midi = music.MIDI(26) # Setup buzzer on pin 26
print("Master control ready") # Print start message to serial monitor
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read the first code from buffer
if code == 0xA95: # Example Master OFF button code
pwm2.duty(0) # Turn Red LED OFF
pwm4.duty(0) # Turn Green LED OFF
pwm5.duty(0) # Turn Blue LED OFF
print("All devices OFF") # Print confirmation message
time.sleep(0.1) # Small delay for stability
Reflection: Remote can shut down all devices at once.
Challenge: Add Master ON button to turn all devices ON together.
โจ Main Project โ Advanced Remote Control System
Goal: Combine all microprojects into one complete remote control system.
MicroPython Code:
# Project 2.7 โ Advanced Remote Control (Main Project)
# This program uses IR remote to control RGB LEDs and buzzer with multiple modes and master control
import machine, time, music, irremote # Import libraries
ir_rx = irremote.NEC_RX(18,8) # Setup IR receiver on pin 18
pwm2 = machine.PWM(machine.Pin(2)) # Red LED setup
pwm4 = machine.PWM(machine.Pin(4)) # Green LED setup
pwm5 = machine.PWM(machine.Pin(5)) # Blue LED setup
midi = music.MIDI(26) # Buzzer setup
print("Advanced Remote Control ready") # Serial start message
while True: # Infinite loop
if ir_rx.any(): # If IR signal received
code = ir_rx.code[0] # Read code
print("Code:", hex(code)) # Print code in hex format
if code == 0xA90: # Button for Red LED
pwm2.duty(512) # Red ON
elif code == 0xA91: # Button for Green LED
pwm4.duty(512) # Green ON
elif code == 0xA92: # Button for Blue LED
pwm5.duty(512) # Blue ON
elif code == 0xA93: # Button for buzzer
midi.pitch_time(440, 200) # Play beep
elif code == 0xA94: # Button for sequence
pwm2.duty(512); time.sleep(0.2) # Red ON
pwm4.duty(512); time.sleep(0.2) # Green ON
pwm5.duty(512); time.sleep(0.2) # Blue ON
midi.pitch_time(440, 200) # Play buzzer
print("Sequence executed") # Serial confirmation
elif code == 0xA95: # Master OFF button
pwm2.duty(0); pwm4.duty(0); pwm5.duty(0) # All LEDs OFF
print("Master OFF") # Serial confirmation
time.sleep(0.1) # Delay for stability
๐ External Explanation
- What it teaches: How to control multiple devices with an IR remote.
- Why it works: Each button sends a unique code; the program matches codes to actions.
- Key concept: Inputs (IR remote) control outputs (RGB LEDs + buzzer).
โจ Story Time
Your robot is now like a smart TV ๐ฎ. With the remote, you can change lights, play sounds, run sequences, and shut everything down with one button.
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 2.7.A โ Command conflicts
Problem: Two buttons trigger the same device unexpectedly.
Clues: Wrong code values in if statements.
Broken code:
if code == 0xA90:
pwm2.duty(512)
if code == 0xA90: # Duplicate condition
pwm4.duty(512)
Fixed code:
if code == 0xA90:
pwm2.duty(512)
elif code == 0xA91: # Correct unique condition
pwm4.duty(512)
Why it works: Each button must have a unique code.
Avoid next time: Always check codes with IR Configuration project first.
๐ Debugging 2.7.B โ Slow remote response
Problem: Remote feels laggy.
Clues: Delay too long in loop.
Broken code:
time.sleep(1) # Delay too long
Fixed code:
time.sleep(0.1) # Shorter delay for faster response
Why it works: Shorter delay makes loop check signals more often.
Avoid next time: Keep delays small (0.05โ0.1 seconds).
โ Final Checklist
- I tested each button and saw correct device response.
- RGB LEDs respond to individual buttons.
- Buzzer plays sound when button pressed.
- Sequence runs correctly.
- Master OFF shuts down all devices.
๐ Extras
- ๐ง Student tip: Use IR Configuration project to map all button codes.
- ๐งโ๐ซ Instructor tip: Encourage students to design their own remote layouts.
- ๐ Glossary: IR remote, code, PWM, duty cycle, sequence.
- ๐ก Mini tips:
- Always print codes to confirm button mapping.
- Use
elifto avoid conflicts. - Keep delays short for smooth control.