Wireless Data: A basic remote control!

When I first started tinkering with Arduinos, I bought a cheap wireless transmitter, and the matching receiver. It was only recently (the day before yesterday) that I had multiple Arduinos and the time to try working on this stuff. Long story short, this was a headache! Pain as it was, I finally managed to get it working!

Bill of Materials:

Now let me start by saying the code I used is just a modified version of the sample code from Sparkfun. At this point it may seem that I am biased towards Sparkfun, and their products. Well, I am. They have great technical support, and awesome products for people who don’t understand what they’re doing. Like myself for instance. I used the Virtual Wire library for Arduino, linked from the Sparkfun product page for the Receiver and Transmitter with a few minor modifications!

In files VirtualWire.cpp and VirtualWire.h Change:

#include “WProgram.h”

To:

#if defined(ARDUINO) && ARDUINO >= 100
#include “Arduino.h”
#else
#include “WProgram.h”
#include <pins_arduino.h>
#endif

The Setup: Transmitter

Connect Transmitter Pin1 (GND) to Ground, Pin2 (DATA) to Arduino Digital Pin 3, Pin3 to VCC (3v-12v) and lastly Pin4 to a piece of wire about 13CM long. I used a breadboard jumper wire about 4 inches long and reliably received data at ~20 meters.

Connect Arduino Pins 8&9 to momentary push buttons, and the buttons to ground. It should look something like this:

Transmitter.Fritz

This was my first attempt at “Fritzing”. I think I like it, and there may be more in the future.

That’s it for the Transmitter, now for the Receiver!

The Setup: Receiver

The Receiver has 8 pins, 3 of them ground, 2 +5V(4.9v-5.1v <Important!!) 2 DATA, and of course an Antenna pin.

Connect the GND pins (Pins1, 6, and 7) to ground. Connect the Two +5V Pins to 5v respectively, connect the First DATA pin to Arduino Pin2. I left the second DATA pin (Pin3 on the receiver) disconnected. Some people have said to tie it directly to ground, others said to use a 200k Ohm resister to tie it to ground. In my case leaving it alone worked just fine, you may have mixed results.

I connected Arduino Pin8 to an LED with a 150 Ohm resistor. This will be used to show the DATA is received properly.

Now for the code:

Transmitter:

#include <VirtualWire.h>

void setup()
{
Serial.begin(9600); // Debugging only
Serial.println(“setup”);

// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_tx_pin(3);

pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);

digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}

unsigned long time;
unsigned long lastTime = time; //Used to determine time since a button has been pressed.

void loop()
{
char *msg;
time=millis(); //Used to determine time since a button has been pressed.

//The next blocks of code check to see if a button attached to a pin is being pressed.
if(digitalRead(8) == LOW){
char *msg = “A”;
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println(*msg);
lastTime=time;
digitalWrite(13, false);
delay(200);} //A simple de-bounce code. If it’s not used, it may send the same signal 10+ times before you can depress the button

if(digitalRead(9) == LOW){
char *msg = “B”;
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println(*msg);
lastTime=time;
digitalWrite(13, false);
delay(200);}

if(digitalRead(10) == LOW){
char *msg = “C”;
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println(*msg);
digitalWrite(13, false);
delay(200);}

if(digitalRead(11) == LOW){
char *msg = “D”;
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println(*msg);
digitalWrite(13, false);
delay(200);}

/*

//Uncomment this code if you’re having trouble receiving  just one byte/character after a single button press.
if((time-lastTime)>1000){
char *msg = “7″;
digitalWrite(13, true); // Flash a light to show transmitting
Serial.println(“Sending ConfigBit”);
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
lastTime=time;
}
*/

}

The Receiver:

//
// SparkFun Electronics 2011
// NPoole
//
// RF ASK Transmitter/Reciever Example
//
// This code depends on the VirtualWire Library for Arduino and is
// based on the example code provided by Mike McCauley (mikem@open.com.au)
// See VirtualWire.h for detailed API docs.
//
// This example shows how to use the VirtualWire library to send and receive
// simple messages and use them to control digital I/O pins. Buttons are
// connected to the transmitting Arduino on pins 8-11 (to ground, internal 20k
// pull-up resistors are set in the code) and in the same fashion,
// LEDs are connected to the recieving Arduino on pins 8-11. When a button is
// pressed on the transmitter, the corresponding LED will light on the reciever.
// This document contains both transmitter and reciever code, simply de-comment
// the piece of code you need to use.
// RECIEVER CODE

