wireless communication problem

Hello Everyone,

I have two arduino uno boards, two xbee shields(http://www.sparkfun.com/products/9976), and two series 1 xbee chip antennas.
I am trying to copy the project here http://lab.guilhermemartins.net/2008/12/24/serial-comunication-between-arduinos-with-wire-wireless/ with only one LED connecting to digital pin 5 of the receiver, and an IR range finder(http://www.robotshop.com/productinfo.aspx?pc=RB-Dem-01&lang=en-US) connected to analog pin 2.
The receiver is getting something, but totally different values at first, then kept at 0. So the LED was not lit.
Does anyone see the problem here? Do I have to assign destination address for both xbees in the code?
Thanks!!

Here are my codes.

//sender

int analogValue2, val2;

void setup()
{
  Serial.begin(19200);
}

void loop()
{
  analogValue2 = analogRead(2);
   
  val2 = map(analogValue2, 0, 1023, 254, 0);  //255 for SYNC
 
  Serial.print(255,BYTE);  //SYNC char
  Serial.print(val2);
 
  delay(1000);
}
//RECEIVER
byte incomingByte, sensor1;

void setup()
{
  Serial.begin(19200);
  Serial.println("Ready!");
  
  //led pins
  pinMode(5, OUTPUT);
  
  delay(1000);
}

void loop()
{
  if(Serial.available()) {  //are there any bytes available on the serial port?
    //assign bytes to the var 'incomingByte'
    incomingByte = Serial.read();
    Serial.print(int(incomingByte));
    
    if((int(incomingByte) == 255)){
      sensor1 = Serial.read();
      Serial.print("Sensor 1 = ");
      Serial.print(int(sensor1));
    }    
  }
  analogWrite(5, sensor1);  
}

Your sender never reads from the serial port, so, in the receiver, what is this for?

    Serial.print(int(incomingByte));

The receiver is getting something, but totally different values at first

How do you know what the receiver is getting?

You should isolate the problem. Put the LED (with resistor) on the sender, WITHOUT the XBee shield. Get it reacting to the IR sensor output properly. Then, put the XBee shield back on, with the switch in the DLINE position (on both shields).

Use NewSoftSerial to create a software serial port involving pins 2 and 3. Use the software serial port to talk to the XBee and the hardware serial port to talk to the PC.

This way, you can send data to the XBee and the serial monitor, or receive data from the XBee and send it to the serial monitor.

Echo what you send, and what you receive (on a different computer or different instance of the IDE/Serial Monitor on the same computer), to make sure what is sent IS what is received.

Do I have to assign destination address for both xbees in the code?

No. You assign MY and DL for each series 1 XBee using X-CTU (from digi.com).

Hi PaulS,

Thanks a lot for the prompt reply!!
I am a newbie about wireless communication with Arduino boards, and am really lost by looking at other people's projects.

When you say

Your sender never reads from the serial port, so, in the receiver, what is this for?

    Serial.print(int(incomingByte));

you mean I should have "Serial.read()" in the sender too? I thought Serial.print(val2); sends the value to serial port, and in the receiver, by using "Serial.read()", it automatically gets incomingByte from serial port?

How do you know what the receiver is getting?

I connected the receiver to my laptop and opened the terminal window. So I can see it's printing something.

You should isolate the problem. Put the LED (with resistor) on the sender, WITHOUT the XBee shield. Get it reacting to the IR sensor output properly. Then, put the XBee shield back on, with the switch in the DLINE position (on both shields).

This is a good idea. I'm going to try it out. Thanks! But when you put the switches in the DLINE position, the xbees are not using serial port to communicate, right? Instead they should use the software serial port to communicate as you said below. So I can monitor both xbees from 2 separate PCs, right?

No. You assign MY and DL for each series 1 XBee using X-CTU (from digi.com).

I was not using X-CTU, I guess this causes another problem. Thanks for your thorough explanation!!

I was not using X-CTU, I guess this causes another problem.

Well, that explains why the two XBees don't communicate.

Hi Paul,

I tried splitting the project. I used one Arduino only with the IR sensor and LED circuit. The LED was responding to the IR received signal correctly.

Then I check out NewSoftSerial and took some time to understand how it works. Here are my new codes.

//sender
#include <NewSoftSerial.h>

NewSoftSerial mySerial(2,3);
int analogValue5;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  analogValue5 = analogRead(5);
  
  Serial.print(255, BYTE);  //SYNC char
  Serial.print(analogValue5);
  
  if(Serial.available())
    mySerial.print(Serial.read(),BYTE);
  
  delay(150);
}
//RECEIVER
#include <NewSoftSerial.h>

NewSoftSerial mySerial(2,3);
byte incomingByte, sensor1;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Ready!");
  
  //led pins
  pinMode(5, OUTPUT);
  delay(1000);
}

void loop()
{
  if(mySerial.available()) {  //are there any bytes available on the serial port?
    //assign bytes to the var 'incomingByte'
    incomingByte = mySerial.read();
    Serial.print(int(incomingByte));
    
    if((int(incomingByte) == 255)){
      sensor1 = mySerial.read();
      Serial.print("Sensor 1 = ");
      Serial.print(int(sensor1));
    }
   }
  analogWrite(5, sensor1);
}

