I bought two RF links from SparkFun a while back (a
315Mhz and a
434Mhz). A few nights ago, I actually hooked one of them up and successfully ran a test program between two Arduinos. Here's the code for the receiver:
#define rxPin 2
#define txPin 3
byte incomingByte = 0;
SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
rfSerial.begin(2400);
Serial.begin(2400);
}
void loop() {
incomingByte = rfSerial.read();
Serial.println(incomingByte, DEC);
incomingByte = 0;
}
And for the transmitter:
#define rxPin 2
#define txPin 3
SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);
byte outgoingByte = 0;
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
rfSerial.begin(2400);
Serial.begin(2400);
}
void loop() {
Serial.println(outgoingByte, DEC);
rfSerial.println(outgoingByte, BYTE);
outgoingByte++;
delay(10);
}
Pretty simple. It just sends the numbers 0-255 repeatedly and prints them out to serial (use the serial monitor to watch the bytes fly by). The part that threw me for awhile was that the receiver has two data pins. I read
somewhere (*ahem* SparkFun KLP Walkthrough Tutorial) that only one of them was useful (pin 2) and the other (pin 3) was optional and could be tied to ground. After banging my head against the wall, I looked at the receiver data sheet which clearly shows that the data pins should be tied together! Once I corrected my wiring, it worked beautifully. So, last but not least, a photo.
9 comments: