IrSwitch: remote switch uses any TV controller (IR)

After my recent tutorial (in Italian language) for the STM8S microcontroller, I decided to realize a small device using the STM8S001J (8 pins) microcontroller, in order to expand the tutorial itself. This device has a practical application: I don’t live anymore in a B&B, but in a “residence” and I’m going to solve the problems that I face in this new location. The first problem to solve is that I don’t have any switch close to my bed to turn off the lights when I go to sleep. So, I decided to make a remotely controlled switch. Well, I don’t want to build also a remote controller, so I used the one that currently works for the TV.

This remote switch is based on IR reception from any TV controller that has a protocol like the NEC or equivalent. The schematic is very simple as you can see in the picture here below (click this link to download the schematic in PDF format):

The question is: how can I control my switch without disturbing my TV (or viceversa) ? I don’t want to read a specific keycode coming from TV remote; I just want to detect that one key has been pressed (or released), I don’t mind what key.

The algorithm is very simple. Look at these steps:

– define “key pressed” event as an IR burst detected for at least 3mS (the NEC protocol has 4.5mS header, even in repeated keycode). After the header there is the data, followed by a pause (no signal) for at least 20mS.
– define “key released” event as the absence of any “key pressed” event for a certain time.

This part of the algorithm is executed in the TIM4 interrupt (1mS tick). The IRIN is the input connected to the IR receiver TSOP4838 that gives a low output when a 38KHz burst is received.

The next steps are executed in the main program (TimedActions) every 256mS:
– wait for “key pressed” event

If the “key pressed” event arises, the red led turns ON, the state machine goes to the next state and a time counter of 8 * 256 mS is started. In such time no others “key pressed” events must occur or the time counter will be restarted.

– wait for “key release” event

– start a “time window” of 8 * 256mS (with red led blinking) where you can press a key to toggle the remote switch

If a key is pressed in this time, the relay toggles and the state machine immediatly changes to the next step. In any case, this step terminates with the red led off and a delay of 4 * 256mS in which no events are taken.

– close the “time window” and restart from idle

That’s all. The remote switch will be activated by any key pressed on your TV remote control. The red led light will turn on and stay on for all the time you continuously press the key. When you release the key, the red led will blink for 2 seconds and any key pressed in such time will toggle the relay. If you don’t press any key while the led is blinking, the command will be aborted and the cycle will start again. I assumed that when you use the remote control for the TV, you often press the key for a short time (as for program change) or repeatly (such in volume control). Using the “time window” after key released is a good way to activate the remote switch without disturbing the TV and viceversa. If you have on your TV remote control some “special key” that hasn’t any relevant effect on the video, use it ! Obviously, if the TV is turned off, you can use any button excluding the “ON” 🙂 that will turn on the TV set !

The full project folder is available for download from here: IrSwitch. Please, use the 7Z program with password: eficara to decompress it. Look at the STM8 tutorial for the working environment. You have the C sources, so you will be able to improve, modify or completely change the algorithm that I developed for my specific situation. Have fun !