How to do RF Communication between 2 Arduinos (and more!)

We recently ordered some very cheap RF modules online to test out with our Arduino’s that we have here, despite being very cheap products they do seem to work incredibly well – So much so that we thought they deserve an article on how to get them up and running and working with two or more Arduino’s (with example sketches)

First of all, the components you’ll be needing are:

You’ll need a basic understanding of the Arduino IDE and basic programming skills to implement the example code posted below.

No matter how hard we looked, we couldn’t find a datasheet for the above components; the pins and voltages are fully labelled though. You can find the components on eBay or from multiple chinese wholesale manufacturers – we ended up paying £4.50 for two of each.

rf

Both components have the pins labelled up; the transmitter having DATA, VCC (5v) and GND and the receiver having GND, DATA, DATA, VCC (5v)Only 1 of the data pins is needed and it doesn’t matter which.

After you have connected these up to the Arduino (or two Arduino’s) make sure to install the VirtualWire library which is linked above. Also note that an antenna is normally used, as this was a cheap product we had to make our own – which was a 15cm piece of coiled wire. It isn’t shown in our pictures as we just tested the transmitter/receiver next to each other but if you are using any sort of distance it will be required.

Our test setup looks like the following once all connected up (Arduino Leonardo / Arduino Uno R3):

ardrf

Let’s get started with the actual coding. It is actually very, very easy to use RF Communication thanks to the VirtualWire library as this does most of the work. All you will need to do is set the transmitter and receiver pin vw_set_rx_pin() and vw_set_rx_pin() as seen in the below sketches.

After much experimenting we found that it wasn’t really efficient to send long strings of data (such as “led1_on” etc.); it would sometimes corrupt at larger distances and it was slower for the transmitter to send the data out – so we came up with another solution that worked well, so much so that we saved nearly 70ms transmitting data by using this new method.

The best course of action was to assign numbers as commands; for example if we send the number 1 then perform a certain action, if we send number 6 we perform another action etc. this would allow for the minimum amount of data to be sent and it would be very easy to check on the receiving end what function to run.

Whilst this was a good working solution this wouldn’t really work if we have multiple RF receivers listening for single digit commands. We then thought about how we can separate this communication between multiple RF receivers, the solution we used was to assign each transmitter a pin code to go along with the command, this means that we can have as many RF transmitters/receivers as we want listening for different data each assigned it’s own pin code.

The transmitting sketch example is below:

Looking at the code above, you can see there isn’t much! All it is doing is setting which pin on the Arduino the transmitter is connected to and using the VirtualWire to send out data. The function sendMessage(pin, command) is just a wrapper for transmitting data – It sorts the pin code and sends out a string such as “1.5″ 1 being the pin and 5 being the command; which repeats every 10 seconds.

You can uncomment all of the debug lines to see what data is being sent and how long it took to transmit. You can find the code for the receiver below:

The receiving sketch is below:

This is a little bit more complex but it’s still quite simple to use. We listen in the loop() function for vw_get_message and if we have some data from the connected RF receiver it will store it in the message byte array. We then send this over to our processResponse function which will strip the message with a “.” delimiter looking for a pin code and command.

If the pin code is correct and the command is valid it will return the command number (5 in this example) or it will return 0 if the pin code is wrong or command is 0. This will allow you to check the command variable for a number and perform a certain task.

An example of this working with many different commands may be the following:

Or you can use a series of If statements checking the command numbers, it’s entirely upto you how you use it.

If you’re wanting to use RF communication for performing tasks (e.g. turning things on/off) then this should be all you need; however if you are wanting to send actual data such as temperatures or other values then this will need some slight modification (it should be simple enough to work out how to add an extra delimiter and split the data up)

That’s it for now, but we have a lot more coming soon for RF communication; we thought it best to cover the basics before we start posting up more code.

Email this to someoneShare on Google+Pin on PinterestTweet about this on TwitterShare on RedditShare on Facebook

2 thoughts on “How to do RF Communication between 2 Arduinos (and more!)

  1. Ok I can’t be certain so I need to ask;
    I have an uno set up to perform a sequence of events. right when I hit the button to start the sequence I want this uno to tell another uno to run its own independent sequence. can this be done with this ^^? I may also like to consider them to work in reverse which im sure would require a 2nd pair of modules.

  2. Hi,
    I love this code and have modded it a bit, i was wondering if there was a way of making the pincode more than 1 digit long? also i have got the code sending command and pin code when buton to pressed and stopping when released and the receiver end reading it and seeing it stop, sending a pin high when it reads it but i cant get i to go low when it stops receiving it. wen ever it looks like it will work It seems to make the pin go to 0.16 when low to 0.24 when high. can you help?
    tom

Leave a Reply to Gus Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">