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

4 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?

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 )

Connecting to %s