Setting up Zigbee Communication to Transfer Data Between Arduino and NodeMCU using XBee Modules

Zigbee communication between Arduino and NodeMCU using XBee Modules

Zigbee is a popular wireless communication protocol used to transfer a small amount of data with very low power. It is widely used in applications where data has to be shared among many nodes within personal space, and with the advent of the Internet of things (IoT), the application for Zigbee is fast growing. It is used in home automation, asset tracking, remote data collection, and much more. If you are already familiar with the architecture of Zigbee and Zigbee protocols, then you already know that wireless communication between two XBee modules can be done without any additional microcontrollers, but in practical application, these modules will always be interfaced with a microcontroller to read and send data. Let’s take that a step further and learn how to establish Zigbee communication using Arduino and NodeMCU boards.

 

Difference between Zigbee and XBee

It is important to clarify at the beginning itself that XBee is a module, a product, which supports many wireless communication protocols like ZigBee, Wi-Fi (Wi-Fly module), 802.15.4, 868 MHz modules, etc. Whereas, Zigbee is a standard protocol used to establish wireless networking (ZigBee communication). Often people use these two terms interchangeably but it shouldn’t be done so. To establish Zigbee communication, we need a receiver (endpoint). For this, an XBee module connected with Arduino/NodeMCU will do, and this will communicate wirelessly with another XBee module (coordinator) that sends data, will be connected to another Arduino board.

 

Hardware Requirements for Establishing Zigbee Communication

  • 1 x Arduino Nano
  • 1 x NodeMCU
  • 2 x  XBee Pro S2C modules
  • 1 x XBee explorer board (to program XBee)
  • USB cables
  • LED (along with a 220-ohm resistor)
  • Push Button

 

Configuring XBee Modules using XCTU Software

Basically, the XBee module can be configured as Coordinator, Router, or End device. To make it work as we desire, first, we have to configure them using XCTU software. You can download and install the XCTU software using the given link.

 

Use USB to serial converter or an explorer board to connect the XBee module with a PC or Laptop. Connect the XBee module to the Explorer board and plugin using a USB cable. We will get into the details on how exactly we can use this software and configure the module, but before we do that, let's get our hardware ready.

 

Interfacing XBee with Arduino (Transmitting Side)

For this side of the connection, we’ve used Arduino Nano. Arduino Uno or NodeMCU boards could also be used. The complete circuit diagram to interface XBee with Arduino nano is shown below.

Interfacing XBee with Arduino

In the above Arduino XBee circuit diagram, we have used a push-button which, when pressed, will transmit data. For this, connect one end of the button to D5 of Arduino and the other end of the button to GND.

 

Connect VCC (pin 1) of XBee module to 3.3V of Arduino Nano and GND (pin 10) of XBee to GND of Arduino Nano. These two connections make up for powering the transmitting side XBee module. Connect Dout (pin 2) to D2 of Arduino Nano and Din (pin 3) to D3 of Arduino Nano. I used a breadboard to build this circuit and my hardware set-up with all the connections is shown below.

Arduino with XBEE

 

Interfacing XBee with NodeMCU (Receiving side)

For this, we’ve used NodeMCU, but it has certain constraints. You can also use Arduino Nano or Arduino Uno for the purpose. The idea of using an Arduino to transmit data and NodeMCU to receive data is that it will have a more practical application like the Arduino can collect some sensor parameters and send it to the NodeMCU via ZigBee, then NodeMCU can process this data and share it or take any action via the Internet as required. The complete circuit diagram to interface XBee with NodeMCU is shown below.

ZigBee Communication Receiver Circuit

Connect Gnd(pin10) of XBee to GND of NodeMCU and Connect Vcc (Pin1) of XBee to 3.3V of NodeMCU. The above-mentioned two connections supply the power to the XBee module as well. Then connect Dout (pin2) of XBee to D6 pin of NodeMCU and Din (pin3) of XBee to D7 pin of NodeMCU for receiving data.

 

As an indication of whether the data is received or not, we’ve used LED. For this, connect LED anode to D2 of NodeMCU and LED cathode to GND through a 220ohm resistor of NodeMCU. Again, I used a breadboard to build this circuit and my hardware set-up with all the connections is shown below.

NodeMCU with XBEE

 

Downloading and Installing XCTU Software

To set up, configure and test your XBee devices. You need XCTU software. It’s an easy-to-use, free, multi-platform application for RF XBee modules. Download the XCTU software here and it’ll guide you to install it as well. After that, open the application and make sure your XBee module is connected properly. Check the COM port of the explorer board in the device manager.

 

