How the IR Remote Control Code Works

IR Remote Signals

The remote flashes its IR LED on/off very rapidly—at 38.5 kHz—for certain periods of time, with periods of off-time in between. The combinations of these pulses are received by the micro:bit as the number pressed, not a series of pulses. How does this work exactly?

The IR receiver is sensitive to bursts of 38.5 kHz infrared light, and responds by outputting a 5V high signal.  The Propeller I/O pin, P10 in this case, measures how long each high signal lasts.  The Propeller's cyberbot firmware interprets the high signal patterns, and then works with the tv_remote.py library to match the patterns to different buttons on your remote.

How IR_Remote_Control.py Works

Our program IR_Remote_Control.py begins by initializing the variables wL and wR to zero, and the variable img to image.HAPPY. After a quick beep, the bulk of the code is a while True: loop. Inside the loop, num = ir(10).remote() checks to see which remote button press is detected and stores the result in the variable num.  Next, a long if...elif series uses the value of num to select new values for  wL and wR as well as an image to display

The new values of wL, wR, and img are passed to the display.show(img), bot(18).servo_speed(wL) and bot(19).servo_speed (wR) calls, so the robot drives off with in a new direction with an updated display until the next new remote button press is detected.

More Actions for More Keys? — There are more buttons on the remote, so you can add more robot behaviors to your project, such as making sound effects on the piezospeaker. However, there is a catch! Keep in mind that when the cyber:bot is generating frequencies on the piezospeaker, it can’t do anything else, like check to see if you have pressed a new button on the remote. The exception is that the servos will maintain the last speed setting they received until they get a new one.