433Mhz Transmitter/Receiver not working ?

Hey everyone! I am currently working on a project to create a doorbell/buzzer thing. I have decided to use a set of 433Mhz transmitter and receiver modules which are very cheap on Ebay. I am having trouble getting them to work properly though, when I use similar code to this guide here : http://arduinobasics.blogspot.co.uk/2014/06/433-mhz-rf-module-with-arduino-tutorial.html

The transmitter just transmits a high or low signal depending on the state of a switch. The receiver is supposed to read that, and then turn off or on the build in led accordingly. In practice however, the receiver just receives a garbled mess with only a slight change when the button is pressed on the tx. The modules are about 10cm away from each other, and have a 17cm antenna on them. I have attached a screenshot of the serial monitor for the tx and rx. Any ideas why the receiver is only getting random signals? Thanks!

//TX code
#define rfTransmitPin 13    //RF Transmitter coonected to pin 13
#define buttonPin 10  //button connected to GND and pin 10

void setup(){
 pinMode(rfTransmitPin, OUTPUT);      
 pinMode(buttonPin, INPUT_PULLUP);  
 Serial.begin(9600);
}

void loop(){

  int buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  
  if (buttonState == LOW){  // if button pressed, transmit a high signal
    digitalWrite(rfTransmitPin, HIGH);       
  }
  else {  // if not pressed transmit a low signal
    digitalWrite(rfTransmitPin, LOW);     
  } 
}
//RX code
#define rfReceivePin A5  //RF Receiver pin = Analog pin A5
#define ledPin 13        //Onboard LED = digital pin 13

unsigned int data = 0;   // variable used to store received data
const unsigned int upperThreshold = 70;  //upper threshold value
const unsigned int lowerThreshold = 50;  //lower threshold value

void setup(){

 pinMode(rfReceivePin, INPUT);
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
}

void loop(){
 
 data = analogRead(rfReceivePin);   //listen for data on Analog pin A5
 Serial.println(data);  //print the data recewived to serial
  
  if(data > upperThreshold){
   digitalWrite(ledPin, LOW);   //If a LOW signal is received, turn LED OFF 
 }
 
 if(data < lowerThreshold){
   digitalWrite(ledPin, HIGH);   //If a HIGH signal is received, turn LED ON   
 }
}

That approach is very unlikely to work.

The receivers output noise when no signal is received, and adjust their sensitivity to match and give a stable output, a short while after a signal is received.

You need to use a library like VirtualWire or RadioHead to reliably transmit data using those modules.

Don't worry about VirtualWire not being supported, it is easy to use and works well. Follow the simple example carefully.

Ahh ok, thanks! I have downloaded the Radiohead library, and tried to modify the example to my needs. Now, when the button is pressed the serial monitor prints A0, and when not pressed it prints 0. It’s a start, however i’m not really sure how to get just a HIGH or LOW from the receiver which is what I want to turn on or off a buzzer.

//Transmitter code

#include <RH_ASK.h>
RH_ASK driver;

void setup(){
  
  Serial.begin(9600);
  pinMode(5, INPUT_PULLUP);
  if (!driver.init())
         Serial.println("init failed");
}

void loop(){
    
    int buttonState = digitalRead(5);
    Serial.println(buttonState);
    driver.send((const uint8_t * )buttonState, 1);
    driver.waitPacketSent();
    delay(100);    
}
//Receiver code

#include <RH_ASK.h>
RH_ASK driver;

void setup(){
  
  Serial.begin(9600);
  if (!driver.init())
         Serial.println("init failed");   
}

void loop(){
  
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)){ //non blocking
	int i;
	driver.printBuffer("Got:", buf, buflen); // Message with a good checksum received, dump it.
  }
}

Program the transmitter Arduino to send a "1" or "0" message and program the receiver Arduino to set an output accordingly.

Note that the following may not work as buttonState is two bytes and you are sending only one byte. Declare buttonState as byte or char.

    int buttonState = digitalRead(5);
    Serial.println(buttonState);
    driver.send((const uint8_t * )buttonState, 1);

Ok, so I have declared buttonState as a byte. I also added a digital write to the built in led to make sure that part is working, which it is. Then I just transmit the buttonState, saying the length is two bytes.

void loop(){
    
    byte buttonState = digitalRead(5);
    digitalWrite(13, buttonState);
    driver.send((const uint8_t * )buttonState, 2);
    driver.waitPacketSent();
    delay(100);    
}

The receiver print out in the serial monitor :
Got:
10 0
when the button is pressed and,
Got:
0 FF
when the button is released. That seems good to me, I just need to figure out how to seperate the actual high / low signal. I have tried just using the "buf"variable, however it doesnt seems to like that.

void loop(){
  
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)){ //non blocking
	int i;
	driver.printBuffer("Got:", buf, buflen); // Message with a good checksum received, dump it.
  }

  if (buf == 10){
    digitalWrite(13, HIGH);
  }
  else{
    digitalWrite(13, LOW);
  }
}

Then I just transmit the buttonState, saying the length is two bytes.

Why? The length of a byte is NOT two bytes.

  if (driver.recv(buf, &buflen)){ //non blocking
    int i;
    driver.printBuffer("Got:", buf, buflen); // Message with a good checksum received, dump it.
  }

What is the purpose of declaring the variable i there?

  if (buf == 10){

The address where buf is stored is almost certainly not 10.