Search code examples
arduinoarduino-unoxbee

Why are my serial readings not accurate? Using 2 xbees


I have one XBee-Arduino transmitting a simple int counter to a receiver XBee-Arduino. The receiver prints fine until it gets to 16 and beyond. This is an example of my outputs:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 48 17 50 19 52 21 54 23 56 25 26 59 28 61 62 63 64 65 98 67 68 69

I have tried new XBees and the XBees do not seem to be the problem.

Transmitter Code:

#include "SoftwareSerial.h"
int count = 1;

// RX: Arduino pin 2, XBee pin DOUT.  TX:  Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3);

void setup() {

  XBee.begin(115200);
  Serial.println ("Initializing...");
}

void loop() {

    XBee.write(count);
    delay(1000);
    count++;

}

Receiver Code:

#include "SoftwareSerial.h"

// RX: Arduino pin 2, XBee pin DOUT.  TX:  Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Started Ground station");
  XBee.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:

  if (XBee.available())  
  {
    int c = XBee.read();
    Serial.println(c);  
    delay(1000);
  }
  else
  {
    Serial.println("XBee not available.");
    delay(1000);
  }
}

I just want the receiver to print the counter as it is. Not sure why I am getting those random numbers after 15. Any help is appreciated.


Solution

  • The incorrect numbers are different by one bit.

    This could be caused by different timing when reading the serial line, especially at high baud rates (Software serial isn't particularly reliable at the higher baud rates). You can see where a bit has drifted into the timing for an adjacent bit e.g.

    16 = 0001 0000 was interpreted as 0011 0000 = 48
    18 = 0001 0010 was interpreted as 0011 0010 = 50
    20 = 0001 0100 was interpreted as 0011 0100 = 52
    27 = 0001 1011 was interpreted as 0011 1011 = 59
    34 = 0010 0010 was interpreted as 0110 0010 = 98
    

    Try using a lower baud rate so that the timing isn't as critical and the bits are less likely to drift into the timing for adjacent bits.