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). Let’s take that a step further and learn how to establish Zigbee communication using Arduino and NodeMCU boards.
This article was originally published in IoTDesignPro along with co-author Lavanya.R
SuppliesHardware Requirements for Establishing Zigbee Communication (Affiliate links are provided)
- 1 x Arduino Nano: https://amzn.to/3gKuoMW
- 1 x NodeMCU : https://amzn.to/3mI64PJ
- 2 x XBee Pro S2C modules: https://amzn.to/3Dud1d2
- 1 x XBee explorer board (to program XBee) : https://amzn.to/3kzivdV
- LEDs: https://amzn.to/3zrbWQN
- Push Button: https://amzn.to/3mPEobw
It is important to clarify at the beginning that XBee is a module, a product, which supports many wireless communication protocols like ZigBee, 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.
Step 2: Making Xbee compatible with BreadboardTo reduce the cost and to make XBee compatible with a breadboard, solder 4 wires from XBee module pins to header pins and use that to connect to the breadboard as shown in the figure above. [First 3 pins and the last pin of the left side of the board]
- Black wire -> Ground
- Red Wire -> VCC
- White Wire -> Tx
- Yellow Wire -> Rx
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.
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.
Step 4: 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. We used NodeMCU for showing the compatibility of XBee with different microcontrollers. Also, NodeMCU can be used to send data to the internet in case if we transmit sensor data. The complete circuit diagram to interface XBee with NodeMCU is as shown below.
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.
Need to Develop This Project Into a PCB?Getting a electronics project into production would be nightmare. To ease you into the production world we have developed a platform (PCB CUPID) for PCB enthusiasts and hobbyists to ask and answer questions related to PCB design, fabrication, and assembly.
In addition to the Q&A feature, this website also has a wealth of blog posts and useful resources to help you learn about developing and manufacturing printed circuit boards. Whether you're a beginner looking for a crash course on PCB basics, or an experienced designer looking for tips and tricks, you'll find something of value on the site
So head on over and check it out, and don't forget to participate in the Q&A community to get help and share your own knowledge. Thanks!
Step 5: Downloading and Installing XCTU SoftwareBasically, the XBee module can be configured as Coordinator, Router, or End device.
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 connect your XBee using a USB to serial converter or an explorer board. Check the COM port of the Xbee in the device manager.
Step 6: Installing Firmware to XBee ModulesFirmware 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”.
Step 2: Select the COM port to which the XBee module is connected and click on “Next”.
Step 3: Keep the default settings and click on “Finish”.
Step 4: Now, on the pop-up window, click on “Add Selected Devices”.
Step 5: Now, the XBee module will appear on the left side of the window. Click on it to update the 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.
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.
Step 8: Give any 4 digits for the Destination Address. This same number must be entered as Source Address for the other XBee 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.
Step 10: Set one device as “Coordinate [1]” and the other device as “End Device [0]”.
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”).
Step 7: Note and COM Port UsedNote: 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).
Step 8: Complete Code for Both Receiver Side and Transmitter SideCODE: Receiver side
#include<SoftwareSerial.h>
int led = 2;
int received = 0;
int i;
//For communicating with zigbee
SoftwareSerial zigbee(13,12);
void setup()
{
Serial.begin(9600);
zigbee.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
//check if the data is received
if (zigbee.available() > 0)
{
received = zigbee.read();
//if the data is 0, turn off the LED
if (received == '0')
{
Serial.println("Turning off LED");
digitalWrite(led, LOW);
}
//if the data is 1, turn on the LED
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; //this variable keeps track of alternative click of the button
void setup()
{
Serial.begin(9600);<br>
pinMode(BUTTON, INPUT_PULLUP);<br>
XBee.begin(9600);
}
void loop()
{
//When button is pressed (GPIO pulled low) send 1
if (digitalRead(BUTTON) == LOW && toggle)
{
Serial.println("Turn on LED");
toggle = false;
XBee.write('1');
delay(1000);
}
//When button is pressed second time (GPIO pulled low) send 0
else if (digitalRead(BUTTON) == LOW && !toggle)
{
Serial.println("Turn off LED");
toggle = true;
XBee.write('0');
delay(1000);
}
}
ConclusionThis tutorial is aimed at giving proper instructions on establishing your first Zigbee communication using just two XBee modules and different microcontrollers boards.
Comments