Skip to content

Project 3.1.14: Ir Remote Fan

Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

The Pico decodes a simple NEC-style IR remote command and toggles a relay-driven fan on or off.

Remote control is useful when ventilation must be changed without touching the fan area directly.

A Pico 2 W prototype with an IR receiver, relay output, LED status indicator, and low-voltage fan load.

IR command decoding, actuator control, and honest limits around single-relay fan control.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
IR remote
IR remote
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Relay
Relay
Dc fan
Dc fan
IR receiver M
IR receiver M

Circuit Connections

Component terminal Connect to Pico physical pin Notes
IR receiver VCC 3.3V output Physical pin 36 Check your module voltage rating
IR receiver GND GND Physical pin 38 Common ground
IR receiver OUT GPIO 14 GPIO 14 / physical pin 19 Digital pulse input
Relay IN GPIO 15 GPIO 15 / physical pin 20 Relay control
Relay VCC External 5V or module-rated supply Not a Pico GPIO pin Use the proper module supply
Relay GND GND Physical pin 38 Share ground unless fully isolated
LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Fan state LED
LED cathode GND Physical pin 23 or 38 Ground return

Wiring Diagram

Circuit Diagram

  3.3V                 -> IR receiver VCC
  GND                  -> IR receiver GND, relay GND, LED cathode
  IR receiver OUT      -> GPIO 14
  GPIO 15              -> relay IN
  GPIO 16              -> 220 ohm resistor -> LED anode
  External supply      -> relay-switched fan

Step-by-Step Assembly

  1. Connect the IR receiver module to Pico 3.3V, GND, and GPIO 14.
  2. Connect the relay input to GPIO 15 and power the relay module correctly.
  3. Wire the LED to GPIO 16 through a 220 ohm resistor and connect the LED cathode to GND.
  4. Test the relay without the fan load first to confirm the on and off behavior.
  5. Point the remote at the IR receiver and record the printed command value if your remote uses a different NEC code.

Testing Individual Components

Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.

  1. Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
  2. Test the input sensor: Run the IR decoder and note the printed command values from your remote before attaching the relay-controlled fan.
  3. Test the output device: Toggle the relay and LED from a simple script to confirm the load path works safely before mixing in IR decoding.
  4. Test the decision logic: Verify that only the chosen power command toggles the fan state and that repeat frames do not retrigger the relay continuously.
  5. Run the full system: Run the full system from several distances and angles with the remote and confirm the fan state always matches the LED state.
  6. Validate the prototype: Test in bright light and from different room positions to see how environmental conditions affect the IR receiver.
  7. Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.

Full Project Code

After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

from machine import Pin, time_pulse_us
import time

IR_PIN = 14
RELAY_PIN = 15
LED_PIN = 16
POWER_CMD = 69
TOGGLE_GAP_MS = 400

ir = Pin(IR_PIN, Pin.IN, Pin.PULL_UP)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
fan_on = False
last_toggle_ms = -TOGGLE_GAP_MS


def set_fan(state):
    relay.value(1 if state else 0)
    led.value(1 if state else 0)


def read_nec_command():
    if ir.value() == 1:
        return None

    lead_low = time_pulse_us(ir, 0, 12000)
    if lead_low < 8000 or lead_low > 10000:
        return None

    lead_high = time_pulse_us(ir, 1, 6000)
    if 2000 < lead_high < 3000:
        return "REPEAT"
    if lead_high < 4000 or lead_high > 5000:
        return None

    value = 0
    for bit_index in range(32):
        low = time_pulse_us(ir, 0, 1500)
        high = time_pulse_us(ir, 1, 3000)
        if low < 200 or low > 1000:
            return None
        bit = 1 if high > 1000 else 0
        value |= bit << bit_index

    command = (value >> 16) & 0xFF
    command_inv = (value >> 24) & 0xFF
    if (command ^ command_inv) != 0xFF:
        return None
    return command


set_fan(False)
print("IR remote fan control ready")

while True:
    command = read_nec_command()
    if command is None:
        time.sleep_ms(20)
        continue

    if command == "REPEAT":
        time.sleep_ms(20)
        continue

    print("IR command:", command)
    now = time.ticks_ms()

    if command == POWER_CMD and time.ticks_diff(now, last_toggle_ms) > TOGGLE_GAP_MS:
        fan_on = not fan_on
        set_fan(fan_on)
        last_toggle_ms = now
        print("Fan state:", "ON" if fan_on else "OFF")

    time.sleep_ms(20)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Pin and state setup Defines the IR input, relay output, LED output, and the known remote command The relay and LED must mirror the same fan state to make validation easy Update POWER_CMD after reading your own remote if needed
read_nec_command Measures IR pulse timing and reconstructs a NEC-style command byte This is the communication layer of the project and the key technical difference from a simple button input If your remote timing differs, print commands first and then adjust only after measurement
set_fan Applies the chosen fan state to both relay and LED A single helper keeps actuator behavior consistent If your relay module is active low, invert the relay output logic here
Main loop Ignores repeat frames, prints commands, and toggles the fan only on the power code This prevents accidental rapid switching and gives a useful debug trail in the Shell Change TOGGLE_GAP_MS if your remote sends repeated toggles too quickly

Expected Result

The serial monitor reports the current reading or state clearly.

The hardware output responds when the decision logic changes state.

Subsystem behavior matches the thresholds, timing, or rules described in the document.

Validation Checks

  • Normal condition test: power on the system and confirm the fan starts off
  • Sensor reading validation: press the remote button and confirm a repeatable command number appears in the serial monitor
  • Output response validation: test the relay first without the fan load, then connect the fan after safe switching is confirmed
  • False trigger test: press other buttons on the remote and confirm they do not change the fan state
  • Limitation test: explain that a single relay can only switch the fan on and off, not provide smooth speed control

Deployment and Limitations

  • This prototype could be used in school, farm, or sanitation demonstrations with safe low-voltage loads
  • Before deployment, the load wiring, enclosure, and power design need careful review
  • Actuator systems need maintenance planning because relay wear and sensor drift affect reliability

Troubleshooting

Problem Possible Cause Solution
No IR commands print Receiver power or signal wiring is wrong Confirm the IR receiver pinout and point the remote directly at the sensor during testing
Wrong button toggles the fan POWER_CMD does not match your remote Print command values and replace POWER_CMD with the actual button code from your remote
Relay chatters Repeat frames are being treated as new commands Keep the repeat-frame filter and the toggle-gap check in place
Fan never runs Relay output works but the load wiring or supply is wrong Test the fan and external power supply separately before reconnecting the relay path