I want to send data to atmega 2560 from my laptop using two Xbee S2C.

  • The first Xbee , let's label it A , is to be interfaced with the laptop.
  • The second Xbee , let's label it B , is to be interfaced with the atmega 2560.

I want to know how to configure these Xbees to communicate with each other. I am a beginner , I know only few things :

  • The Xbee A will be interfaced with the laptop using Xbee usb adapter , which will facilitate the UART communication between Laptop and Xbee A.
  • The Xbee B will be interfaced with the Atmega2560 by providing Xbee B with power and connecting Tx and Rx of the micro controller to that of Xbee B.These two will then communicate via UART.

I am also not clear that the controller's Tx and Rx pin will connect to Xbee B's Tx and Rx pin respectively or the other way around. Please guide.

Here's the code I am burning to Atmega 2560 , initially for turning a led on/off on PB6

#define F_CPU 14745600

unsigned char data; //to store received data from UDR1

//Function to initialize ports
void port_init()
DDRB = 0x40;


//Function To Initialize UART0
// desired baud rate:9600
// actual baud rate:9600 (error 0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
// UBRR0L = 0x47; //11059200 Hz
UBRR0L = 0x5F; // 14745600 Hzset baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;

ISR(_VECTOR(26))        // ISR for receive complete interrupt
data = UDR0;                //making copy of data from UDR0 in 'data'  variable 

UDR0 = data;                //echo data back to PC

if(data == 0x37)
    PORTB = 0x00;
if(data == 0x39)
    PORTB = 0x40;           


//Function To Initialize all The Devices
void init_devices()
cli(); //Clears the global interrupts
port_init();  //Initializes all the ports
uart0_init(); //Initailize UART0 for serial communiaction
sei();   //Enables the global interrupts

 //Main Function
 int main(void)

enter image description here


1 Answer 1


There are good instructions here for set up of the modules:


The way the UART is set up is clearly outlined in the datasheet here:


You should note that you might want CTS and RTS as well as Tx and Rx. You can use with or without, but you have to really take care if you do not implement it. If you are sending low amounts of data and not very often you'll probably get away without it.

Din on the module is connected to Tx of the micro's UART and the Tx and the PC's UART. However, please note that the UART of the PC will be at RS232 levels and you will need to use a level converter to get them to 3V3.

Similarly, Dout will go to the Rx pins of UARTs at each end, with you needing level conversion at the PC end.

You should read up on RTS and CTS and how they are used. Some micros have UARTs with dedicated flow control, others you have to implement in software. The Mega2560 does not have hardware flow control, so you'll have to implement in software. This is a nice little document:


Once again, at the PC end, you'll need level converters.

I think this should get you going once you have done some more reading.

  • \$\begingroup\$ I configured the Xbees and verified them by connecting them to PC via X-CTU. Both were successfully sending and recieving data. But when I burnt the code to turn on/off led it didn't work. Is the level conversion at the PC end not taken care by usb interface , whose photo I have attached above(edit)? If not , how I can do that? \$\endgroup\$ Aug 15, 2017 at 11:26
  • \$\begingroup\$ Ah, but that's cheating, that's on a carrier board which you didn't say before. ;) As far as the LED is concerned I have no idea. Need way more info on exactly what you have done and what does and doesn't work. \$\endgroup\$
    – DiBosco
    Aug 15, 2017 at 11:42
  • \$\begingroup\$ I am sending data via X-CTU to the microcontroller. On sending 7 the led will turn off and on sending 9 it will turn on. The code for which I have attached. Separately , While connecting both the xbees to PC as coordinator and router , they are communicating properly. Then I burny the led on/off code on Atmega 2560 and connect Xbee B to microcontroller , and Xbee A to PC . Opened X-CTU terminal window , and sent 7 or 9 , but led didn't turned on. However , tx and rx of both Xbees were blinking when I was sending data. \$\endgroup\$ Aug 15, 2017 at 11:51
  • \$\begingroup\$ OK, so I'm assuming you have one of these carrier boards connected to a micro on a dev board? When you set a break point in the interrupt for receiving data, how does it step through that code? Also, does it echo that value back to the PC? \$\endgroup\$
    – DiBosco
    Aug 15, 2017 at 12:35
  • \$\begingroup\$ I have two carrier boards. One for interfacing with PC and the other for interfacing with micro . ISR vector 26 is triggered when there is unread data in the receive buffer. It's not echoing back to PC. \$\endgroup\$ Aug 15, 2017 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.