This blog post will explain how to detect keypresses from an IR remote. First, the required libraries need to be imported. A variable named pin_ir has to be initialized to pin 0 and set as an input. The callback function is declared and it sorts out repeat codes from codes from button presses. NEC remotes send out repeat codes. The callback function will call the decode function to return the value associated with the keypress on an ir remote. The variable ir will initialize the ir receiver using variables pin_ir and callback as arguments.
from machine import Pin,freq
from picobricks import NEC_16
pin_ir = Pin(0, Pin.IN)
def decode(data):
if data == 0x07:
return “Button 7”
if data == 0x08:
return “Left”
if data == 0x09:
return “Button 9”
if data == 0x0D:
return “Button #”
if data == 0x15:
return “Button 8”
if data == 0x16:
return “Button *”
if data == 0x18:
return “Up”
if data == 0x19:
return “Button 0”
if data == 0x1C:
return “Ok”
if data == 0x40:
return “Button 5”
if data == 0x43:
return “Button 6”
if data == 0x44:
return “Button 4”
if data == 0x45:
return “Button 1”
if data == 0x47:
return “Button 3”
if data == 0x52:
return “Down”
if data == 0x46:
return “Button 2”
if data == 0x5A:
return “Right”
return “ERROR”
# User callback
def callback(data, addr, ctrl):
if data < 0:
pass
else:
print(decode(data))
ir = NEC_16(pin_ir, callback)
The Picobricks zero to hero kit was used to write this blog post.