#include <VirtualWire.h>

void setup()
{
Serial.begin(9600); // Debugging only
Serial.println(“setup”);

// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_rx_pin(2);
vw_rx_start(); // Start the receiver PLL running

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop()
{

// digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;

digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
//Serial.print(“Got: “);

for (i = 0; i < buflen; i++)
{
char nChar=buf[i];
//Serial.println(buf[i]);
if(nChar == ‘A’){digitalWrite(8, HIGH);Serial.print(“Got: “); Serial.print(nChar);}
if(buf[i] == ‘B’){digitalWrite(8, LOW);Serial.print(“Got: “); Serial.print(nChar);}
if(buf[i] == ‘C’){digitalWrite(10, HIGH);}
if(buf[i] == ‘D’){digitalWrite(11, HIGH);}
if(nChar == ’7′){Serial.print(“Config Recieved.”);}
Serial.print(” “);
}
Serial.println(“”);
digitalWrite(13, false);

}
}

As I mentioned, I reused the Sparkfun example code. I split it into two parts, and modified them to suit my needs. I left their header in the Receiver code, if you didn’t notice :P

Usage: If it all works correctly, you should be able to press Button 1 (attached to Transmitter Arduino, Pin8) and the LED on Receiver Arduino should light up! Pressing Button2 (attached to Transmitter Arduino, Pin9) should turn the LED off.

Notes: Powering the transmitter from an Arduino UNO’s 5V Pin doesn’t exactly work. Since I am powering the Arduino from a 5V USB ‘wall wart’, I used the VIN Pin to power the transmitter. Using a Duemilanove, I did not experience this issue. For antenna’s, I used a 4 inch breadboard jumper wire on the transmitter, and an 8 inch on the receiver. Though other lengths of wire didn’t seem to change anything on either end.

About these ads

17 responses to “Wireless Data: A basic remote control!

    • Are you talking bidirectional communication? No, I have not. Using these cheap modules you would have to use one of 315MHz transmitters and receivers, and one of the 434MHz transmitters and receivers or a transceiver respectively. You can’t have more than one transmitter running on the same frequency at the same time. It would be easily possible though. I’d test it out, but I don’t have the 434MHz modules on hand. Why do you ask?

  1. Pingback: Remote Control Tank: The Transmitter | Tired Juan's Blog

    • The LED is only there to show that data is being received. I can possibly help. Do you want the LED to toggle on and off after a button press? Do you want it on only while the button is being pressed? (doesn’t work very well with these units)

      • yes i want it to stay on whilst i am pressing the button and after i release the button it switch off because i want to switch on a motor by pessing the button instead of an led i appreciate if you can help me thanks a lot .

      • I’ve been having a problem with this myself. While I’ve been able to send data over long distances reliably, I can’t seem to get it to stream properly. I tried creating a remote for a tank platform I have, but it simply won’t stay connected. I’ll revisit this blunder of mine and see if I can’t get it working in the next few days. Check back next week for new posts and I’ll address the issue (hopefully) by next Monday.

  2. long distance isn’t that much of an issue for my project but my main focus goes on trying to switch the motor on and off from the receiver and I just can’t get do it. I thought you can help me because instead of an indicator LED where it shows that data is received i want that when data is received the motor switches on. thanks in advance for your time and help.

      • First of all i wish to thank you for your help, and if possible when you will revise your issue all i wish is to solve my LED/motor problem in which i aim to transmit data via 2 arduino’s to turn constantly on my LED/motor. I know this is extra work for you but i would really appreciate it.

      • Via 2 Arduinos? Unless you’re using something like a 2.4ghz transceiver, it’s not going to work. Using the cheap 315/434 mhz transmitters, one would interfere with the other if they tried to transmit during the same times. You’d have to have one 315 and one 434, or receivers on transmitting Arduinos to check if the other is transmitting. I’ll do my best to get one working, but you’re on your own for two.

      • I think my problem is hardware related. I just can’t get a stable connection between the two, and it was working when I originally set things up. I ordered some parts, but it’ll be a while before they come in from China. Sorry it took so long to respond, I thought it was code, but I’ve been through it again and again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s