Built-in project · Arduino Uno R3

Remote control light switch (IR on 12, servo on 11), on an Arduino Uno

An IR receiver on pin 12 and a servo on pin 11: press 1 on the remote and the servo arm swings to flip a wall switch on, press 2 and it flips it off. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $34. On Mokxi they're free.

Remote control light switch (IR on 12, servo on 11) · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Receiving NEC infrared codes with IRremote
  • Positioning a hobby servo with the Servo library
  • Ignoring repeated codes from a held button
  • Returning an actuator to a neutral position
  • Sharing 5V and ground between parts
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

An infrared receiver on pin 12 decodes the remote, and a hobby servo on pin 11 swings an arm. Taped beside a wall switch, the arm can flip it for you.

Button 1 turns the servo to 150 degrees to switch on, and button 2 turns it to 30 degrees to switch off. After half a second it returns to 90 degrees so you can still use the switch by hand. Other buttons just print their code.

Parts list

5 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Infrared receiver, VS1838B pin OUTArduino Uno R3 pin 12
Hobby servo pin PWMArduino Uno R3 pin 11
Infrared receiver, VS1838B pin VCCArduino Uno R3 pin 5V
Hobby servo pin VCCArduino Uno R3 pin 5V
Infrared receiver, VS1838B pin GNDArduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Infrared remote, 21 buttons pin IRInfrared receiver, VS1838B pin IR

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A remote control for a light switch: a servo arm presses it for you.
//
//   IR receiver OUT -> pin 12   VCC -> 5V   GND -> GND
//   servo signal    -> pin 11   servo + -> 5V   servo - -> GND
//
// Tape the servo beside an ordinary wall switch so its arm can push the
// switch up or down. Then the remote works the light without any change to
// the house wiring.
//
// Button 1 swings the arm to 150 degrees (on), button 2 to 30 degrees (off),
// and after each push the arm comes back to the middle so the switch can
// still be used by hand.

// The handset speaks NEC, so only that decoder is compiled in, as IRremote's
// own examples do to save flash. Without this line the library brings in
// every protocol's decoder, about 9 KB of the Uno's 32.
#define DECODE_NEC
#include <IRremote.hpp>
#include <Servo.h>

const int RECEIVER_PIN = 12;
const int SERVO_PIN = 11;
const int MIDDLE = 90;

Servo arm;

void push(int angle, const char *what) {
  arm.write(angle);
  Serial.println(what);
  delay(500);
  arm.write(MIDDLE);
}

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
  arm.attach(SERVO_PIN);
  arm.write(MIDDLE);
  Serial.println("Mokxi Uno: remote light switch. Press 1 for on, 2 for off.");
}

void loop() {
  if (!IrReceiver.decode()) return;
  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
  uint8_t command = IrReceiver.decodedIRData.command;
  IrReceiver.resume();
  if (repeat) return;
  if (command == 0x0C) {
    push(150, "1: switch on");
  } else if (command == 0x18) {
    push(30, "2: switch off");
  } else {
    Serial.print("button 0x");
    Serial.print(command, HEX);
    Serial.println(" does nothing here");
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

push sketch.ino

Moves the arm to angle, prints a message, waits 500 ms and then moves the arm back to MIDDLE, which is 90. That return step is what keeps the real switch usable by hand.

void push(int angle, const char *what) {
  arm.write(angle);
  Serial.println(what);
  delay(500);
  arm.write(MIDDLE);
}

setup sketch.ino

Opens the serial port, starts the receiver on RECEIVER_PIN, attaches the servo to SERVO_PIN and moves it to MIDDLE. It then prints which buttons do what.

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
  arm.attach(SERVO_PIN);
  arm.write(MIDDLE);
  Serial.println("Mokxi Uno: remote light switch. Press 1 for on, 2 for off.");
}

loop sketch.ino

Waits for a decoded code, grabs the command, resumes listening and ignores repeats. Command 0x0C calls push with 150 for on, and 0x18 with 30 for off. Any other button prints its hex code.

void loop() {
  if (!IrReceiver.decode()) return;
  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
  uint8_t command = IrReceiver.decodedIRData.command;
  IrReceiver.resume();
  if (repeat) return;
  if (command == 0x0C) {
    push(150, "1: switch on");
  } else if (command == 0x18) {
    push(30, "2: switch off");
  } else {
    Serial.print("button 0x");
    Serial.print(command, HEX);
    Serial.println(" does nothing here");
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The servo jitters or resets This project
    Make sure the servo shares ground with the Uno, and its signal wire goes to pin 11.
  • Buttons do nothing This project
    Press 1 or 2. Other buttons are only printed. Check the receiver OUT on pin 12.
  • No signal is seen, or the module gets hot, because the three legs are in the wrong order. Infrared receiver
    On the VS1838B, with the dome facing you, the legs are usually OUT, GND, VCC from left to right. Check your module's marking before powering it. Supply is 2.7 to 5.5 V.
  • A plain LED blinking at the receiver is never detected. Infrared receiver
    The receiver only responds to light modulated at about 38 kHz. Send with a library such as IRremote, which makes the carrier. OUT idles high and goes low during a burst, so decode that inverted signal.
  • Button codes come out wrong or do not match another sketch. Infrared remote
    Remotes differ. Print the raw codes for your buttons first, then compare against those values. A repeat while you hold a button is a special repeat code, not a new press.
  • Nothing is received at all. Infrared remote
    Check the receiver pin order of OUT, GND and VCC, which varies by module. Use the pin your IR library expects. Keep the receiver away from bright light and aim the remote at it.
  • The servo jitters, twitches or resets the board when it moves. Hobby servo
    Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply.
  • The servo hums at the end of its travel or will not reach full angle. Hobby servo
    Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.