Electronic – arduino – How to make XBee sleep

arduinosleepxbee

Arduino Fio with XBee radio.

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?

Best Answer

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.

This is your code updated:

#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);
}