Rf Communication using arduino.



In this post we will see how to communicate between two arduinos using the rc switch library .

It uses the rf module which operates on 433 Mhz and is available easily on spark fun.

Rc Switch Library download

Transmitter

Receiver

fig 1 connection of rf link to arduino


Here's the code:

Transmitter Code

/*  Example for different sending methods   
 http://code.google.com/p/rc-switch/   
 Need help? http://forum.ardumote.com*/

#include <RCSwitch.h>
const int buttonPin = 3;
int buttonState = 0;
const int buttonPin2 = 2;
int buttonState2 = 0;
RCSwitch mySwitch = RCSwitch();
void setup() {
 pinMode(buttonPin, INPUT);
 pinMode(buttonPin2, INPUT);
  Serial.begin(9600);

  // Transmitter is connected to Arduino Pin #10  mySwitch.enableTransmit(10);

}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (buttonState == HIGH) {

  mySwitch.send(9, 24);
  delay(100);

}
else {
  if (buttonState2 == HIGH) {

  mySwitch.send(5, 24);
  delay(100);

else {


  }
    // turn LED off:
  }
  delay(200);
}

Receiver Code

/*
  Simple example for receiving
  
  http://code.google.com/p/rc-switch/
  
  Need help? http://forum.ardumote.com
*/

#include <RCSwitch.h>


int led = 13;
RCSwitch mySwitch = RCSwitch();
#define pressed1 9   //defines incoming data set by user which is transmitted
#define pressed2 5


void setup() {
  Serial.begin(9600);
 mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2 pinMode(led,OUTPUT);
}
void loop() {


  digitalWrite(led,LOW);
  if (mySwitch.available()) {
    
    int value = mySwitch.getReceivedValue();
    
    if (value == 0) {
      Serial.print("Unknown encoding");
    } else {
      //  used for checking received data      
         /*Serial.print("Received ");       
          Serial.print( mySwitch.getReceivedValue() );      
          Serial.print(" / ");     
          Serial.print( mySwitch.getReceivedBitlength() );      
          Serial.print("bit ");      
          Serial.print("Protocol: ");     
          Serial.println( mySwitch.getReceivedProtocol() );*/    }
if (mySwitch.getReceivedValue())

{
    process();



}

mySwitch.resetAvailable();


Serial.println("  ");  
}
  digitalWrite(led,LOW);
  
  delay(100);
  
}
void process()
{
  unsigned long res = mySwitch.getReceivedValue();
  
  switch (res){
    case pressed1:
     Serial.println("pressed 1");
     break;
     case pressed2:
     Serial.println("pressed 2");
     break;
     
  }
}





Credits : Rc Switch library
          Sparkfun
          arduino

Popular Posts

Like us on Facebook

Flickr Images

Subscribe