Installing Firmware to XBee Modules

Firmware should be installed in both the XBee modules first, for that we’re using the XBee development board.

 

Step 1: Open XCTU software and click on “Discover boards”.

XCTU Software

 

Step 2: Select the COM port to which the XBee module is connected and click on “Next”.

XBee Module COM Port

 

Step 3: Keep the default settings and click on “Finish”.

Xbee Module Port Parameters

 

Step 4: Now, on the pop-up window, click on “Add Selected Devices”.

Discovering Radio Modules

 

Step 5: Now, the XBee module will appear on the left side of the window. Click on it to update the user interface.

Updating User Interface

 

Step 6: To update the firmware, click on “Update”, select “802.15.4 TH” in the Function set and select the newest firmware in the Firmware version and click on update. After this, a pop-up window will appear. Click on YES.

Firmware Version Updating

 

Step 7: Enter any 4 digits as PAN ID. PAN ID is a personal area network (PANs) identifier. Each network must be given a unique ID. Make sure that for both the XBee modules, the same PAN ID is entered. This indicates that both the XBee modules are in the same network.

Personal Area Network ID

 

Step 8: Give any 4 digits for the Destination Address. This same number must be entered as Source Address for the other XBee module.

XBEE Module Destination Module

 

Step 9: Give any 4 digits for the Source Address. This same number must be entered as the Destination Address for the other XBee module.

XBEE Module Source Address

 

Step 10: Set one device as “Coordinate [1]” and the other device as “End Device [0]”.

Installing Firmware to XBEE Module

 

Step 11: Now, click on Write in the top bar. After that is successful, the Symbol changes from “E” to “C” (End Device to Coordinator, as we have set. For the other XBee module, it should be “E”).

XBEE Module Firmware Installing

Note: As mentioned in the steps, this must be carried out for both the XBee modules. The only difference between both will be that the Source and Destination Addresses are opposite for the End Device and Coordinator and one must be selected as the End Device and the other as “Coordinator”. In the “Coordinator Enable” drop-down list, make sure both the XBee modules are given the same PAN ID.

 

COM 5 is for nodeMCU (receiving side) and COM 14 is for Arduino nano (Sending side).

 

Destination Address

Source Address

PAN ID

XBee module 1 (Coordinator)

9999

8888

9898

XBee module 2 (End Device)

8888

9999

9898

 

Soldering XBee Pins to Arduino and NodeMCU

The pins of the XBee module are not compatible with breadboards. Hence, solder 4 wires from XBee module pins to header pins and use that to connect to the breadboard as shown in the figure below. Alternatively, you can also use female to male header connector wires, if you want to avoid soldering works.

Shows header pins used to connect XBee module with Breadboard

In an overview, two XBee modules are used to establish ZigBee communication. An Arduino Nano and NodeMCU is used to communicate between both the XBee devices. The XBee modules are coded such that when a push-button is pressed on the Transmitter side, LED is lit on the Receiving side. This indicates that the data transmitted at the source is received properly at the destination.

 

ZigBee Communication Program for Arduino

The complete Arduino code for Zigbee communication can be found at the bottom of this page. The explanation of the code is as follows.

#include "SoftwareSerial.h"
SoftwareSerial XBee(2,3);

 

This part emulates serial communication in pin 2 (Rx) and pin 3 (Tx)

int BUTTON = 5;

 

Pin 5 is where the button is connected on Arduino Nano, this is needed for sending the information.

boolean toggle = false;

 

To track if the button is clicked or not, the statement is used. If the button is pressed once, it turns the LED ON, if it is pressed again, it turns the LED OFF. To keep track of this, the toggle is used.

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

 

The program starts from a void setup, and the baud rate for the serial monitor is set.

pinMode(BUTTON, INPUT_PULLUP);

 

Initializing the button pin to read input.

  XBee.begin(9600);
}

 

This part of the snippet uses software serial to begin the connection between XBee and Arduino. 9600 is the default communication speed for the XBee module.

void loop()
{
   if (digitalRead(BUTTON) == LOW && toggle)
   { 

 

This part is to check if the button is pressed. If it is pressed, then the pin reads LOW and makes a note of this. It also checks if the button is previously clicked.

Serial.println("Turn on LED");

 

This part is to print the message on the serial monitor

toggle = false;

 

Reset the previous click so that it can detect the click again. When the toggle is false, this puts the LED in an OFF state.

XBee.write('1');

 

Send the message to XBee

delay(1000);
}

 

A delay of 1 second is caused to wait for the message to be delivered

else if (digitalRead(BUTTON) == LOW && !toggle)
{

 

This bit checks if the button is clicked

Serial.println("Turn off LED");

 

Print the message on the serial monitor

toggle = true;

 

Set to the previous clicked state to turn OFF the LED

 XBee.write('0');

 

Send the message to XBee

delay(1000);  
   }
}

