How to send button state through RF 433MHz

Hi, I've been trying to get my RF 433MHz (xy-mk-5v) system running and can't get it to work. I am trying to control lights and other things using relays, like commercially bought RF controlled power bricks. All I need is some code to get me on track. Essentially, I want a button to be pressed at the the transmitter end and then at the receiver end make a pin high for a certain period of time. Any help would be appreciated, Thanks.

I did the same for an RF remote control.
Use Keypad.h library to read the button matrix,
Use Virtualwire.h library to send the button press, and receive it on the other end.

VirtualWire.pdf (138 KB)

Thanks, cool project! Do you have some example sketch for it or some similar code to what I want to do. I did see the library but I can't fully understand it. I am relatively new to the arduino environment. Thanks

Here’s my remote control code. Interrupt is created when a button is pressed, the keypad is read, the character sent, and it goes back to sleep.
The Keypad.h library leaves the column pins high, I had to write them low before going to sleep so that when pressed the diode cathode would go low; then write them back high again on waking for the library to read.
This was one of my earliest Arduino coding attempts, so it may look kind of kludgy.

transmitter_shortened.pde (11.4 KB)

Thanks for that. I'll have a look at it and see what I can do.

Hi, I just had a look at it and I am struggling with understanding it all. Would you be able to simplify the code just for one button, instead of multiple buttons that are in a matrix or point me to where you got your ideas from. Thanks

I just got it to work. I found some code and modified it until I came across the solution. It works GREAT!

Transmitter:

#include <VirtualWire.h>
 
int RF_TX_PIN = 2;
 
void setup()
{
  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.
  pinMode(3, INPUT_PULLUP);
  pinMode(13, OUTPUT); 
}
 
void loop()
{
  int sensorVal = digitalRead(3);
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
    const char *msg = "0";
    vw_send((uint8_t *)msg, strlen(msg)); 
    delay(20);
  } 
  else {
    digitalWrite(13, HIGH);
    const char *msg = "1";
    vw_send((uint8_t *)msg, strlen(msg)); 
    delay(20);
  }
}

Receiver:

#include <VirtualWire.h>
 
int RF_RX_PIN = 2;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_rx_pin(RF_RX_PIN);  // Setup receive pin.
  vw_setup(2000); // Transmission speed in bits per second.
  vw_rx_start(); // Start the PLL receiver.
}
 
void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if(vw_get_message(buf, &buflen)) // non-blocking I/O
  {
   if(buf[0]=='1'){
     digitalWrite(13,1);
   }  
   if(buf[0]=='0'){
     digitalWrite(13,0);
   }
}
}

Not that hard, yes?