Level 2Unit C · Session 19
19

Unit C · Telling a machine what you want

From across the room

What we are making: a machine you control with the remote from your kit, from the other side of the classroom.

The idea

The remote has an infrared LED in the end. It flashes light you cannot see, in a pattern, and every button has its own pattern. The receiver on your board sees the flashes and hands your program a number.

Here is the part worth understanding: nobody can tell you what your numbers will be. They depend on the remote. So today's job is to find them out and write them down — which is exactly what an engineer does with undocumented hardware.

New words
InfraredLight just beyond red, invisible to us, easy for a sensor to see.
CodeThe number a particular button sends. Different for every remote.
resume()Telling the receiver you are ready for the next press.
Arduino UnoUSB to the laptop5VGND11IR receiverthe little black domeOUTGNDVCCsignalPin order on these varies by batch. Read the tiny letters on the module.
The infrared receiver. The remote in the kit already speaks to it.
Finding out what your remote says
1#include <IRremote.h>
2 
3int receiverPin = 11;
4int ledPin = 13;
5 
6void setup() {
7 Serial.begin(9600);
8 IrReceiver.begin(receiverPin);
9 pinMode(ledPin, OUTPUT);
10}
11 
12void loop() {
13 if (IrReceiver.decode()) {
14 long code = IrReceiver.decodedIRData.decodedRawData;
15 Serial.println(code); // write this number down
16 
17 if (code == 3125149440) { // <-- put YOUR button's number here
18 digitalWrite(ledPin, HIGH);
19 }
20 if (code == 3108437760) { // <-- and another button's here
21 digitalWrite(ledPin, LOW);
22 }
23 
24 IrReceiver.resume(); // get ready for the next press
25 }
26}

Do it — the discovery part

  1. Install the IRremote library. Make sure it is version 4 or later.
  2. Wire the receiver. Read the tiny letters on the module itself — the pin order is not the same on every batch.
  3. Upload and open the Serial Monitor.
  4. Press every button on the remote, one at a time, and write the number down next to the button in your notebook. This table is the deliverable.

Then make it do something

  1. Put two of your numbers into the two if lines in the program.
  2. Upload. Those two buttons now turn the built-in LED on and off.
  3. Point the remote at the wall, away from the sensor. Does it still work? Try it.
💡 Tip
The numbers in the program are not yours. They came from one particular remote on one particular day. If you upload the program without changing them, nothing will happen, and nothing is broken. Put your own numbers in.
What you seeWhyWhat to do
Serial Monitor prints nothingThe pin order is wrong.Read the labels on the module itself. OUT is not always the first pin.
The same huge number every timeThat is the repeat code.It is sent while a button is held down. Tap the button instead.
Works close up, not across the roomSomething is shading the receiver.Move it clear. Strong sunlight also drowns the signal.
A different number on every pressAn older library version.You need IRremote version 4 or later. Tell your teacher.

What you learned

  • Remote controls are light, flashing too fast and too red for you to see.
  • When hardware is undocumented, you find out by experiment and write it down.
  • A number that works on your desk may be wrong on somebody else's.
Challenge optional — only if you finish early
  • Map five remote buttons to five servo positions.
  • Find out what happens if two remotes are used at once.
18. Typing a number in