Create one-second delay to wait for the message to be delivered

 

ZigBee Communication Program for NodeMCU

The complete NodeMCU code for Zigbee communication can be found at the bottom of this page. The explanation of the code is as follows.

#include<SoftwareSerial.h>

The Receiver part of the code is almost the same as Arduino code

SoftwareSerial zigbee(13,12);

This part emulates serial communication in pin 13 (Rx) and pin 12 (Tx)

int led = 2;

Set LED as pin 2

int received = 0;

To store the received data from XBee

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

Initialize serial monitor and connect to XBee module with default baud rate i.e 9600

pinMode(led, OUTPUT);
}

Initialize the LED as output

void loop() {
  if (zigbee.available() > 0) {

Check if any data is available from XBee

received = zigbee.read();

Store the data in the received variable

if (received == '0'){

Check if the received data is 0

    Serial.println("Turning off LED");
    digitalWrite(led, LOW);
    }

Then print the message and turn OFF the LED

   else if (received == '1'){
    Serial.println("Turning on LED");
    digitalWrite(led, HIGH);
  } 
}
}

If the received data is 1, then print the message and turn ON the LED.

 

I hope you liked the tutorial, if you have any questions regarding this project, please comment down below. Also, check out the working demonstration of this project given below.

Code

CODE: Receiver side

#include<SoftwareSerial.h>

int led = 2;

int received = 0;

int i;

SoftwareSerial zigbee(13,12);

void setup() {

  Serial.begin(9600);

  zigbee.begin(9600);

  pinMode(led, OUTPUT);

}

void loop() {

  if (zigbee.available() > 0) {

  received = zigbee.read();

    if (received == '0'){

    Serial.println("Turning off LED");

    digitalWrite(led, LOW);

    }

     else if (received == '1'){

    Serial.println("Turning on LED");

    digitalWrite(led, HIGH);

  }

}

}

CODE: Transmitter side

#include "SoftwareSerial.h"

SoftwareSerial XBee(2,3);

int BUTTON = 5;

boolean toggle = false;

void setup()

{

  Serial.begin(9600);

  pinMode(BUTTON, INPUT_PULLUP);

  XBee.begin(9600);

}

void loop()

{

  if (digitalRead(BUTTON) == LOW && toggle)

  {

    Serial.println("Turn on LED");

    toggle = false;

    XBee.write('1');

    delay(1000);

  }

  else if (digitalRead(BUTTON) == LOW && !toggle)

  {

    Serial.println("Turn off LED");

    toggle = true;

    XBee.write('0');

    delay(1000); 

  }

}

Video

19 Comments

It's in fact very complicated in this active life to listen news on TV, thus I just use world wide web for that purpose, and obtain the latest information.|

how can I transfer code to the node MCU through the Arduino software? I open the arduino software => tools =>board but i can not find the suitable board for node MCU

I am using the IR sensor instead of the push button. Is it possible to transmit the sensor data to the receiver side. Is there is any code available?
Transmitter side- Arduino interface with the Zigbee and IR sensor.
Receiver side - Zigbee interface with nodemcu8266.

I have to convey my love for your generosity for people that require help on that field. Your personal dedication to passing the message all over became amazingly functional and has regularly empowered somebody just like me to realize their targets. Your entire interesting information implies a great deal a person like me and a whole lot more to my mates. Warm regards; from all of us.

I as well as my friends appeared to be checking out the nice points found on your site and before long got a horrible suspicion I had not expressed respect to the blog owner for those secrets. Those people are already certainly very interested to learn all of them and have in effect absolutely been having fun with them. Appreciation for indeed being considerably accommodating and for utilizing varieties of amazing themes most people are really desirous to be aware of. My very own sincere apologies for not expressing appreciation to you earlier.

There are certainly numerous details like that to take into consideration. That could be a nice level to deliver up. I offer the thoughts above as common inspiration but clearly there are questions like the one you bring up where an important thing can be working in sincere good faith. I don?t know if best practices have emerged round things like that, but I am sure that your job is clearly recognized as a good game. Both boys and girls really feel the impression of only a second抯 pleasure, for the rest of their lives.

