cheap wireless RF communication; arduino to arduino = switch to LED
January 24, 2010 | Filed Under Blog, Installation Art
A week ago, I got the cheap wireless rf transmitter / receiver pair (from sparkfun) to work very well using VirtualWire.h library (pdf). I think I got it to transmitted from one arduino to another up to about 300 feet (according to what it looked like on Google Maps).
It is very simple to wire up. The switches attached to digital pin 9 and 10 have a 220ohm resister between the digital pin and ground – then one wire from the switch goes to the digital pin and the other goes to 5V. The receiver and transmitter are wired as follows. Someone said to connect the data pins together, but it works great as shown below (I think Tom Igoe explains that the data pins are different, the one I leave untouched can be used in different applications).
Here is the code I adapted for the transmitter (from the VirtualWire.h library examples). Make sure you add a greater than < and a less than > symbol around VirtualWire.h in the beginning of the code (the plugin I’m using to display code will not let me add them ???):
// transmitter.pde
#include VirtualWire.h // http://www.open.com.au/mikem/arduino/VirtualWire-1.4.zip
int switchPin1 = 9; // switch connected to digital pin 2
int switchValue1; // a variable to keep track of when switch is pressed
int switchPin2 = 10; // switch connected to digital pin 2
int switchValue2; // a variable to keep track of when switch is pressed
void setup()
{
pinMode(switchPin1, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin1, HIGH); // sets the default (unpressed) state of switchPin to HIGH
pinMode(switchPin2, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin2, HIGH); // sets the default (unpressed) state of switchPin to HIGH
vw_setup(2000); // Bits per sec
}
void loop()
{
////// check switch1 //////
const char *mymessage1 = "B";
// check to see if the switch1 is pressed
switchValue1 = digitalRead(switchPin1);
if(switchValue1 == LOW) {
mymessage1 = "Y";
}
else {
mymessage1 = "N";
}
digitalWrite(13, true); // turn on LED to show transmitting
vw_send((uint8_t *)mymessage1, strlen(mymessage1)); // transmit message
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); //turn off LED to show tranmitting is done
////// check switch2 //////
const char *mymessage2 = "C";
// check to see if the switch2 is pressed
switchValue2 = digitalRead(switchPin2);
if(switchValue2 == LOW) {
mymessage2 = "Z";
}
else {
mymessage2 = "O";
}
digitalWrite(13, true); // turn on LED to show transmitting
vw_send((uint8_t *)mymessage2, strlen(mymessage2)); // transmit message
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // turn off LED to show transmitting is done
// I still need to do some more research as to how often it can check the state
// of the switch . . . how often can it transmitt? baud rate limits?
delay(200);
}Here is the code I adapted for the reciever (from the VirtualWire.h library examples). Make sure you add a greater than < and a less than > symbol around VirtualWire.h in the beginning of the code (the plugin I’m using to display code will not let me add them ???):
// receiver.pde
#include VirtualWire.h // http://www.open.com.au/mikem/arduino/VirtualWire-1.4.zip
char inString[32];
int inCount;
void setup()
{
pinMode(9, OUTPUT); // sets the LED pin to be an output
pinMode(10, OUTPUT); // sets the LED pin to be an output
////// serial code is commented out because this application does //////
////// not require a computer - remove comment to send to computer //////
//Serial.begin(9600);
//Serial.println("setup");
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
inCount = 0;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // turn on LED to show received good message
// Message with a good checksum received, dump it.
//Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
//Serial.print(buf[i]);
//Serial.print(" ");
inString[inCount] = buf[i];
inCount++;
}
//Serial.println("");
////// if statements comparing incoming message //////
if (strcmp(inString, "N") == 0){
digitalWrite(10, true);
}
if (strcmp(inString, "Y") == 0){
digitalWrite(10, false);
}
if (strcmp(inString, "O") == 0){
digitalWrite(9, true);
}
if (strcmp(inString, "Z") == 0){
digitalWrite(9, false);
}
digitalWrite(13, false); // turn off LED to show done
}
}It is easy to get the arduinos to react differently depending on what you put in the end of the reciever code. For example, one receiver arduino could have the following code, which would light an LED on pin10 if switch1 were depressed:
////// if statements comparing incoming message //////
if (strcmp(inString, "N") == 0){
digitalWrite(10, true);
}
if (strcmp(inString, "Y") == 0){
digitalWrite(10, false);
}
digitalWrite(13, false); // turn off LED to show done
}
}The other receiver arduino could have the following code, which would light an LED on pin10 if switch2 were depressed:
////// if statements comparing incoming message //////
if (strcmp(inString, "O") == 0){
digitalWrite(10, true);
}
if (strcmp(inString, "Z") == 0){
digitalWrite(10, false);
}
digitalWrite(13, false); // turn off LED to show done
}
}The following pix show me pressing switch1 and switch2 (I’m using arduinos on breadboards and a Duemilanove):


Comments
4 Responses to “cheap wireless RF communication; arduino to arduino = switch to LED”
Leave a Reply

Couldn’t you just connect the transmitter/receiver to the serial pins on the arduino and use the default arduino library to handle data flow? Or use the SoftwareSerial library if you want to still have access to serial for PC connection?
I do think that might work (I have not tried) – but if I understand things correctly, the VirtualWire.h library created by Mike McCauley has error protection in it to check if the message received a good message or whether it is just noise. This is the first thing I have done with RF and the Arduino – I’m curious to know if what you suggest does work though!
Does your code work for a transmitter receiver pairing at a higher frequency maybe 2.4 Ghz?
Timothy – I am really not sure – I cannot see why not though. I don’t think that the code/library has anything to do with at what frequency the transmitters and receivers are working. I’d give it a try though. I really did very little changes to the example code provided with the VirtualWire.h library (pdf).