RF Link between two Arduinos

This tutorial shows how one Arduino can send data wirelessly to another Arduino. This can be accomplished in a number of ways, but I am showing how do to it using these 315MHz RF Link modules. When I bought mine it cost $11.95, but the price has gone up to $13.95. There are probably much better choices at the $20 range - these RF Link modules can only broadcast from transmitter to receiver, as opposed to creating a network of inter-communicating microcontroller. But if you're fine with one-way communication across an open space, then the RF Link is fairly easy to use - you just pretend it's a physical serial connection. (if a "serial connection" doesn't sound easy to you, then have a look at this tutorial).

The RF Link comes in two parts, a small transmitter and a wide receiver. The drawing below shows how you'd wire each of these up to any microcontroller (each of which is represented by a brain). They are quite easy to wire up as you can see in the "walkthrough tutorial" provided by Sparkfun. I found that the antenna wasn't really optional beyond a distance of about 24 inches with the transmitter getting 5V. There's no real explanation of this, but I cut a wire 23 cm long to use an antenna and then I connected it to ground through a 1uF capacitor (my first guess was 0.1uF but all the data was garbled until I switched, so be flexible with this value!)

If you're using the Arduino platform, then here is some basic code for the following situation: I push a button and a PC across the room is made aware of this. In other words, a button is read by Arduino #1, which pushes information through the transmitter. Somewhere else in RF range, Arduino #2 is pulling in data from the receiver and sending some of this data up the USB cord to a computer.

The following is the transmit code for Arduino #1. Notice that I used the software serial library so that you can save the Serial port for something else later. Probably the most important twist in this code is that the trasmitter needs to be sending data CONSTANTLY or else the receiver will be left hanging and will start reading random junk floating in RF space -- by sending data all the time, just imagine that you're overpowering all the junk. So you'll probably need to decide on a byte of information that actually means "nothing" or "no data now" or "off". I chose 98, which is wonderfully arbitrary. Finally I'm assuming you've got a normally open switch wired to pin 12 (pulled down by a 10K resistor).

#include 

/* 
Read a pushbutton and send a value
to another microcontroller.  
*/

#define rxPin 2
#define txPin 3
#define buttonPin 12
#define ledPin 13

// set up a new serial connection for communicating with RF transmitter 
// (this also frees up Arduino's built in serial port for PC communication)
SoftwareSerial rfSerial =  SoftwareSerial(rxPin, txPin);

byte val = 0;
byte onState = 99;
byte offState = 98;

void setup() {
  pinMode(buttonPin, INPUT);    // declare pushbutton as input
  pinMode(ledPin, OUTPUT);    // declare led as output
  rfSerial.begin(2400); // our RF Link is a 2400 baud model (make sure you check this!)
  // start by blinking high and then low so we can tell the setup routine has been completed
  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
}

void loop(){
  val = digitalRead(buttonPin);
  if ( val == HIGH ) {         // check if the input is HIGH (button pressed)
    rfSerial.print(onState, BYTE); // OK send it to the RF transmitter
  } else {
    rfSerial.print(offState, BYTE);  // send a 0 through the transmitter
  }
}

OK and here is the receiver code which goes on Arduino #2. Once again I'm using the SoftwareSerial library, but this time it's necessary because I need to use the usual Serial port to send data up the USB to the computer. The loop is filled with a little extra code to avoid sending the same byte over and over and over. A byte is only sent if it is new (changes from "off" byte to "on" byte for instance and no data would be sent again until it goes back "off").

#include 

/* 
Read from a RF Link receiver module
and get the data into a computer.  
*/

#define rxPin 2
#define txPin 3
#define ledPin 13

// set up a new serial connection for communicating with RF receiver 
// (this also frees up Arduino's built in serial port for PC communication)
SoftwareSerial rfSerial =  SoftwareSerial(rxPin, txPin);

char prevChar = 0;

void setup() {
  // set up the input and output pins
  pinMode(rxPin, INPUT); // set up pins for serial comm. with RF receiver
  pinMode(ledPin, OUTPUT);
  // initialize serial comm
  rfSerial.begin(2400); // begin serial connection with RF Link unit
  Serial.begin(2400); // begin serial communication over USB to the computer
  // blink LED on and then off just to let us know the setup routine is complete
  digitalWrite(ledPin,HIGH); // turn on LED
  delay(1000);
  digitalWrite(ledPin,LOW); // turn off LED
}

void loop(){
  char someChar = '0'; 
  someChar = rfSerial.read(); // read whatever the RF Link has to offer
  // print out the character: 
  if (someChar!=prevChar) { // only print out new data (don't print 0 a billion times in a row)
    Serial.print(someChar, BYTE); // send data to computer
    prevChar=someChar; // store what we just read
  }
}

Categories: | | | |