Arduino library · Communication

rc-switch: 433 MHz remote sockets and OOK modules

Yes. You can use the rc-switch library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (RF transmitter, FS1000A). The example below, "Send a 433 MHz code and receive it", runs on the Arduino Uno R3.

Built in on Mokxi: #include <RCSwitch.h> and it builds, with nothing to install. Free, and no account needed.

What rc-switch does

rc-switch is the library behind most "control a 433 MHz outlet from an Arduino" projects. Cheap remote sockets and doorbells use simple on-off keyed radio: the remote holds the carrier on and off in a pattern of short and long pulses, and the chips inside (PT2262, EV1527 and their copies) decode it. A FS1000A transmitter or similar module on one Arduino pin can send the same pattern, and an XY-MK-5V or similar receiver can read codes from an existing remote.

Codes can be sent in several forms. send() takes a decimal number and a bit length, or a string of 0s and 1s; sendTriState() takes the 0, 1 and F form that PT2262 sockets use; switchOn() and switchOff() build the code for sockets with DIP switches from the group and device settings. Twelve protocols cover the different pulse timings, with protocol 1 matching most sockets.

Mokxi has no 433 MHz transmitter or receiver part yet, so this library builds and runs but nothing receives the codes. send() still drives the data pin with each protocol’s real pulse timings, so a logic analyzer on that pin (10 in the example) shows the code going out, and the serial monitor notes once that no transmitter is simulated. enableReceive() says there is no receiver, and available() stays false.

Library Manager namerc-switch
Authorsui77
Include#include <RCSwitch.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesRF transmitter, FS1000A, RF receiver, XY-MK-5V

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <RCSwitch.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "rc-switch" by sui77 from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add rc-switch to lib_deps in PlatformIO. Mokxi follows the calls of release 2.6.4, so a sketch written here builds there too.

What to know in the simulator: The FS1000A and XY-MK-5V parts carry the codes: every receiver on the canvas hears every transmitter. A code is taken once it has arrived twice, as upstream, so the receiver’s idle noise is thrown away. No remote socket is simulated.

The calls you will use

In the order a sketch meets them.

RCSwitch mySwitch = RCSwitch()

The object that sends and receives codes.

mySwitch.enableTransmit(10)

The pin wired to the transmitter’s DATA input.

mySwitch.setProtocol(1)

Chooses one of the 12 pulse timings. Protocol 1 suits most sockets; the receive example tells you which one a remote uses.

mySwitch.setRepeatTransmit(3)

How many times each code is sent. Sockets often need several repeats to react; the default is 10.

mySwitch.send(5393, 24)

Sends a code given as a decimal number with its bit length, the form the receive example prints.

mySwitch.switchOn("11111", "00010")

Turns on a socket with DIP switches: the five group switches, then the five device switches.

mySwitch.sendTriState("00000FFF0F0F")

Sends a 12-character tri-state code, the 0, 1 and F notation of PT2262 sockets.

mySwitch.enableReceive(0)

Listens on interrupt 0 (pin 2 on an Uno). Then available(), getReceivedValue() and getReceivedProtocol() read each code.

if (mySwitch.available()) {
  Serial.println(mySwitch.getReceivedValue());
  mySwitch.resetAvailable();
}

Example: Send a 433 MHz code and receive it

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
// rc-switch's SendDemo and ReceiveDemo on one board: the FS1000A sends,
// the XY-MK-5V hears it, and the receive interrupt runs while send() is busy.
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  // The transmitter's DATA pin.
  mySwitch.enableTransmit(10);
  // The receiver's DATA on interrupt 0, which is pin 2 on an Uno.
  mySwitch.enableReceive(0);
  // Optional: protocol (1 suits most sockets), pulse length and repeats.
  // mySwitch.setProtocol(2);
  // mySwitch.setPulseLength(320);
}

void loop() {
  // A socket's code as a decimal number, sent 10 times.
  mySwitch.send(5393, 24);
  if (mySwitch.available()) {
    Serial.print("Received ");
    Serial.print(mySwitch.getReceivedValue());
    Serial.print(" / ");
    Serial.print(mySwitch.getReceivedBitlength());
    Serial.print("bit Protocol: ");
    Serial.println(mySwitch.getReceivedProtocol());
    mySwitch.resetAvailable();
  }
  // DIP switches 11111, socket B, as tri-state.
  mySwitch.switchOn("11111", "00010");
  delay(1000);
}

Wiring

  • Arduino Uno R3 10 to RF transmitter, FS1000A DATA
  • Arduino Uno R3 5V to RF transmitter, FS1000A VCC
  • Arduino Uno R3 GND to RF transmitter, FS1000A GND
  • Arduino Uno R3 2 to RF receiver, XY-MK-5V DATA
  • Arduino Uno R3 5V to RF receiver, XY-MK-5V VCC
  • Arduino Uno R3 GND to RF receiver, XY-MK-5V GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

The socket does not react to the code

The protocol or pulse length differs from the original remote. Read the remote with a receiver and the receive example, then copy its protocol with setProtocol() and its pulse length with setPulseLength().

The range is only a meter or two

The transmitter has no antenna. Solder a 17 cm straight wire to the ANT pad for 433 MHz, and run the transmitter from 9 to 12 V if the module allows it.

available() never turns true

enableReceive() takes an interrupt number, not a pin: 0 is pin 2 on an Uno. On an ESP32 pass the pin itself. In Mokxi it stays false because no receiver part exists yet.

Questions and answers

Can rc-switch copy a garage door remote?

Usually not. Most garage and car remotes use rolling codes that change with every press. rc-switch works with fixed-code devices such as cheap sockets and doorbells.

Projects that use rc-switch

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try rc-switch without installing anything

Open the example, change the sketch, and keep your version in a free account.