How to make XBee sleep?

I have read the XBee manual (pg. 24-25), trying to figure out how to put it to sleep, but what I tried did not work. Then I found an example sketch using XBee sleeping, and I have condensed the code into this:

#include <avr/sleep.h>
#define XBEE_sleepPin 6

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

void xbeesleep() {
Serial.println("sleep");
pinMode (XBEE_sleepPin,INPUT);    // put XBee to sleep
digitalWrite(XBEE_sleepPin,LOW);  // Setting this pin to LOW turns off the pull up resistor, thus saving precious current
}

void xbeewake() {
Serial.println("wake");
pinMode(XBEE_sleepPin,OUTPUT);   // Set the "wake-up pin" to output
digitalWrite(XBEE_sleepPin,LOW); // wake-up XBee
delay(1000); //make sure that XBee is ready
}

int i = 0;
void loop() {
if (i==0)
xbeewake();
else if (i==5)
xbeesleep();
Serial.println(i);
i = (i+1) % 10;
delay(2000);
}


I have connected a wire from the digital port 6 to the DTR port on the Arduino, which I believe is connected directly to the DTR/SLEEP_RQ pin on the XBee. According to the manual, when sleeping the XBee should ignore all input via the serial connection. But it still transmits in the periods where it is supposed to sleep. Here is the output from the console monitor:

wake
0
1
2
3
4
sleep
5
6
7
8
9
wake
0


Any idea what is wrong with my setup? Or just advice how to make the XBee sleep?

• In your xbeesleep() function, the XBEE_sleepPin pin should be set as output: pinMode(XBEE_sleepPin,OUTPUT); As it is in the xbeewake() function. – m.Alin Jun 6 '12 at 20:29
• also, you're writing LOW both places. One of them should be high. – darron Jun 7 '12 at 2:55
• I think you are right. I'm doing some experiments, and I will come back. – Lucy Brennan Jun 7 '12 at 9:32
• @darron Good catch. In the xbeesleep() function, the XBEE_sleepPin should be set HIGH. – m.Alin Jun 7 '12 at 10:42
• I have problems with setting the correct sleep mode register, SM (see here). With your corrections and in the correct sleep mode, I would expect it to work. – Lucy Brennan Jun 7 '12 at 20:16

First of all you have to configure your XBee with atsm = 5 and atdi7 = 0. then is better to use one 10k resistor between pin 6 and dtr/sleep_rq and finally when you want the XBee to sleep do not let the program to print the numbers from 5-10.

#include <avr/sleep.h>
#define XBEE_sleepPin 6

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

void xbeesleep() {
Serial.println("sleep");
delay (3000);
pinMode (XBEE_sleepPin,OUTPUT);    // put XBee to sleep0
digitalWrite(XBEE_sleepPin,HIGH);  // Setting this pin to LOW turns off the pull up resistor, thus saving precious current
}

void xbeewake() {
Serial.println("wake");
pinMode(XBEE_sleepPin,OUTPUT);   // Set the "wake-up pin" to output
digitalWrite(XBEE_sleepPin,LOW); // wake-up XBee
delay(1000); //make sure that XBee is ready
}

int i = 0;
void loop() {
if (i==0)
{
xbeewake();
}
else if (i==5)
{
xbeesleep();
}
if (i<5)
{
Serial.println(i);
}
i = (i+1) % 10;
delay(3000);
}


I have executed the following code on my Arduino Uno

#include <avr/sleep.h>
#define XBEE_sleepPin 5

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

void xbeesleep() {
Serial.println("sleep");
delay (3000);
pinMode (XBEE_sleepPin,OUTPUT);    // put XBee to sleep0
digitalWrite(XBEE_sleepPin,HIGH);  // Setting this pin to LOW turns off the
pull up resistor, thus saving precious current
}

void xbeewake() {
Serial.println("wake");
pinMode(XBEE_sleepPin,OUTPUT);   // Set the "wake-up pin" to output
digitalWrite(XBEE_sleepPin,LOW); // wake-up XBee
delay(1000); //make sure that XBee is ready
}

int i = 0;
void loop() {
if (i==0)
{
xbeewake();
}
else if (i==5)
{
xbeesleep();
}
if (i<5)
{
Serial.println(i);
}
i = (i+1) % 10;
delay(3000);
}


And It behaves as expected. Note that if you are using Xbee Shield then you would have to change settings on your module. All you have to do is open Xctu software and change the option D7 to Do Low[4]. The reason why you have to change this option is that when xbee goes to sleep it turns CTS pin high, which in turn resets your arduino therefore your code is not being executed.

Ticking the upload the latest firmware checkbox in x-ctu usual fixes the problem but you should ensure that all your xbees use the same firmware version.