Saturday, 6 December 2014

433 Mhz RF Transmitter Receiver - Arduino Coding

Assalam u Alikum Everyone!

Today, as my very First post on this blog, I am going to explain the test working of 433 Mhz RF Transmitter Receiver using Arduino. You people will surely Enjoy this post.. :)


Component List
  1. 2x Arduinos
  2. Tx Rx pair of 433 Mhz RF Device
  3. RGB Led
  4. Portable Power sources for portability

I used Arduino Uno and Nano and sent a Text String over RF and received  the string displayed it in Serial Terminal and used RGB Led as message flag.


So here is the transmitter code
#include <VirtualWire.h> //library to be used
char cad[] = {"Bismillah Allah u akbar"} ;//Message
int cad_len = sizeof(cad) / sizeof(char);// Message length calculation
void setup() {
  vw_setup(2000);
  vw_set_tx_pin(3); // 433Mhz TX pin -? Arduino Digital 3
}
void loop() {
  vw_send((uint8_t  *)cad, strlen(cad));//Transmission
  delay(400);
} 

And here goes the transmitter code
#include <VirtualWire.h>
void setup() {
  Serial.begin(9600);
  vw_setup(2000);
  vw_rx_start();
  vw_set_rx_pin(3); // 433 Mhz Rx to Arduino Digital 3
}
String msg;
void loop() {
  msg = "\0";
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if ( vw_get_message(buf, &buflen) ) // if some data is coming i.e. message buffer is not empty
  {
    for (int i = 0; i < buflen; i++)    {
      msg += String(char(buf[i])); // add data to message string
    }
    if (msg != "\0")
      Serial.println(msg); //print msg rcvd ob serial terminal
    delay(100);
  }
}

A more complex approach to Rx 
if a clean message is  received, RGB Led shows Green. If msg is corrupted , Red Leds is On. and when no message is received Blue Led remains ON
#include <VirtualWire.h>
#define GREEN A1
#define BLUE A0
#define RED A2
void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  vw_setup(2000);
  vw_rx_start();
  vw_set_rx_pin(3); // 433 Mhz Rx to Arduino Digital 3
}
String msg;
char bufchar;
bool bl;
void loop() {
  msg = "\0";
  bl = 0;
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if ( vw_get_message(buf, &buflen) ) // if some data is coming i.e. message buffer is not empty
  {
    bl = 1;
    for (i = 0; i < buflen; i++)    {
      bufchar = char(buf[i]);
      msg += String(bufchar); // add data to message string
    }
    if (msg.equals("Bismillah Allah u akbar"))
      /*heck , if data Recived is True, Switch On Green Led.
      If data is corrupted switch on Red Led*/
    {
      Serial.println("Msg rcvd");
      digitalWrite(GREEN, HIGH);
      digitalWrite(BLUE, LOW);
      digitalWrite(RED, LOW);
    }
    else {
      Serial.println("Msg Error");
      digitalWrite(GREEN, LOW);
      digitalWrite(BLUE, LOW);
      digitalWrite(RED, HIGH);
    }
    Serial.println(msg); //print msg rcvd ob serial terminal
    delay(100);
  }

  if (bl == 0)
  { // if no data is recieved switch on Blue Led
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, HIGH);
    digitalWrite(RED, LOW);
  }

  delay(100);

}
 I have added self explanatory comments. I hope this tutorial has helped you. Stay Tuned

No comments:

Post a Comment