Using IR remotes for controlling microcontrollers and computers

Check out the new site at https://rkblog.dev.

TV remotes have a LED that glows in the infrared. The TV has a light detector that detects this light and decodes a signal embedded in it. The LED doesn't just glow but pulses (PWM) at a fixed rate, usually 38KHz. The receiver has a demodulator that will react only to light pulsing at such frequency. Within such modulated light some data can be transferred allowing TV to recognize signals to change volume, pick channel etc.

IR transmitters and receivers can be used in our project - to control a computer or a robot with a microcontroller and more. You just need some code and those two parts connected to GPIO. In this article I'll show a basic setup for PyMCU.

IR transmitter and receiver

Remotes may use different protocols of sending data. PyMCU supports only one of them - Sony SIRC, which can be found in most (if not all) Sony remotes. There are Arduino libraries that support more/all common protocols, but for pymcu we have to stick with Sony-compatible remotes. I bought a cheap replacement for a Sony TV remote - PIL0112 and that worked nicely with this microcontroller.

To receive signal we need a receiver that is also cheap - either receiver or receiver on a small PCB with pins for ease of use. Depending on shop you may find different names and board. I used Keyes KY-022 board. It has 3 pins - GND, VCC and signal pin (connect to a digital pin).

PIL0112 remote and Keyes KY-022 receiver connected to pymcu

PIL0112 remote and Keyes KY-022 receiver connected to pymcu

Receiving IR signals using PyMCU

The board has one simple method for receiving (and decoding) IR signals - irDecode:

import pymcu

mb = pymcu.mcuModule()

mb.digitalState(1, 'input')
while True:
    ir = mb.irDecode(1)
    if ir != 255:
        print ir

I've connected the signal pin to D1 on PyMCU so I had to set the microcontroller pin to receive mode. Then the code reads the value from that pin with irDecode. 255 is the default value returned when nothing is received. When you press a button on the remote you will see a number corresponding to the button. You can create a map of values for every button and then assign actions for given button. Note that pressing a button for a longer time will cause the receiver to receive the signal multiple times.

RkBlog

Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact