RF communication between Arduinos

From ESE205 Wiki
Revision as of 05:50, 12 December 2017 by Nb275 (talk | contribs)
Jump to navigation Jump to search

Overview

Many people have sent communications between an Arduino and another computer using a serial connection, perhaps in a course like CSE 132. Most people, though, do not have experience communicating wirelessly between Arduinos using Radio Frequency technology. Let's dig in.

Materials

You will need:
-2 Arduinos (most types will work, uno and mega definitely will)
-virtualWire library download (http://www.resistorpark.com/arduino-virtualwire-library-download/)
-RF Link Receiver (https://www.sparkfun.com/products/10532)
-RF Link Transmitter (https://www.sparkfun.com/products/10534)
-A breadboard
-Around 15 wires
-1 7V-12V power supply for Arduino
-1 Serial connection for Arduino

Setting up the transmitter

-connect the top pin on the transmitter to GND on Arduino ('top' pin is the one farthest away from the 'ant' pin)
-connect second from top pin on transmitter to digital pin 12 on Arduino
-connect third pin from top on transmitter to 5V power supply on Arduino

Upload the following code to the Arduino connected to the transmitter:

#include <VirtualWire.h>

char *controller;
void setup() {
pinMode(13,OUTPUT);
vw_set_ptt_inverted(true);
vw_set_tx_pin(12);
vw_setup(2400);// speed of data transfer Kbps }

void loop(){
controller="1" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(2000);
controller="0" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,0);
delay(2000);

}

Transmitterr.jpeg PrettyTransmit.png

Setting up the receiver

-connect GND on Arduino to black command strip
-connect 5V on Arduino to red command strip
-connect second pin from top on receiver to black command strip (top refers to where the receiver antenna is)
-connect third pin from top on receiver to black command strip
-connect fourth pin from top on receiver to red command strip

-connect fourth pin from bottom on receiver to red command strip
-connect second pin from bottom on receiver to digital pin 12 on Arduino
-connect bottom pin on receiver to black command strip
Upload the following code to the Arduino connected to the receiver:

#include <VirtualWire.h>

void setup()
{Serial.begin(9600);
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(12);
vw_setup(2400); // Bits per sec
pinMode(13, OUTPUT);

vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
if(buf[0]=='1'){
digitalWrite(13,1);
} if(buf[0]=='0'){
digitalWrite(13,0);
}}}

ExhibitB.jpeg PrettyReceive.png

Conclusion

Upload the transmitter code to the transmitting Arduino, then plug into into a power source. Upload the receiver code to the receiving Arduino, and watch the magic happen. The built in LED's on both Arduinos should blink simultaneously every two seconds. Two prove that RF communication is actually happening, rather than the lights just happening to be synched up, remove the power source from the transmitter and plug it back in a few seconds later. The lights should synch up again.



Sources

virtualWire library download http://www.resistorpark.com/arduino-virtualwire-library-download/
RF Link Receiver https://www.sparkfun.com/products/10532
RF Link Transmitter https://www.sparkfun.com/products/10534
Datasheet/wiring tutorial (and pictures) https://www.sparkfun.com/datasheets/RF/KLP_Walkthrough.pdf
Code http://www.instructables.com/id/RF-315433-MHz-Transmitter-receiver-Module-and-Ardu/