I truly wanted to compose a message to express gratitude to you for the pleasant pointers you are giving out on this website. My long internet research has finally been honored with reasonable facts and techniques to exchange with my good friends. I would tell you that many of us readers are very much lucky to dwell in a wonderful network with many brilliant people with great solutions. I feel pretty fortunate to have discovered your entire web page and look forward to some more fabulous moments reading here. Thanks once again for everything.

Can I simply say what a relief to seek out someone who truly is aware of what theyre speaking about on the internet. You definitely know how to convey an issue to mild and make it important. Extra individuals need to learn this and understand this aspect of the story. I cant imagine youre no more common because you definitely have the gift.

I would like to express some thanks to this writer just for bailing me out of such a predicament. Because of surfing through the the web and seeing opinions which are not beneficial, I thought my entire life was well over. Being alive without the approaches to the issues you've sorted out through the review is a critical case, as well as those which might have in a negative way damaged my entire career if I hadn't discovered your blog. The talents and kindness in maneuvering a lot of stuff was excellent. I don't know what I would have done if I hadn't come across such a subject like this. I am able to now look forward to my future. Thanks so much for this impressive and result oriented guide. I will not be reluctant to suggest the sites to any person who would like guide about this issue.

My wife and i were very comfortable that Ervin could carry out his studies through the ideas he made out of the web page. It's not at all simplistic just to continually be making a gift of tips which often many people might have been making money from. And we figure out we need you to thank because of that. All of the illustrations you've made, the straightforward website navigation, the friendships you can make it easier to foster - it is everything exceptional, and it is leading our son and us imagine that this idea is brilliant, and that is quite important. Many thanks for all!

I have to show appreciation to this writer for bailing me out of this dilemma. After exploring through the world-wide-web and getting tricks which are not beneficial, I believed my life was gone. Living without the answers to the problems you've fixed through your good article content is a crucial case, and the kind which may have adversely affected my entire career if I had not discovered your web blog. Your primary mastery and kindness in playing with every part was crucial. I am not sure what I would've done if I had not discovered such a solution like this. I am able to now relish my future. Thanks very much for your skilled and result oriented help. I won't hesitate to refer your web blog to any individual who needs and wants care on this area.

I have to express my appreciation to you just for rescuing me from such a condition. Right after researching through the world-wide-web and meeting views which were not pleasant, I assumed my life was done. Existing without the presence of strategies to the problems you've sorted out by means of this report is a crucial case, and the kind which could have negatively damaged my career if I had not come across your web site. Your main ability and kindness in dealing with all the pieces was important. I'm not sure what I would have done if I hadn't come upon such a point like this. It's possible to at this time look ahead to my future. Thanks so much for your specialized and sensible help. I won't hesitate to endorse your blog post to anybody who ought to have guidance about this matter.

Thanks so much for providing individuals with such a spectacular opportunity to read from this website. It's always very superb and jam-packed with a great time for me personally and my office acquaintances to visit your blog particularly three times in a week to study the new guides you will have. And indeed, I am just at all times happy with all the gorgeous inspiring ideas served by you. Some 3 tips in this post are definitely the most beneficial we have had.

Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don抰 know why Unable to subscribe to it. Is there anybody getting an identical rss drawback? Anybody who is aware of kindly respond. Thnkx

Needed to send you one tiny observation just to give many thanks the moment again for your beautiful tactics you have featured here. It's really wonderfully generous of people like you in giving freely what exactly many of us could possibly have marketed for an e book in making some profit for themselves, certainly now that you could have done it if you ever wanted. The creative ideas as well worked to be the great way to be aware that most people have similar passion like my personal own to understand lots more with regard to this issue. I know there are a lot more fun times ahead for folks who discover your blog post.

I simply desired to thank you very much yet again. I do not know the things I would've created in the absence of the type of hints shared by you concerning that situation. It seemed to be a alarming crisis in my position, but encountering your expert way you processed it took me to cry over contentment. Now i'm thankful for the information and as well , believe you realize what a powerful job your are carrying out training others through the use of your web page. Most probably you haven't come across any of us.

I'm also commenting to make you be aware of of the helpful discovery my wife's daughter undergone reading your web page. She noticed a wide variety of details, which included how it is like to have an amazing helping mood to get folks without difficulty know selected problematic subject matter. You undoubtedly did more than visitors' expected results. Thank you for giving those beneficial, trustworthy, informative and even cool tips about this topic to Evelyn.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.