ArduinoArduino UnoExact chip

433 MHz key fob and a relay

RCSwitch reads a 433 MHz key fob: key A switches the relay on, key B off.

433 MHz key fob and a relay in Argynix Lab

The code

sketch.inoC++
// A 433 MHz receiver on pin 2 (interrupt 0) and a four-key fob: RCSwitch decodes its
// EV1527 codes. Key A switches the relay on pin 7 on, key B off. Every code is printed:
// set the fob's address in the inspector and the codes change with it.
#include <RCSwitch.h>

RCSwitch rf;

void setup() {
  Serial.begin(115200);
  pinMode(7, OUTPUT);
  rf.enableReceive(0); // pin 2
  Serial.println("Press A or B on the key fob");
}

void loop() {
  if (!rf.available()) return;
  unsigned long code = rf.getReceivedValue();
  Serial.print("Code ");
  Serial.print(code);
  Serial.print(" (");
  Serial.print(rf.getReceivedBitlength());
  Serial.println(" bits)");
  switch (code & 0xF) {     // the fob's key is in the last four bits
    case 8: digitalWrite(7, HIGH); Serial.println("Relay on"); break;  // A
    case 4: digitalWrite(7, LOW); Serial.println("Relay off"); break;  // B
  }
  rf.resetAvailable();
}
libraries.txtC++
rc-switch