VirtualWire tutorial?

Any chance of one? VirtualWire sounds like what I need for my project, but the mystery to me is how to use it.
I’ve tried to de-cypher the code, but I can’t see how to add inputs, or how to affect the output content. Or maybe I’m on the wrong track entirely.
I searched the forum & got only 2 hits which may be handy later.
Any tips welcome.

I think the examples are the only resource. :-/

I played with it for the first time last night and it is really easy to use.

The library includes transmitter and receiver examples. I just compiled the two pieces and loaded each on a separate Arduino and it just worked. I’m using the 434Mhz transmitter and receiver that I got from SparkFun.

The only issue I ran into was that the examples would not compile without adding the statement

#include <WProgram.h>

above the #include statement for VirtualWire.h.

It might be good to mention a little more about what you’re hoping to achieve.

VirtualWire would probably be one of the best documented libraries out there but maybe the total amount of detail is a little daunting to start with. :slight_smile:


Thanks for the replies, guys.
@ follower: You’re right. :-[ Short story: I want to use switches/ buttons etc on the inputs of the transmitter arduino to control things (mainly motors) with outputs from the receiver arduino. I get that I might need an interface after the second arduino.
Quote:< one of the best documented libraries>: hmm, I’m sure I must have missed a lot of it. Any links? Apart from the site I downloaded VirtualWire from. I better check there again.

@dogsop: Are you using the libraries unmodified by your own code?( See my previous post for what I’m trying to do)
Even though I’m still struggling with code, I’m probably just having a serious brain fart. Eventually something will hit me the right way and the penny will drop. Hit me, guys!

In the VirtualWire download directory there is the VirtualWire documentation in PDF format.

If I understand you correctly you want to be able to push a button connected to the transmitter Arduino and then have the receiver Arduino operate a motor or whatever?

If so, then you will want to look at how to get the transmitter example to accept an input and send some data over the wireless link as a result. And then look at how to get the receiver example to perform an action when it gets the data.


@follower: We’re making some headway- Thanks for your input so far. Taking your reply a paragraph at a time:

  1. I have that PDF. It’s the one I’ve been trying to figure out.
  2. Correct, except I need to use multiple buttons & multiple motors.
  3. In section 4 of the PDF I see
    extern void vw_set_tx_pin(uint8_t pin);

which I understand is for setting the input pin, but I can’t seem to find an example of it anywhere else that gives me a clue how to use it. Where does the pin number go?

I also need to know if I can use that instruction, say 10 times in the one sketch to set 10 input pins.

I keep referring to the Arduino Primer (may not be its correct name) I downloaded months ago, which got me through simpler sketches, but Mike’s PDF almost looks like a different language!

Hi rodmac,

I’ve used the VirtualWire library to replace a broken transmitter/receiver in a RC car which has two motors, one to control forwards/backwards, and the other to control the steering (left/right).

You should only call vw_set_tx_pin(uint8_t pin) if you want to use a different pin for transmitting, the library uses pin 12 by default. This means that ‘pin’ (normally 12) will output data from the arduino to the Sparkfun transmitter.

You should use vw_send(uint8_t* buf, uint8_t len) to actually send the data to the transmitter.

Here’s part of the code I use in the remote:

#define pinLED 13
void setup() {
    vw_setup(2000);       // Bits per sec

void loop() {
  sendCommand('L', 12); //Go left and set motor speed to 12
  sendCommand('F',132);  //Go forward and set motor speed to 132
  sendCommand('S', 0); //Stop!

void  sendCommand(char command[], byte value) {
  char buffer[5]; //create buffer to hold data to be transmitted
 //create buffer to hold value converted to a string e.g '123' - max value 
 //is 255 ('cos val is of type byte).
  char num[4];
  memset(buffer, '\0', sizeof(buffer));
  memset(num, '\0', sizeof(num));

 //My command var is a single letter e.g. 'S' stop, 'R' right etc
  buffer[0] = command[0];

  itoa(value, num, 10); //convert value to a string
  //concatenate num to the buffer, result would be 'S0', 'F132', 'L12'
  strcat(buffer, num); 

  digitalWrite(pinLED, true); // Flash a light to show transmitting
  vw_send((uint8_t *)buffer, strlen(buffer)); //Send the data
  vw_wait_tx(); // Wait until the whole message is gone

Then in the arduino actually in the car (which uses the Sparkfun receiver)

void setup(){
    vw_setup(2000);       // Bits per sec

    vw_rx_start();       // Start the receiver PLL running
   //Receiver is connected to 11 (default pin in VirtualWire library)



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

    //see if we have received a message from our TX
    if (vw_get_message(buf, &buflen)) // Non-blocking
        //invalid message length must be S/B/F/L/R + number (max 3 digits)
        if (buflen < 3 || buflen > 5)

        char val[buflen]; //Same as buf, last char will be used for null terminator
        memset(val, '\0', sizeof(val));

        //Copy value from string i.e. 213 from R213 into separate string
        strncpy(val, (char *)buf + 1, buflen - 1);

        //convert string containing value to integer e.g. "213" to 213.
        int VAL = atoi ( val );

        switch (buf[0]) {
            case 'B':
                Serial.print("spin motor backwards at speed:\t")


            case 'F':
                Serial.print("spin motor forwards at speed:\t")


            case 'L':
                Serial.print("Steer car left at speed:\t")


            case 'R':
                Serial.print("Steer car right at speed:\t")

            case 'S':
                Serial.println("STOP THE CAR")

            default: //do nothing


Hi Veronica.
Thanks a million! Finally I see where I’ve been going wrong! I thought
vw_set_tx_pin(uint8_t pin); was setting up an input. Now I see that it sets the output pin.
Can I assume that the input pins are set using standard digital input call, e.g. pinMode(inPin, INPUT) ? I don’t see that in the part of the code you posted.

But just a quick look at your code has answered a lot of questions I haven’t even asked yet. I’m going to study it closely as soon as I get the chance.
I hope anyone else who is puzzled by virtualWire finds this topic.
Thanks again! :slight_smile:

Hi rodmac,

Yeah, sorry about not posting the full code, I’m using a Wii nunchuck as input for the remote and thought that code would just complicate the example too much. To answer your question, yes, you can use pinMode(inPin, INPUT) and then digitalRead(inPin) , you’re just reading the state of buttons, right ?



Hi Veronica,
No apology necessary! Your post is already changing my view of how libraries, e.g. VirtualWire, fit into the Arduino scheme. My lack of experience with them had me totally confused. It’s much clearer now.
Thanks again.