Example Code for Arduino-Xbee LED Control
Last revision 2026/01/12
This guide provides example code and instructions for using Arduino and Xbee to control an LED. It includes software preparation, wiring diagrams, setup details, and sample code to turn the LED on and off using Serial1 communication.
Software Preparation
This board is only supported in the latest Arduino IDE 1.0.1 or later version so you will also need to update the IDE.
Latest Arduino IDE
Wiring Diagram


Other Preparation Work
Xbee Serial1 port baud rate should be set the same to the xbee baud rate
Sample Code
// # Description:
// It is just used to test Leonardo module
// Write by Grey
// Input "p" once time the led will be turn on;twice to turn off.
// It is controled by Serial1 Xbee side only. but you can check the status from serial port.
int ledPin=13;
int val;
int count=0;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
Serial1.begin(115200); // Xbee Serial1 port baud rate should be set the same to the xbee baud rate
}
void loop()
{
val=Serial1.read(); // read xbee Serial1 data
if(-1!=val) // "-1" means no data
{
if('p'==val){
if(count==0)count=1;
else if(count==1)count=0;
}
if(count==0){
digitalWrite(ledPin,HIGH); // Turn on
Serial.println("LED ON"); // you can check the status from serial port.
Serial1.println("LED ON"); //you can check the status from serial1 port(xbee).
}
else if(count==1)
{
digitalWrite(ledPin,LOW); // Turn off
Serial.println("LED OFF"); //you can check the status from serial port.
Serial1.println("LED OFF"); //you can check the status from serial1 port(xbee).
}
}
}
Result
Input "p" once time the led will be turn on;twice to turn off. It is controled by Serial1 Xbee side only. but you can check the status from serial port.
Additional Information
DFRobot Leonardo with Xbee R3 has a Xbee socket.It uses Serial1 to communicate with Leonardo.
Was this article helpful?
