0
\$\begingroup\$

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;
}
\$\endgroup\$
1
  • \$\begingroup\$ Will these 2 be line of site of each other? What type of accuracy/precision are you looking for? \$\endgroup\$
    – Kellenjb
    Commented May 2, 2012 at 22:26

2 Answers 2

6
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Thx Russel but wouldn't Acoustic be an issue of interference from walls same with Laser? I've been thinking about this for a while and it is true that it is inaccurate as sometimes they would be right next to each other and my count would be zero. \$\endgroup\$ Commented May 2, 2012 at 19:12
  • \$\begingroup\$ So I've been thinking now would I be able to place one ultrasonic range finder on the uno and another on the mega would they be able to tell the distance from them? \$\endgroup\$ Commented May 2, 2012 at 20:06
2
\$\begingroup\$

Doing that, you are not able to measure the distance. You are only able to know whether the link is alive or not, and the percentage of the time that it has been alive.

Measuring distance is a very complex task. One method would be to measure round-trip travel times (or one-way times, if both systems shared a very accurate time reference). Since RF waves travel at the speed of light, you need to measure very short times, and therefore need to deal with high-frequency circuits.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.