Programming arduinos with XBees

I need to send three analog signals wirelessly using series one xbees. I have heard the best way to do this is to use an xbee shield on both sides and send the data serially using serial streaming, where the signals will be read through the analog pins and sent serially to the receiving arduino where they will be read serially. I have also heard this programming is easy but I have never worked with arduinos so is there any samples on how to do this programming.

There has been a few recent threads on using XBee’s with Arduino in the forums here. Look/search for them (Projects, Arduino and Wireless sub-forums) as a few have the same question you have and the answers are posted.

Here is a code I have came up with for the transmitting side…im using an Arduino mega.

if anyone notices anything I should add or take away or if its completely wrong please let me know.

int Sensor1 = 0;

int Sensor2 = 1;

int Sensor3 = 2;

void setup()

{

Serial.begin(9600);

}

void loop()

{

byte servoValue1;

byte servoValue2;

byte servoValue3;

int SensorV1 = analogRead(Sensor1);

int SensorV2 = analogRead(Sensor2);

int SensorV3 = analogRead(Sensor3);

Serial.print(SensorV1);

Serial.print(SensorV2);

Serial.print(SensorV3);

delay(100);

can’t tell since the code doesn’t say that you’re going to use the Arduino XBee library. Looks like no, as there are no dotted references to library class methods shown.

You probably don’t want to reinvent the wheel here.

Maybe you are trying to use the XBees in the transparent serial mode (wireless UART extension)?

Is Series 1 transparent only? I seem to remember reading this somewhere but I could be totally flaking.

If that’s the case, the code you wrote looks reasonable, at first glance. You probably want to format your output with delimiters or something otherwise your readings will all smash together - meaning, you read 125, 140, and 96 for your sensor readings. Your code would send “12514096”. You probably want to add colons or slashes or something so you’d instead send

(125/140/96) or something like that, to denote the start of a group of readings and separate them.

Rob Faludi’s book “Building Wireless Sensor Networks” talks about stuff like this. I’d recommend it - it’s useful for Arduino newbies too.

As an aside, Megas are really useful for developing Arduino code because of all the I/O and multiple serial ports. Make sure you’re using them to their best advantage - you can use Serial1 for talking to your XBee and the default Serial as a debug console, for example.

hherhold:
Is Series 1 transparent only? I seem to remember reading this somewhere but I could be totally flaking.

It’s transparent out-of-the-box/factory default. But can communicate with the microcontroller over serial with API-packet protocol if configured appropriately. The latter is much more structured, and the sending device adres is part of the packet. So you always know from who it came. But also adds protocol overhead.

Okay thanks everyone! with the receiving side we are controlling servo motors using a Simulink code. The Simulink code reads from certain pins. Using the Dout pin of the xbee the signals will no longer be in these separate pins of the arduino. To solve this problem we are hoping to use an arduino uno to receive the signals with another xbee and then output them through three separate pins to the mega so that they can be used in the Simulink code. Will the code for the receiving side be similar to the one I have posted above for the transmitting side?

I changed my code a little and used the serial monitor to check it. I had an input of 2 volts with a frequency of 1 KHz to test it and got numbers like 150 150 93 87 106 38 60 42 0 0 0 183. Just wanted to see if this is what I should be getting.

here is my code that I changed

void setup() {

Serial.begin(9600);

}

void loop() {

int sensorValue1 = analogRead(A0);

int sensorValue2 = analogRead(A1);

int sensorValue3 = analogRead(A2);

Serial.println(sensorValue1);

Serial.println(sensorValue2);

Serial.println(sensorValue3);

delay(1);

}

How about focussing on just 1 analog input signal? Then you can check how the Arduino senses that signal. And if the numbers match your input signal. That will be tricky to tell, since we don’t know the exact interval between those measurements. Also, you haven’t said anything about what shape that 2 volts, 1 kHz is supposed to be. So, you may tell us if that is what you should be getting (every 3rd value)!

[EDIT]Me thinks checking with a fixed steady DC voltage would tell you more how this is working.

I just tried sending 2 volts of DC voltage through one signal and got numbers ranging from around 930 to 1040 in the serial monitor. Im thinking this is wrong…

Yes, I used the above program on my Arduino Uno R3. (It’s so basic there can’t be anything wrong with it.) I disabled the measurement (and transmission) of A1 and A2 in my program, and increased the delay to 1 second. I connected a simple voltage divider to A0 (2 resistors:, 1K from 5V pin on top of a 2.2K resistor to GND) If I consider the voltage on the Aref pin (the analog reference value which would result in 1023), then the middle point of the 2 resistors neatly results in the right ADC value.

void setup() {

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

 int sensorValue1 = analogRead(A0);
// int sensorValue2 = analogRead(A1);
// int sensorValue3 = analogRead(A2);

 Serial.println(sensorValue1);
 //# Serial.println(sensorValue2);
 // Serial.println(sensorValue3);
 delay(1000); 
}

So I would question your voltage source, or the way it is connected. Did you also connect the minus or GND connection of the voltage source to the arduino’s GND?

If I connect nothing at all to A0, then the values change alot around 330. Especially if I come near with my hand or body. Which makes sense as the input pin is floating, and my body functions as an antenna/variable capacitor.

okay thank you so much, and this should work with the transmitting xbee to transmit the signal?

Sure, if the Xbee is setup to just transmit the characters as it receives them, then yes. It works just like a cable replacement. There might be a bit of lag though.

The analog inputs or other functions of the Xbee are not used. Atleast, that is what I understood from your feeback, it being undesired or unneccessary.

[edit]Ultimately you need a way to seperate the values, and add a ‘begin’ and ‘end’ marker, so that your Simulink code knows which value is the 1`st, 2nd and 3rd. And can notice when the sequence is messed up. As you can allways experience loss of data over wireless links. Don’t expect it to be perfect.

This is how my xbees are set up, the only changes I made are with the channel, pan ID, MY, and DL

transmitting xbee:

Channel:10, pan ID: 1234, MY:10, DL:11

receiving xbee:

Channel:10, pan ID:1234, MY:11, DL:10

Is this correct for sending the signals serially?

Yes, those settings are only to make them adres each other, and be on the same channe and PAN. The other registers in the XBee are good by default I think. Provided that you do not send the data too quickly.

Okay and if I decide to send the data quicker is there a way to do this without too many changes to the xbee and arduino?

No. If it was easy it was provided by default.However, I don’t have a hard number at which throughput rate you’ll need this. The Arduino has an internal serial data buffer, but it isn’t very large I think. The Xbees also have a small buffer. But can always get filled to the top, so you would need two additional lines between the Xbee (CTS and RTS pin) and it’s Arduino (any 2 data pins available) to let them tell the other to stop sending as the buffer is full. For the Arduino you need to write the code (or find a library that does this already), for it to respond to hold signals from the Xbee. The Xbee is pretty much hardware prepared to do this, but you may need to enable the RTS function on that pin (not sure which one), by writing a value to a specific register. Hardware flow control is what this is called. Software flow control is also possible, by sending specific bytevalues to make the other side stop sending temporarily.

Details are in the XBee manual for your device. Make sure you read it inside and out.

Ofcourse the baudrate plays into this too. The default baudrate (9600 for both Arduino and Xbee) is probably low enough for this to be unlikely. But it may happen.