Electronic – arduino – Distance between 2 arduino’s using rf links

arduinodistanceRF

I currently have a setup where I send a char using a Tx of 434MHz and an Uno to a Mega with a Rx. The Mega counts how many times it receives the char and then if it falls below a certain number it triggers an alarm. Is this a viable way to measure the distance between two microcontrollers while indoors or is there a better way.

Transmitter (Mega)

#include <SoftwareSerial.h>

int rxPin=2; //Goes to the Receiver Pin
int txPin=5; //Make sure it is set to pin 5 going to input of receiver

SoftwareSerial txSerial = SoftwareSerial(rxPin, txPin);
SoftwareSerial rxSerial = SoftwareSerial(txPin, rxPin);
char sendChar ='H';

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin,OUTPUT);
  txSerial.begin(2400);
  rxSerial.begin(2400);
}

void loop() {
  txSerial.println(sendChar);
  Serial.print(sendChar);
}

Receiver

#include <SoftwareSerial.h>

//Make sure it is set to pin 5 going to the data input of the transmitter
int rxPin=5; 
int txPin=3; //Don't need to make connections
int LED=13;
int BUZZ=9;
int t=0;

char incomingChar = 0;
int counter = 0;

SoftwareSerial rxSerial = SoftwareSerial(rxPin, txPin);

void setup() {
  pinMode(rxPin, INPUT); //initilize rxpin as input
  pinMode(BUZZ, OUTPUT); //initilize buzz for output
  pinMode(LED, OUTPUT); //initilize led for output
  rxSerial.begin(2400); //set baud rate for transmission
  Serial.begin(2400); //see above
}

void loop() {
  for(int i=0; i<200; i++) {
    incomingChar = rxSerial.read(); //read incoming msg from tx

    if (incomingChar =='H') {
      counter++; //if we get bit "h" count it  
    }
    delay(5); //delay of 10 secs
  }
  Serial.println(incomingChar);
  Serial.println(counter); //prints the the bits we recieved

  if(counter<55) {
    //if we receive less than 100 bits than print out of range triggers alarm
    Serial.println("out of range"); 
    tone(BUZZ,5000,500);digitalWrite(LED,HIGH);
  }
  else {
    noTone(BUZZ);digitalWrite(LED, LOW);
    //if we get more than 100 bits than we are within range turn off alarm
    Serial.println("in range"); 
  }

  counter = 0;
  incomingChar=0;
}

Best Answer

That's ingenious, but unfortunately it is liable to be a very inaccurate system with poor repeatability. Range will be affected by antenna characteristics, and alignment. Power level would vary between units. Local conditions and RF noise will have some effect.

Other options include

  • Acoustic, usually ultrasonic - time of flight or triangulation. Sometimes amplitude as you are doing.

  • RF - time of flight or swept Doppler Radar or triangulation.

  • LASER. Similar to RF, Phase detection, ...

Many of the robot sites discuss range finding methods.