Xbee.h returning incorrect ADC values

I am trying to modify the tweet-a-watt project and push the data to pachube with an Ethernet shield instead of sending to twitter from my PC. I’m having some trouble reading the xbee analog inputs 0 and 4 (volts and amps). I have the xbee tx is setup just like the Tweet-A-Watt project calls for:

MY=1, SM=4, ST=3, SP=C8, D4=2, D0=2, IT=13, IR=1

I’m basically using the XBee_Test_IOSamples sketch that comes with Xbee.h library to read the data. When I read xbee analog inputs, I get this:

Volts = ( 163.00 211.00 322.00 489.00 650.00 766.00 826.00 837.00 785.00 665.00 493.00 335.00 224.00 172.00 164.00 226.00 351.00 524.00 679.00 )

Current = ( 322.00 489.00 650.00 766.00 826.00 837.00 785.00 665.00 493.00 335.00 224.00 172.00 164.00 226.00 351.00 524.00 679.00 0.00 0.00 )

You’ll notice that the current values are the same as the voltage values, just shifted two spots to the left. Anyone know what I am doing wrong?

Here’s my sketch:

#include <XBee.h>
XBee xbee = XBee();
Rx16IoSampleResponse ioSample = Rx16IoSampleResponse();

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

void loop() {
  float volts[19];
  float current[19];
 
  //attempt to read a packet
  xbee.readPacket();

  if (xbee.getResponse().isAvailable()) {
    // got something

    if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) {
      xbee.getResponse().getRx16IoSampleResponse(ioSample);

      Serial.print("Received I/O Sample from: ");
      Serial.println(ioSample.getRemoteAddress16(), HEX);

      Serial.print("Sample size is ");
      Serial.println(ioSample.getSampleSize(), DEC);

      for (int k = 0; k < ioSample.getSampleSize(); k++) {
          volts[k] = ioSample.getAnalog(0, k);
          current[k] = ioSample.getAnalog(4, k);
      }
     
      // Print array with voltage and current ADC values from Xbee
      Serial.print("Volts =   ( ");
      for (int i = 0; i < samples; i++) {
          Serial.print(volts[i]);
          Serial.print(" ");
      }
      Serial.println(" )");
      Serial.print("Current = ( ");
      for (int i = 0; i < samples; i++) {
          Serial.print(current[i]);
          Serial.print(" ");
      }
      Serial.println(" )");
     
    }
    else {
      Serial.print("Expected I/O Sample, but got ");
      Serial.print(xbee.getResponse().getApiId(), HEX);
    }
  }
  else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet. Error code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
}

Which XBee module are you using, Series 1 or Series 2?

If a Series 1 did you connect a reference voltage to the Vref pin of the XBee?

Look in the proper XBee manual for the pin number and the maximum Vref.

What is the real voltage at the XBee’s ADC input pins as read by a DVM?