I plugged the xbee shields back on to the Arduino boards and switched to DLINE, uploaded the two sketches. The two objects(the sender and the receiver) are plugged to two different PCs.
This time I've pre-configured the two Xbees with X-CTU, made each one the destination of the other.
But after uploading the sketches, I still did not get anything other than the "Ready!" word printed by the hardware serial port on the receiver's terminal window. I tried switching both to UART, and both to DLINE too... Neither worked. The sender's terminal window is printing each analogvalue gotten from Pin5 though.

I am thinking maybe I'm still not using NewSoftSerial correctly. Could you advice how to use it correctly? Thanks a lot!!

The XBee and the Arduino can talk to the serial port on the Arduino at the same time.

Try your code using Serial only.

Hi Paul,

It turned out that one xbee shield was broken...
I replaced it with a new one and now the two are communicating. I can see the DIN DOUT LEDs blinking all the time.
The receiver terminal is also printing "sensor1=some value". But the values are really big, much bigger than the values printed on the sender's terminal.
The receiver's terminal stopped printing after several dozens of value prints when both are in UART mode.
But if I switch the receiver to DLINE and keep the sender in UART mode, the receiver keeps printing, with wrong values though...
Do you see any reason for this? Thanks!!

Here are parts of the print outs on both terminals.

Sender terminal
ÿ464ÿ466ÿ468ÿ485ÿ466ÿ464ÿ465ÿ465ÿ465ÿ465ÿ467ÿ469

Receiver terminal
Ready!
255Sensor 1 = 525548255Sensor 1 = 525457255Sensor 1 = 525450255Sensor 1 = 525448255Sensor 1 = 525353255

I guess 'ÿ' is the character of ASCII 255. So for example, when sending 464, the receiver gets 525548. When I turned the IR sensor to send small values, like 2 or 3, the receiver gets some values around 50...

Sensor 1 = 525548255

If you printed spaces between the values, you would see
Sensor 1 = 52 55 48 255
52 is the ASCII code for 4
55 is the ASCII code for 7
48 is the ASCII code for 0

Printing the values as characters, instead of integers, you would have seen
Sensor 1 = 470ÿ

Not printing data that wasn't there, you would have seen
Sensor 1 = 470

Thanks, Paul!
Both are printing the same characters now.
So it would always be better to print characters instead of integers, right? Because serial port sends 8-bit characters, right?

Because serial port sends 8-bit characters, right?

The serial port handles 8 bit bytes. It is up to you to read and write the values the same on both ends.

Hi Paul,

I tried this simple project without software serial.
I used 2 9v batteries to power up the boards. Both sender and receiver are switched to UART. But it seems this time it's not working. The LED is not lightening. The sender's DIN LED is blinking while the receiver's DOUT LED is blinking. This is so weird. Could you suggest what is wrong here? Thanks!

Here are my code

//sender

int analogValue5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  analogValue5 = analogRead(5);
  
  Serial.print(255, BYTE);  //SYNC char
  Serial.print(analogValue5, BYTE);

  delay(50);
}
//RECEIVER

byte incomingByte, sensor1;

void setup()
{
  Serial.begin(9600);
  //Serial.println("Ready!");
  
  //led pins
  pinMode(5, OUTPUT);
  delay(1000);
}

void loop()
{
  if(Serial.available()) {  //are there any bytes available on the serial port?
    //assign bytes to the var 'incomingByte'
    incomingByte = Serial.read();
    //Serial.print(incomingByte);
    
    if((int(incomingByte) == 255)){
      sensor1 = Serial.read();
      //Serial.print("Sensor 1 = ");
      //Serial.print(sensor1);
      //Serial.print(" ");
    }
   }
  analogWrite(5, sensor1);
}
  if(Serial.available()) {  //are there any bytes available on the serial port?
    //assign bytes to the var 'incomingByte'
    incomingByte = Serial.read();
    //Serial.print(incomingByte);
    
    if((int(incomingByte) == 255)){
      sensor1 = Serial.read();

If there is a byte to read, read both of them. How's that working for you?

Never mind. It's clear that the answer is "not very well". Wait for there to be two bytes before reading anything.

PaulS:
Wait for there to be two bytes before reading anything.

But the sender sends two bytes at a time. So if the first byte the receiver got is the sync byte, then the following byte must be the sensor value. It worked when I was using software serial...

Should I write

if((int(incomingByte) == 255) &&(Serial.available()))
      sensor1 = Serial.read();

instead?

But the sender sends two bytes at a time.

No, it doesn't It sends two bytes, one after the other. But, it sends them like I type - slowly.

The Arduino can read the data far faster (many orders of magnitude) than it can be sent. Nearly as soon as the first byte appears, the Arduino discovers that it is available, and reads that one and the one that is still being sent (i.e. nothing).

Should I write

instead?

You could. It wouldn't work. All that it would do is ensure that sensor1 was never valued.

You need to wait for there to be two bytes.

if(Serial.available() > 1) { //are there any bytes available on the serial port?

The Serial.available() function does not return true/false. It returns the number of characters available to read. Wait for it to report that both have arrived before you read anything.

Hi Paul,

Thanks for helping!
I finally found the real problem. It was the power supply.
When I switch to USB power supplies for both, it worked. But still didn't work with battery supplies.
This is so weird. Do you have any idea why the battery supplies don't work? I can see the sender's DIN LED and the receiver's DOUT LED blinking all the time as they do while using USB power supplies. But the LED which connects to the output pin of the receiver is not lightening up...