mercoledì 4 settembre 2013

Use Arduino with RF Modules

Hi guys, today I'm here with a really cool project.
If you have ever wondered how to make 2 arduinos communicates without using expensive shield, here is the solution: there are some RF modules sold for about 2$ (transmitter + receiver)  that offer one way, easy-to-use communication with a range of about 150 meters.
In this post I'll explain how to use this modules.

First of all you'll need:
-2 arduinos
-a rf transmitter
-a rf receiver

STEP 1: the transmitter
Connect the transmitter to arduino this way:




Upload this code to the arduino connected to the transmitter:
 
#include <VirtualWire.h>
void setup(){
  vw_setup(2000); // bps
  }
void loop(){
  send("hello world"); //your message
  delay(1000);
  }
void send (char *message){
  vw_send((uint8_t *)message, strlen(message));
  vw_wait_tx();
  }

STEP 2: the receiver
Connect the receiver to the arduino this way:





Upload this code to the arduino connected to the receiver:

#include <VirtualWire.h>
byte message[VW_MAX_MESSAGE_LEN];
byte messageLength = VW_MAX_MESSAGE_LEN;
void setup(){
  Serial.begin(9600);
  Serial.println("Device is ready");
  vw_setup(2000); //bps
  vw_rx_start();
  }
void loop(){
  if (vw_get_message(message, &messageLength)){
    Serial.print("Received: ");
      for (int i = 0; i < messageLength; i++){
        Serial.write(message[i]);
        }
      Serial.println();
    }
  }

STEP 3: test ;)
Connect the 2 arudino and check with the serial monitor if it works


I hope you like this post, if you have any question please comment or send me an email to damianoandre@gmail.com

Bye, Dami

18 commenti:

  1. good video... but if i want to send a value? do you know how to do?

    RispondiElimina
  2. convert your int in a string with this:
    String myString = String(n);
    and send it

    RispondiElimina
  3. Well,tried the two scetches and got errors :
    1) vw_setup not declared in this scope
    2) vw_max_message was not declared ...

    Karl

    RispondiElimina
  4. Kalle, have you installed VirtualWire from here? http://www.pjrc.com/teensy/td_libs_VirtualWire.html
    I just finished a project where I use the same rf modules to control my garden lights via remote control. If you still struggle, I'll try to help. I'll add details of my project by the weekend on: http://blog.digigram.za.net

    RispondiElimina
  5. it's possible trasmit in am? i try but i trasmit in wfm

    RispondiElimina
  6. Thanks for the clear and straightforward post - I got these working with no effort. I'm curious though about what kind of range others have seen with these. You mention 150m. I can't seem t get a signal to go beyond 5m no matter how I orient the modules.

    RispondiElimina
  7. So I can transmit a value, say 1 or 0 to the receiver, this works fine. My problem lies when I try to get the receiver to write this value to a pin to turn an LED on or off. I have tried digitalWrite (6, message[i]). This didn't work. Of course the message is a serial message so do you suggest I try analogWrite(6, message[i]); ? Any suggestions?

    RispondiElimina
  8. you can try:


    if(message[i] == "0"){
    digitalWrite(6, HIGH);
    }
    else{
    digitalWrite(6, LOW);
    }

    RispondiElimina
  9. you can try:


    if(message[i] == "0"){
    digitalWrite(6, HIGH);
    }
    else{
    digitalWrite(6, LOW);
    }

    RispondiElimina
  10. "ISO C++ forbids comparison between pointer and integer"

    RispondiElimina
  11. thanks for this tutorial, I'm planning to use the arduino devices for a little project, and as I'm new in this, I have a question, do I need the breadboard ? You didn't mention it, so I'm not sure.

    RispondiElimina
  12. If you're a beginner I suggest you to buy a breadboard, a small one costs just 9$ (http://www.ebay.it/itm/1pcs-MB102-Breadboard-830-Point-Solderless-PCB-Bread-Board-Test-Develop-DIY-/121142945487?pt=LH_DefaultDomain_0&hash=item1c34aea6cf)
    If you don't want to buy a bredboard you'll have to solder all components but I highly recommend you a breadboard

    RispondiElimina
  13. i don't see this method vw_set_tx_pin(pin#) in there.... shouldn't that be in there

    RispondiElimina
  14. Hi there another example with same RF devices, a basic protocol and a Gertboard

    RispondiElimina
  15. Woderful post. The rf module that you try is unsteable and sensitive to jam. If you need good performation, we suggest you try RF4432:
    http://www.appconwireless.com/PRODUCTS/showproduct.php?lang=en&id=7

    RispondiElimina
  16. Can you help in using this same code in 8051 microcontroller what changes should I do...

    RispondiElimina
  17. My name is Daiyaan and I am stupid

    RispondiElimina