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 Runs in your browser. Free, and no account needed.

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.

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.

#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");
  }
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 12; Infrared receiver, VS1838B pin OUT
  • Arduino Uno R3 pin 11; Hobby servo pin PWM
  • Arduino Uno R3 pin 5V; Infrared receiver, VS1838B pin VCC; Hobby servo pin VCC
  • Ground: Arduino Uno R3 pin GND; Infrared receiver, VS1838B pin GND; Hobby servo pin GND
  • Infrared receiver, VS1838B pin IR; Infrared remote, 21 buttons pin IR

Change it and keep it

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