I just followed the build instructions for Tweet-a-watt. According to the schematic (http://www.ladyada.net/make/tweetawatt/solder.html) Vref is connected to Vcc. I’m using a Series 1 xbee. I can’t measure the actual voltages because the xbee is inside the kill-a-watt housing. If I open it up to measure the volts, then I can’t plug it into the wall, as a result there is no power.

Regardless of the actual voltages at these inputs, I shouldn’t be seeing the same values on ADC 0 as ADC 4, just shifted by two samples.

I did a little testing with the xbee Tx plugged into the sparkfun xbee breakout board and plugged this into the USB port on my PC. All the ADC values read 1023, which is fine because there is nothing hooked up to the inputs, but I do notice some strange results depending on how may ADC inputs are enabled. The sample size is 19.

2 ADC inputs enabled (ADC 0 and 1)

ADC 0: All 19 samples are 1023

ADC 1: All 19 samples are 1023

2 ADC inputs enabled (ADC 0 and 3)

ADC 0: All 19 samples are 1023

ADC 3: First 18 samples are 1023, sample 19 is zero

2 ADC inputs enabled (ADC 0 and 4)

ADC 0: All 19 samples are 1023

ADC 4: First 17 samples are 1023, sample 18 & 19 are zero

2 ADC inputs enabled (ADC 0 and 5)

ADC 0: All 19 samples are 1023

ADC 5: First 17 samples are 1023, sample 18 & 19 are zero

3 ADC inputs enabled (ADC 0, 3, 4) - code only returns 14 samples, even though xbee is still set to 19

ADC 0: All 14 samples are 1023

ADC 3: First 13 samples are 1023, sample 14 is zero

ADC 4: First 13 samples are 1023, sample 14 is zero

4 ADC inputs enabled (ADC 0,3,4,5) - get mostly errors, sometimes 11 samples come through

ADC 0: All 11 samples are 1023

ADC 3: All 11 samples are 1023

ADC 4: First 10 samples are 1023, sample 11 is zero

ADC 5: First 10 samples are 1023, sample 11 is zero

So just enabling ADC or changing around which ones are enabled is causing some strange behavior.

I would expect strangeness if there is nothing connect to the ADC inputs.

Try connecting to a resistor ladder divider. Say four 2k resistor in series between Vcc and ground with an ADC input at each resistor pair junction. Or just a resistor divider or pot on each ADC input.

waltr:
I would expect strangeness if there is nothing connect to the ADC inputs.

Try connecting to a resistor ladder divider. Say four 2k resistor in series between Vcc and ground with an ADC input at each resistor pair junction. Or just a resistor divider or pot on each ADC input.

I think I already covered that scenario in my first post. It didn’t use a voltage divider, but I have voltages going into the two analog inputs.

Scott216:
I am trying to modify the tweet-a-watt project and push the data to pachube with an Ethernet shield instead of sending to twitter from my PC. I’m having some trouble reading the xbee analog inputs 0 and 4 (volts and amps). I have the xbee tx is setup just like the Tweet-A-Watt project calls for:

MY=1, SM=4, ST=3, SP=C8, D4=2, D0=2, IT=13, IR=1

I’m basically using the XBee_Test_IOSamples sketch that comes with Xbee.h library to read the data. When I read xbee analog inputs, I get this:

Volts = ( 163.00 211.00 322.00 489.00 650.00 766.00 826.00 837.00 785.00 665.00 493.00 335.00 224.00 172.00 164.00 226.00 351.00 524.00 679.00 )

Current = ( 322.00 489.00 650.00 766.00 826.00 837.00 785.00 665.00 493.00 335.00 224.00 172.00 164.00 226.00 351.00 524.00 679.00 0.00 0.00 )

You’ll notice that the current values are the same as the voltage values, just shifted two spots to the left. Anyone know what I am doing wrong?

Here’s my sketch:

#include <XBee.h>

XBee xbee = XBee();
Rx16IoSampleResponse ioSample = Rx16IoSampleResponse();

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

void loop() {
float volts[19];
float current[19];

//attempt to read a packet
xbee.readPacket();

if (xbee.getResponse().isAvailable()) {
// got something

if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) {
  xbee.getResponse().getRx16IoSampleResponse(ioSample);

  Serial.print("Received I/O Sample from: ");
  Serial.println(ioSample.getRemoteAddress16(), HEX);

  Serial.print("Sample size is ");
  Serial.println(ioSample.getSampleSize(), DEC);

  for (int k = 0; k < ioSample.getSampleSize(); k++) {
      volts[k] = ioSample.getAnalog(0, k);
      current[k] = ioSample.getAnalog(4, k);
  }
 
  // Print array with voltage and current ADC values from Xbee
  Serial.print("Volts =   ( ");
  for (int i = 0; i < samples; i++) {
      Serial.print(volts[i]);
      Serial.print(" ");
  }
  Serial.println(" )");
  Serial.print("Current = ( ");
  for (int i = 0; i < samples; i++) {
      Serial.print(current[i]);
      Serial.print(" ");
  }
  Serial.println(" )");
 
}
else {
  Serial.print("Expected I/O Sample, but got ");
  Serial.print(xbee.getResponse().getApiId(), HEX);
}

}
else if (xbee.getResponse().isError()) {
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
}
}

Your code is for one XBee.

If you would use 4 XBee transmitters for example with 4 different addresses, which code will you use to know which one is sending data?

I’m having the same issue here, the current values are the same as the voltage values,

shifted two positions to the left.

Did anyone solved this problem?

I’m using the official Arduino xbee shield, which comes already assembled. So I hope this isn’t a hardware problem.

Thanks in advance

Not sure if this will help (as this is several years later). Apparently despite the fact that some of these radios claim to have an internal reference, they in fact do not. I am using the Pro-900 and after some time spent on the phone with Digi it was made known to me that the reference pin must be connected to a voltage between 2.6v and Vcc. So… Hope that helps someone.