I was looking for a way to handle wireless communications between two Arduino boards. Then I found inexpensive RF transmitter and receiver modules at Electronics-DIY.com.
Here are some things to consider while using RF solution:
- Communications is only one way. If you wanted two way communications you’d need to buy two receivers and two transmitters.
- The variable gain on the receiver causes it to pick up lots of background noise. I had to do some processing with the Arduino to filter out this noise. More details about this below in the code section.
- Bandwidth maxes out at 2400 bps, but there is a version with 4800 bps. A large portion of this bandwidth is used for network protocol I wrote that handles error detection.
- Range is limited to a max of 500 feet.
The advantages are that it is cheap and it is pretty easy to use.
Code
Since the receiver is constantly picking up random noise I add a few extra bytes to every data packet. I add two bytes to signify the start of a data packet. Then I send the a byte address. This address allows multiple devices to work in the same area without interfering with each other. Next is the data (in my example code it’s an unsigned int (2 bytes). Lastly I send a checksum which is a simple xor of all the data bytes to make sure the data got received without being corrupted.
I broke the Arduino code into two files. If you’ve never used two files before with Arduino all you need to to is keep both files in the same directory and the Arduino IDE merges them for you.
Here is the full code for the main application
// Used Arduino 0017
// This is a simple test app for cheap RF transmitter and receiver hardware.
// 433MHz RF Transmitter: http://electronics-diy.com/product_details.php?pid=250
// 433MHz RF Receiver: http://electronics-diy.com/product_details.php?pid=251
// This says whether you are building the transmistor or reciever.
// Only one of these should be defined.
//#define TRANSMITTER
#define RECEIVER
// Arduino digital pins
#define BUTTON_PIN 2
#define LED_PIN 13
// Button hardware is setup so the button goes LOW when pressed
#define BUTTON_PRESSED LOW
#define BUTTON_NOT_PRESSED HIGH
void setup()
{
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(1200); // Hardware supports up to 2400, but 1200 gives longer range
}
#ifdef TRANSMITTER
void loop()
{
static int prev_button = BUTTON_NOT_PRESSED; // Previous button press value
int cur_button; // Current button press value
cur_button = digitalRead(BUTTON_PIN);
if ((prev_button == BUTTON_NOT_PRESSED) && (cur_button == BUTTON_PRESSED))
{
writeUInt(271); // Put any number you want to send here (71 is just a test)
}
delay(50); // Debounce button
prev_button = cur_button;
}
#endif //TRANSMITTER
#ifdef RECEIVER
void loop()
{
boolean light_led = false;
if (readUInt(true) == 271) // Check to see if we got the 71 test number
{
light_led = true;
}
if (light_led)
{
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
}
}
#endif //RECEIVER
Full code that does the network error catching
// Used Arduino 0017
// This does does some error checking to try to make sure the receiver on this one way RF serial link doesn't repond to garbage
#define NETWORK_SIG_SIZE 3
#define VAL_SIZE 2
#define CHECKSUM_SIZE 1
#define PACKET_SIZE (NETWORK_SIG_SIZE + VAL_SIZE + CHECKSUM_SIZE)
// The network address byte and can be change if you want to run different devices in proximity to each other without interfearance
#define NET_ADDR 5
const byte g_network_sig[NETWORK_SIG_SIZE] = {0x8F, 0xAA, NET_ADDR}; // Few bytes used to initiate a transfer
// Sends an unsigned int over the RF network
void writeUInt(unsigned int val)
{
byte checksum = (val/256) ^ (val&0xFF);
Serial.write(0xF0); // This gets reciever in sync with transmitter
Serial.write(g_network_sig, NETWORK_SIG_SIZE);
Serial.write((byte*)&val, VAL_SIZE);
Serial.write(checksum); //CHECKSUM_SIZE
}
// Receives an unsigned int over the RF network
unsigned int readUInt(bool wait)
{
int pos = 0; // Position in the network signature
unsigned int val; // Value of the unsigned int
byte c = 0; // Current byte
if((Serial.available() < PACKET_SIZE) && (wait == false))
{
return 0xFFFF;
}
while(pos < NETWORK_SIG_SIZE)
{
while(Serial.available() == 0); // Wait until something is avalible
c = Serial.read();
if (c == g_network_sig[pos])
{
if (pos == NETWORK_SIG_SIZE-1)
{
byte checksum;
while(Serial.available() < VAL_SIZE + CHECKSUM_SIZE); // Wait until something is avalible
val = Serial.read();
val += ((unsigned int)Serial.read())*256;
checksum = Serial.read();
if (checksum != ((val/256) ^ (val&0xFF)))
{
// Checksum failed
pos = -1;
}
}
++pos;
}
else if (c == g_network_sig[0])
{
pos = 1;
}
else
{
pos = 0;
if (!wait)
{
return 0xFFFF;
}
}
}
return val;
}
Increasing the Range
I did all of my initial testing without any of these improvement and everything worked fine with these devices inside the same room.
- Add an antenna. All you need is a 23 cm piece of wire. I did this and it made it so I could reliably transmit data from one corner of my house to the other (3 floor town house).
- Increase the voltage for the transmitter. The transmitter can use 2-12 volts. With 5 volts I got pleanty of range for my use case, but increase this if you need more range.
- Reduce the baud rate. My test app runs at 1200 bps out of the max 2400 bps. You could drop this even further to something like 300 bps and that should help reduce transfer errors and hopefully increase range.