from machine import Pin
import time
IR_PIN = 15 # GPIO pin connected to IR OUT
ir = Pin(IR_PIN, Pin.IN, Pin.PULL_UP)
# NEC timing (microseconds)
START_LOW = 9000
START_HIGH = 4500
BIT_LOW = 560
BIT_HIGH_0 = 560
BIT_HIGH_1 = 1690
pulse_times = []
last_time = 0
receiving = False
def ir_callback(pin):
global last_time, pulse_times, receiving
now = time.ticks_us()
duration = time.ticks_diff(now, last_time)
last_time = now
if duration > 10000:
# New frame
pulse_times = []
receiving = True
else:
if receiving:
pulse_times.append(duration)
ir.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=ir_callback)
def decode_nec(pulses):
if len(pulses) < 66:
return None
data = 0
index = 0
# Skip start pulses
index += 2
for i in range(32):
low = pulses[index]
high = pulses[index + 1]
index += 2
if high > 1000:
data |= (1 << i)
return data
print("📡 IR Receiver Ready... Press any remote button")
while True:
if receiving and len(pulse_times) > 66:
receiving = False
code = decode_nec(pulse_times)
if code:
print("IR Code (HEX):", hex(code))
pulse_times = []
time.sleep_ms(100)