Neutrino
Published © CC BY

Xbee/Zigbee Setup with Arduino and NodeMCU

Basics on how to set up Zigbee using XCTU.

BeginnerProtip2 hours42,331
Xbee/Zigbee Setup with Arduino and NodeMCU

Things used in this project

Story

Read more

Schematics

Transmitter

transmitter circuit diagram

Receiver

receiver circuit diagram

Code

Transmitter code

Arduino
Upload this code to Arduino nano
#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);
    }
}

Receiver Code

Arduino
use this code for NodeMCU
#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);
        }
    }
}

Credits

Neutrino

Neutrino

14 projects • 39 followers
Electrical Engineer and a programming hobbyist. I love to build exciting stuff!
Thanks to Lavanya R .

Comments