In this lesson I show how to practically implement an Infrared IR sensor into a Raspberry Pi Pico project. For your convenience, the code developed in the video is included below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import time from machine import Pin, freq from ir_rx.print_error import print_error from ir_rx.nec import NEC_8 IRdict ={69 : 'POWER', 70: 'MODE',71 : 'OFF', 68: 'PLAY', 64 : 'BACK', 67 : 'FORWARD', 7: 'ENTER', 21: '-', 9 : '+', 22 : 0,25 : 'LOOP', 13:'USD' , 12: 1, 24 : 2,94 : 3, 8 :4, 28 : 5, 90 : 6, 66 : 7, 82 : 8, 74 : 9} newCommand=[] beginRecord=False cmdReady=False angleString='' newBit="" irPin=17 myIR = Pin(irPin, Pin.IN) def callback(IRbit, addr, ctrl): global newCommand global beginRecord global cmdReady global IRdict if IRbit==69: beginRecord=True newCommand=[] cmdReady=False if beginRecord==True and IRbit!=-1: newCommand.append(IRdict[IRbit]) if IRbit==7: cmdReady=True IR = NEC_8(myIR, callback) # Instantiate receiver try: while True: if cmdReady==True: print(newCommand) cmdReady=False except KeyboardInterrupt: IR.close() print("Program Terminated") |