Aim a servo turret with the IR remote

Steer a servo left and right with the arrow keys of the infrared remote, centre it with OK, and let it sweep by itself at the press of a button.
  • Level:Beginner
  • Duration: 40 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Aim a servo turret with the IR remote – EEMENTOR STEM™ lesson

Infrared remotes control TVs, air conditioners and toys. In this lesson the remote from your kit controls a small turret: the arrow keys turn it left and right, OK points it straight ahead, and another key makes it sweep from side to side on its own like a security camera. First you find out which codes your remote sends, then you give each one a job.

What you'll learn

  • How to read button codes from an infrared remote
  • How to give each code a job with if and else if
  • How to ignore the repeat signal when a button is held
  • How to mix manual control and an automatic mode

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • IR receiver module and IR remote (UNO R3 Starter Kit)
  • Servo motor (UNO R3 Starter Kit)
  • A small box or a plastic cup as the turret, and tape
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Make it real: tape a paper cup upside down onto the servo horn and draw a face or a camera lens on it. Stand the servo on a box with its shaft pointing up.

Wiring

Breadboard wiring diagram: Aim a servo turret with the IR remote
Breadboard view. Check each connection against the wiring table below.
From To
IR receiver module S UNO pin 2
IR receiver module + and − 5V and GND
Servo orange / yellow wire UNO pin 9
Servo red wire 5V
Servo brown / black wire GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Find your remote's codes

  1. Install the IRremote library (by shirriff, z3t0 and ArminJo, version 4 or newer).
  2. Upload the sketch below and open the Serial Monitor at 9600 baud.
  3. Press left, right, OK and one more key (for example up) on the remote. Each press prints a line such as Code 0x8.
  4. Put the four codes into KEY_LEFT, KEY_RIGHT, KEY_OK and KEY_SWEEP, and upload again.

The sketch already contains the codes of the common 17-key remote with arrow keys. If your remote has no arrows (the 21-key type), use |<<, >>| and ▶|| instead.

The sketch

#include <IRremote.hpp>
#include <Servo.h>

const int IR_PIN = 2;
const int SERVO_PIN = 9;

// Codes of the 17-key remote; replace them with the codes your remote shows
const uint8_t KEY_LEFT = 0x08;
const uint8_t KEY_RIGHT = 0x5A;
const uint8_t KEY_OK = 0x1C;
const uint8_t KEY_SWEEP = 0x18;     // the up arrow

Servo turret;
int angle = 90;
bool sweeping = false;
int sweepStep = 2;
unsigned long lastSweep = 0;

void aim(int newAngle) {
  angle = constrain(newAngle, 0, 180);
  turret.write(angle);
}

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK);
  turret.attach(SERVO_PIN);
  aim(90);
  Serial.println("Press keys on the remote");
}

void loop() {
  if (IrReceiver.decode()) {
    uint8_t key = IrReceiver.decodedIRData.command;
    bool held = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
    IrReceiver.resume();                       // ready for the next code

    Serial.print("Code 0x");
    Serial.print(key, HEX);
    Serial.println(held ? " (held)" : "");

    if (key == KEY_LEFT) {
      sweeping = false;
      aim(angle + 10);                         // holding the key keeps turning
    } else if (key == KEY_RIGHT) {
      sweeping = false;
      aim(angle - 10);
    } else if (key == KEY_OK && !held) {
      sweeping = false;
      aim(90);
    } else if (key == KEY_SWEEP && !held) {
      sweeping = !sweeping;
    }
  }

  if (sweeping && millis() - lastSweep >= 30) {
    lastSweep = millis();
    if (angle + sweepStep > 170 || angle + sweepStep < 10) sweepStep = -sweepStep;
    aim(angle + sweepStep);
  }
}

How it works

The remote flashes an invisible infrared LED in a fast pattern. The receiver module turns the flashes into pulses, and the IRremote library decodes them into a command number for each key. Holding a key sends a short "repeat" signal instead of the full code; the library marks those with IRDATA_FLAGS_IS_REPEAT. The arrow keys use repeats on purpose so that holding them keeps the turret turning, while OK and the sweep key ignore them, so a long press counts once.

Sweep mode is a small automatic program: every 30 ms the turret moves 2 degrees, and at either end sweepStep changes sign so it turns back. Any arrow key switches sweep mode off and gives control back to you. aim() keeps the angle between 0 and 180 so that no key can push the servo past its stop.

Try this next

  • Use the number keys to jump to fixed angles: 1 = far left, 5 = middle, 9 = far right.
  • Add the active buzzer and beep at both ends of the sweep.
  • Make the sweep faster or slower with two more keys.

Troubleshooting

  • No codes appear: check S on pin 2, and point the remote straight at the receiver from about 1 m.
  • A different code every time: fluorescent lamps and sunlight confuse the receiver. Try in a darker spot.
  • Left and right are swapped: swap + 10 and - 10 in the sketch.
  • Compile errors about IRremote: install version 4 or newer and keep #include <IRremote.hpp>.
  • The board restarts when the servo moves: servos draw bursts of current. Use a good USB port or a powered hub, and check that the servo's red wire is on 5V.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit