My Cart

How to Remotely Control an RGB LED Using XBee Connectivity Kit

Difficulty Level: Beginner -

1. Introduction

This tutorial allows the user to turn ON and OFF a RGB LED with different colors depending on data sent by XBee gateway.

Ingredients:

    - 1 x XBee Connectivity Kit:
    • 1x Platform
    • 2x XBee
    • 1x XBee Shield
    • 1x XBee gateway
    • 1x RGB LED
    • 3x 220Ω resistors
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the XBee Shield, consult the main tutorial.

Step 1: Connection

Connect the XBee shield to Arduino and then connect XBee module or you can connect it directly to Raspberry Pi connection bridge. Connect the LDR with a resistor in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the VCC supply (5V in Arduino and 3.3V in Raspberry Pi) and ground.

Connect pin 6, 10 and 11 to the RGB LED with a resistor in each pin and power it with VCC in the longest leg of the RGB LED.

On the other hand you must connect a gateway with a XBee module to a PC.

Step 2: The Code

Arduino:

Code:
/*  
 *  XBEE Module 
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos MartĂ­nez
 */
 
char buffer;

//define constants
#define RED 6
#define GREEN 10
#define BLUE 11

void setup() {
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);

}

void loop() {

  while(Serial.available()){
    buffer = Serial.read();
    if (buffer == 'R'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 1);
      Serial.println("RED");
    }
    else if (buffer == 'G'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 1);
      Serial.println("GREEN");
    }
    else if (buffer == 'B'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 0);
      Serial.println("BLUE");
    }
    else if (buffer == 'P'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 0);
      Serial.println("PINK");
    }
    else if (buffer == 'Y'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 1);
      Serial.println("YELLOW");
    }
    else if (buffer == 'C'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 0);
      Serial.println("CYAN");
    }
    else if (buffer == 'W'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 0);
      Serial.println("WHITE");
    }
    else if (buffer == 'O'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 1);
      Serial.println("OFF");
    }
    else{
      Serial.println("NOT A COLOR!");
    }
  }

}
        

Raspberry Pi:

Code:
/*  
 *  XBEE Module 
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos MartĂ­nez
 */
 
//Include arduPi library
#include "arduPi.h"

char buffer;

//define constants
#define RED 6
#define GREEN 10
#define BLUE 11

void setup() {
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  
  }

void loop() {

   while(Serial.available()){
    buffer = Serial.read();
    if (buffer == 'R'){
       digitalWrite(RED, 0);
       digitalWrite(GREEN, 1);
       digitalWrite(BLUE, 1);
       printf("RED\n");
    }else if (buffer == 'G'){
       digitalWrite(RED, 1);
       digitalWrite(GREEN, 0);
       digitalWrite(BLUE, 1);
       printf("GREEN\n");
    }else if (buffer == 'B'){
       digitalWrite(RED, 1);
       digitalWrite(GREEN, 1);
       digitalWrite(BLUE, 0);
       printf("BLUE\n");
    }else if (buffer == 'P'){
       digitalWrite(RED, 0);
       digitalWrite(GREEN, 1);
       digitalWrite(BLUE, 0);
       printf("PINK\n");
    }else if (buffer == 'Y'){
       digitalWrite(RED, 0);
       digitalWrite(GREEN, 0);
       digitalWrite(BLUE, 1);
       printf("YELLOW\n");
    }else if (buffer == 'C'){
       digitalWrite(RED, 1);
       digitalWrite(GREEN, 0);
       digitalWrite(BLUE, 0);
       printf("CYAN\n");
    }else if (buffer == 'W'){
       digitalWrite(RED, 0);
       digitalWrite(GREEN, 0);
       digitalWrite(BLUE, 0);
       printf("WHITE\n");
    }else if (buffer == 'O'){
       digitalWrite(RED, 1);
       digitalWrite(GREEN, 1);
       digitalWrite(BLUE, 1);
       printf("OFF\n");
    }
    else if (buffer !='\n'){
	   printf("NOT A COLOR!\n");
	}
  }

 
}

int main (){
    setup();
    while(1){
        loop();
    }
    return (0);
}


        

Intel Galileo:

Code:
/*  
 *  XBEE Module 
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */			
			
char buffer;

//define constants
#define RED 6
#define GREEN 10
#define BLUE 11

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);

}

void loop() {

  while(Serial1.available()){
    buffer = Serial1.read();
    if (buffer == 'R'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 1);
      Serial.println("RED");
    }
    else if (buffer == 'G'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 1);
      Serial.println("GREEN");
    }
    else if (buffer == 'B'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 0);
      Serial.println("BLUE");
    }
    else if (buffer == 'P'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 0);
      Serial.println("PINK");
    }
    else if (buffer == 'Y'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 1);
      Serial.println("YELLOW");
    }
    else if (buffer == 'C'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 0);
      Serial.println("CYAN");
    }
    else if (buffer == 'W'){
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 0);
      digitalWrite(BLUE, 0);
      Serial.println("WHITE");
    }
    else if (buffer == 'O'){
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 1);
      Serial.println("OFF");
    }
    else{
      Serial.println("NOT A COLOR!");
    }
  }

}