This is only a preview of the August 2018 issue of Silicon Chip. You can view 41 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Brainwave Monitor – see what’s happening in your brain":
Items relevant to "Miniature, high performance sound effects module":
Items relevant to "Turn any PC into a media centre – with remote control!":
Items relevant to "Bedroom (or any room!) no-connection door alarm":
Purchase a printed copy of this issue for $10.00. |
Control your computer with
an infrared remote
control
By Tim Blythman
Don’t have a “Smart TV”? Even if you do,
you may want to connect a computer to your
TV for playing videos, music, games, viewing
photos, web browsing and so on. This is known as
a home theatre PC (HTPC). But operating a keyboard and mouse from the
couch is clumsy. Why not use an infrared remote control for important
functions like play, pause, next track, etc? It’s surprisingly easy to do!
L
ike many tech-savvy people, I have
a computer hooked up to my TV. I
use it mainly for watching DVDs and
YouTube videos on the big screen. I
can even play games or surf the net.
But if I’m sitting back watching a
movie and I want to pause it to get a
drink, I don’t want to fiddle around
with a wireless mouse or keyboard.
I have trouble with my wireless
mouse since its range is only just good
enough to reach from the couch to the
TV, so it doesn’t work reliably. And I
often forget to turn it off after using
it, prematurely flattening its batteries.
An infrared remote control is far
better suited to that sort of task and
you probably already have one (or
many!) at hand, which are likely to
have some spare buttons which you
could re-assign for various purposes.
It’s so much easier to just pick it up
and press a button.
You may already have the parts
needed to build this project. There’s
hardly any assembly required since it
only involves two components. You
can use it with a wide range of remote
controls, including inexpensive generic programmable remotes.
How does it work?
If you’re familiar with the Arduino
Leonardo’s capabilities, you might
You might own a HTPC, like the ASUS PB40, or have built your own to use with
a TV. While useful, it can be a pain to control using traditional means. That's
why we've come up with a way to use a standard IR remote to control it.
66
Silicon Chip
Australia’s electronics magazine
have an inkling as to how we achieve
the computer interface. The Leonardo has a USB interface and it can be
set up to appear, from the computer’s
point of view, as a USB keyboard and/
or mouse. This is also true of other
ATmega32U4-based Arduino boards;
there are a few.
So then we just need to arrange to
receive infrared commands and we
can translate them into keystrokes or
mouse movements/clicks.
The Leonardo remains attached to
the computer’s USB port (you can even
use it with a Raspberry Pi) so it doesn’t
need any external source of power.
The only difficulty is knowing what
codes to expect from the remote control. For this, we can temporarily set
up the Leonardo to tell us what codes
it is receiving. We’ve already done this
with a remote control that’s available
from Jaycar so you can simply use the
codes we provide and get the remote
control up and running in minutes.
It sounds simple, but the devil is
in the detail. What keystrokes do we
need to emulate? And how do we
send them?
Hardware
As we mentioned, we can use pretty
much any ATmega32U4-based Arduino
siliconchip.com.au
allow us to emulate a keyboard (github.
com/arduino-libraries/Keyboard) and
a mouse (github.com/arduino-libraries
/Mouse). The only remaining hard part
is deciding which infrared code corresponds to which action.
With most of the complex function
hidden in the libraries, our software
sketch mainly deals with reading
the codes from the infrared receiver
library and then feeding the appropriate actions to the keyboard and mouse
emulation libraries.
Construction
This remote control from Jaycar
Electronics (XC3718) has 21
keys, each of which generates a
different code based on the NEC IR
protocol. These codes are detected
and converted into keystrokes or
mouse actions.
board, including the “Leonardo” or
the smaller “Leostick” version that’s
available from Jaycar.
If space is at a premium, there is a
variant known as a “Micro”, and an
even smaller (clone) version, which
can be found under the name “Beetle”. The Beetle isn’t much bigger than
most other USB dongles, so should fit
just about anywhere.
A 3-pin IR receiver module can then
be attached to the Arduino board, so
we can now receive the signals from
our remote control.
Software
Fortunately, there are a number of
libraries available that already do most
of the hard work for us. The first one
(IRremote; github.com/z3t0/ArduinoIRremote) is used to receive and decode the infrared signals, giving us a
different code for each button that’s
pressed on the remote control. This
is a great library that can also be used
to transmit infrared signals.
The two other libraries we’re using
The Beetle can
just be plugged
into a USB
type-A port on
a computer,
as is.
siliconchip.com.au
There isn’t much to the hardware
so it makes sense to assemble it first.
The hardware can be used for figuring
out what code is generated by each remote control button, and then re-used
as the actual IR/keyboard/mouse interface. We built two prototypes using
different Arduino modules as follows.
A tiny Beetle
The Arduino Beetle variant is a
minimal ATmega32U4-based board
designed by DFRobot. Although it
hasn’t been around for long, it has
been “cloned” and these clones are
available from many online stores. We
don’t even need to solder the supplied
headers to it. We can simply solder the
infrared receiver straight to the pads
on the board.
When complete, the final unit is
smaller than most USB flash drives.
The infrared receiver’s GND pin is soldered to the GND pad on the PCB, the
Vcc pin to the 5V pad and the DATA
pin to digital input D11. The infrared
receiver library can use any digital pin
as the input but this is the one that we
have chosen.
Most infrared receiver modules
use the same pinout (including those
sold by Jaycar and Altronics) so you
can most likely solder yours as shown
here.
But if you're using a receiver from
a different source, it would be a
There are a few
variants of Arduino
compatible boards
that will work with
this project, like the
ProMicro pictured.
Compatible boards
need to be based on
the ATmega32U4
processor which
has a built-in USB
interface.
good idea to double-check the data
sheet. Looking at the lens from the
front with the leads at the bottom,
the pins from left-to-right are DATA,
GND and Vcc.
If you like, you can carefully twist
the receiver so that it will face the
right way (towards the couch you are
comfortably sitting on) when plugged
into the computer. As long as the
wires don’t touch, you should have
no troubles. Use heatshrink insulation if in doubt.
A larger variant
If you have an Arduino-compatible
Leonardo or Micro board lying around
for prototyping, it is entirely possible
to put this project together without
any soldering. Perhaps you just want
to test out that it does what you want
before assembling something more
permanent.
In that case, we can use an Arduinocompatible module and some jumper
leads to quickly put everything together. It may not be as compact but
if your other family members don’t
mind bits of electronics sitting near
the TV, it will work just as well as the
Beetle version.
We also happened to have an infrared receiver assembly that includes
the actual receiver module plus an
onboard LED. This
This IR receiver module is a
great way to get up and running
quickly. It also has an onboard LED to
indicate when it is receiving a signal. You can
then pair this with the Arduino Leonardo shown
right, an Arduino Micro or similar device.
Australia’s electronics magazine
August 2018 67
spreadsheet or other document for use
in our next step. Using a spreadsheet
makes it easy to assign a name to each
code for later reference.
Keep in mind that the buttons you
use on the remote shouldn’t be used
for anything else, even if the TV is in
a different mode.
Otherwise, the TV and computer
might both respond to the same button press, with unexpected results.
Finding out which key codes
to generate
Finding out the codes that an IR
remote control uses is as simple as
opening a spreadsheet program and
loading our “IR_Code_Typer.ino”
sketch, which types the received
codes directly into a spreadsheet.
means that you can easily see if it’s receiving a signal or not.
If using such a module, check the
markings on it to see which pins are
which. They generally have an “S” to
identify the data pin and a “-” for the
ground pin, with the unmarked middle
pin being positive 5V supply.
Run a jumper lead from the “S” pin
to D11 on the Leonardo (we’ve used
blue), “-” to GND (grey) and the middle pin to 5V (violet).
Getting the codes
If you aren’t using the Jaycar remote control, you will need to figure
out which codes are produced by each
button that you intend to use. We’ve
written a brief sketch which looks for
signals from the infrared receiver and
then types that code out on the PC using the emulated keyboard.
You can dump the codes into a
◄
68
Silicon Chip
Having determined what button
you’ve pressed on the infrared remote,
the Arduino code then needs to know
which key or button press to generate
in turn. This will depend on the software that you’re running on the PC.
I use the VLC media player for
watching videos on my HTPC. It’s free
and for the most part, it just works. If
you use a different player, it will probably have a different set of keyboard
shortcuts although most seem to use
the space bar to play/pause. If in doubt,
open your player of choice and mash
away at the keyboard until you find
out what key does what!
The commands that I wanted to use
for VLC are: play/pause, toggle fullscreen mode, skip backwards and forwards. There’s a great guide to all the
shortcut keys at https://wiki.videolan.
org/Hotkeys_table/
The keys I needed are in the first
dozen listed, so this information was
easy to establish.
The Jaycar remote control I used
Wiring diagram for the
Arduino Leonardo version. We
used an IR receiver module for
the Leonardo, which can then
be hooked up via flying leads
or similar.
only has a single play/pause toggle
button, so I had to settle for using the
space key to toggle between play and
pause. Toggling fullscreen involves
simply pressing the “F” letter key.
For skipping forwards and backwards, we have the choice between
very short, short, medium and long
jumps.
According to the application's settings, a short jump is 10 seconds,
which sounds like a good amount and
is accessed using the Alt-Left Arrow
or Alt-Right Arrow key combinations.
Sending keystrokes to a PC
The Arduino keyboard library is
fairly easy to use. For example, to
send the “F” keypress for toggling fullscreen mode, we can simply use this
line of code:
Keyboard.write(‘f’);
It’s almost as though we are printing a character to the serial monitor.
But for key combinations like Alt-Left
Arrow, it’s not quite so easy.
There are two catches here. One is
that we are sending a non-printing key
(ie, the arrow key) and the second is
that we’re sending a key modifier (Alt).
This web page gives an overview
of all the special keys: www.arduino.
cc/en/Reference/KeyboardModifiers
This tells us that to send a Left Arrow keypress, we can use the following code:
Keyboard.write(
KEY_LEFT_ARROW);
◄
Wiring diagram for the smaller
Arduino Beetle remote receiver. The
pins of the IR receiver can just be
inserted directly into the Beetle and
soldered. The DATA pin on the IR
receiver can go to any free digital
pin on the Arduino, but you'll need
to change the software to match
which pin you're using if not D11.
Australia’s electronics magazine
siliconchip.com.au
To send Alt-Left Arrow, we need
to send the computer the correct key
presses and releases in the correct order, with a slight delay, as that is how
the computer is expecting to receive
them (as though a real human was
pressing the keys):
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(
KEY_LEFT_ARROW);
delay(100);
Keyboard.releaseAll();
This is a bit involved but it gives
us a lot of flexibility. For example,
you could use the following code
sequence to run any Windows program (in this case Notepad) using the
WIN+R shortcut:
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(‘r’);
delay(100);
Keyboard.releaseAll();
delay(200);
Keyboard.print(“notepad”);
Keyboard.write(KEY_RETURN);
If you’re going to use a sequence like
this, it’s a good idea to test it individually before mapping it to an infrared
remote button and remember that the
computer may respond differently if a
different program has the focus (ie, is
in the foreground) when the sequence
is activated.
For sequences which start with a
press of the “Windows Key”, like the
one immediately above, this should
not be a problem as they are captured
by the operating system, regardless of
which program is in the foreground.
But it it’s also worth testing what happens if a certain keystroke occurs under a different program.
What about the mouse?
As we mentioned, there’s also the
possibility of emulating mouse movements and button presses. If you have
a spare group of five or even nine buttons on the remote control, it’s possible
to use them to move the mouse cursor
around and click.
The library provides two different
functions to control the mouse. The
simplest is:
Mouse.move(x,y);
This simply moves the mouse pointer in the x and y directions by the number of pixels specified. If x is negative,
the pointer moves left and if x is positive, the pointer moves right. Up is
siliconchip.com.au
By changing the value highlighted, we can change which infrared code/button
press the Leonardo responds to. For this line of the sketch, if the current code
matches, a signal is sent to the computer to move the mouse pointer ten pixels to
the right.
To manually install the libraries (rather than using the library manager), unzip
or copy them into the Arduino libraries folder. This can be found by going to the
File → Preferences menu in the Arduino IDE and looking for the "Sketchbook
location", as shown above. The libraries are stored in a subfolder at this location.
negative on the y-axis. To simulate a
left-click, you can use:
Mouse.click();
If you want to click a different button, do it like this:
Mouse.click(MOUSE_RIGHT);
Note that all mouse movements are
relative. You can’t send a command to
move the pointer to a particular position on the screen. If you have to do
this, you might be able to come up with
a scheme where you move the mouse
into one corner of the screen and then
move it relative to that point, but we
won’t go into details as we haven’t
tried it.
In practice, the best way to implement mouse cursor control with an infrared remote is to have a button each
for up, down, left, right and click. Extra buttons, if available, can be used
for diagonal movements.
This makes steering the pointer feel
a bit like playing an old video game
Australia’s electronics magazine
with a joystick but it is fine for some
basic screen navigation, eg, to select
a video to play.
Getting the infrared codes
If you are using the suggested Jaycar
remote control and already have the
Arduino IDE installed, jump ahead to
“Uploading the main sketch”.
If you have an existing remote control you would like to make work, you
will first need to upload the “IR_Code_
Typer” sketch to find out what codes
correspond to what keys.
To compile and upload the software
that runs on the board, we need to have
the Arduino IDE (Integrated Development Environment) installed.
This can be downloaded from www.
arduino.cc/en/main/software for Windows, macOS and Linux.
Download and install a version to
suit your operating system and start
the Arduino IDE. You will need at
least version 1.6.4 to use the Library
Manager in the next step.
August 2018 69
Open the Library Manager by going to the Sketch → Include Library
→ Manage Libraries menu and search
for “irremote”. When you find it, click
on the install option that is presented.
If the Library Manager is not working or not available, you have the option of installing the IRremote.zip library supplied in our download package using the “Add .ZIP Library” option in that same menu.
Now plug the device into a USB port
on the computer and select the board’s
serial port in the Tools → Port menu.
Observe whether the board is seen as
a “Leonardo” or “Micro” and based on
this, select either “Arduino Leonardo”
or “Arduino/Genuino Micro” under
the Boards menu. If you choose the
wrong option it simply won’t work, so
if in doubt try one or the other.
Before uploading the code, open a
text editor such as Notepad or Leafpad, as the board becomes a keyboard
device immediately after the upload
completes.
Now open the IR_Code_Typer.ino
sketch file, which you will have extracted from our download package,
then click “Upload” (or press CTRL+U
on your keyboard).
Assuming the upload completes
successfully, switch to the text editor
that you opened earlier, then point
your infrared remote control at the unit
and press one of the buttons.
You should see a hexadecimal value
appear and pressing a different button
should give you a different code. You
may also get different codes if you hold
the button down.
A good way to record the codes is to
open a spreadsheet program and create a list with the button names that
you want to use in the first column.
Then move the cursor to the top of
the second column and press each
remote control button in turn, corresponding to the names in the first
column.
The code should appear in that cell
and the cursor will move to the next
cell below.
Uploading the main sketch
You might like to try uploading the
sketch to the board without making
any changes, just to check that everything works as it should.
The sketch code we have created
will work with the Jaycar XC3718 remote control pictured earlier, but can
be supplemented with your own codes.
70
Silicon Chip
By default, it uses the numbers 1-9
as a joystick type interface, with the
5 key being the same as a left mouse
click and the other numbers moving
the pointer in different directions by
10 pixels at a time.
For example, number 4 is to the left
of centre so it will move the mouse
to the left.
We’ve also set up the Play/Pause
button to emulate a spacebar press and
the Previous and Next buttons generate Alt-Left and Alt-Right keypresses
respectively. These last two correspond to forward and back on many
web browsers too.
If you are using a different remote
control or want to change what the buttons do, you will need to modify the
code. Specifically, you will be modifying the series of “if” statements which
check to see which IR code has been
received and then perform an appropriate action.
Before making any modifications,
save the Sketch file under a different
name so that you don’t overwrite the
original.
Using your spreadsheet as a reference, replace the hexadecimal value
in each “if” statement with one of
the button codes that you noted earlier. Then, inside the braces that follow that if statement, you will need
to change or add the code to send the
necessary keyboard or mouse events
to the computer.
For example, let’s say that in response to button code 0xFF1234 being received, you want to generate
a keypress equivalent to pressing
CTRL+ALT+t on the keyboard. Your
new “if” statement would look like:
if (code == 0xFF1234) {
Keyboard.press(
KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(‘t’);
delay(100);
Keyboard.releaseAll();
code=0;
}
The “code=0;” line towards the end
should be included if you do not want
the action to be repeated if the key is
held down. If you do want it to repeat,
leave that bit out.
Once you have done this with all
the buttons you want to use on the
remote and removed any extraneous
“if” statements which may be left
over from the original code, you can
Australia’s electronics magazine
proceed to upload this sketch to the
main board.
You can handle codes from multiple
remote controls in the same sketch.
Having uploaded the sketch, check
that it works as expected. If you find
any keys are not responding or not doing what you think they should, check
the code in the “if” statements.
Finalising the code
You might notice in the codes that
we check for 0xFFFFFFFF. This is a
code emitted by remote controls that
use the NEC encoding which indicates that the current key is being held
down and the effect of that key should
be repeated.
We implement this by saving the
code of whatever key was last pressed
and then substituting that code if a repeat code is seen.
What next?
It’s a very useful device as described,
but these Arduino boards have a number of spare pins and you could use
these for other tasks that can also be
controlled by the remote control.
For example, you could wire up
some LEDs and arrange for remote
buttons to switch them on and off for
some instant mood lighting.
You could even consider integrating some code to switch remote power
points on and off. You could do this
using some Arduino code provided
by Jaycar which works with the wireless power point switches. This can be
downloaded from www.jaycar.com.
au/iot-wireless-switch
SC
Parts List
1 infrared remote control
(eg, Jaycar XC3718)
1 infrared receiver module
(eg, Jaycar ZD1952,
Altronics Z1611A)
Beetle-based version
1 DFRobot Beetle or equivalent
(ProMicro etc)
Leonardo-based version
Arduino Leonardo board or
equivalent (eg, Jaycar
XC4430, Altronics Z6214)
1 set of plug-socket breadboard
leads (eg, Jaycar WC6028,
Altronics P1017)
siliconchip.com.au
|