Gustavo Reynaga
Published © GPL3+

Arduino 101 & App Inventor BLE RGB Lamp

Create your own RGB controlled lamp with Arduino 101 & App Inventor using BLE.

BeginnerFull instructions provided1 hour4,091
Arduino 101 & App Inventor BLE RGB Lamp

Things used in this project

Hardware components

Arduino 101
Arduino 101
×1
Breadboard (generic)
Breadboard (generic)
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×3
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
MIT App Inventor
MIT App Inventor

Story

Read more

Schematics

Fritzing Squematic

Code

Arduino code

Arduino
Arduino code to make a BLE control a RGB Led
/*
  
  This code was modify by Gustavo Reynaga @gsreynaga and base on the job by:
  Andrew McKinney (mckinney@mit.edu)  
  A Simple example that alows you to turn on/off a light connected to a digital relay (switch)  using a Arduino 101 microcontroller.

  

*/
#include <CurieBLE.h>

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService RGBService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedIntCharacteristic switchCharacteristicRed("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic switchCharacteristicBlue("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic switchCharacteristicGreen("19B10013-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);


const int RedPin = 3; // pin to use for the Light
const int BluePin = 5; // pin to use for the Light
const int GreenPin = 6; // pin to use for the Light


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

  // set Color pin to output mode
  
  pinMode(RedPin, OUTPUT);
  pinMode(BluePin, OUTPUT);
  pinMode(GreenPin, OUTPUT);
  //This configuration its for Cathode Led if you use a Anode, need to make a change to LOW
  digitalWrite(3,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("RGB Service");
  blePeripheral.setAdvertisedServiceUuid(RGBService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(RGBService);
  //blePeripheral.addAttribute(switchCharacteristic);
  blePeripheral.addAttribute(switchCharacteristicRed);
  blePeripheral.addAttribute(switchCharacteristicBlue);
  blePeripheral.addAttribute(switchCharacteristicGreen);

  // set the initial value for the characeristic:
  //switchCharacteristic.setValue(0);
  //use 0 (Zero) invalue for ANODE Led
  switchCharacteristicRed.setValue(255); 
  switchCharacteristicBlue.setValue(255);
  switchCharacteristicGreen.setValue(255);

  // begin advertising BLE Light service:
  blePeripheral.begin();

  Serial.println("BLE RGB service.");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the color:
      if (switchCharacteristicRed.written()) {
          Serial.println(switchCharacteristicRed.value());
          int light = switchCharacteristicRed.value();
          analogWrite(RedPin,light);
          Serial.println(light);
      }
      else
      if (switchCharacteristicBlue.written()) {
          Serial.println(switchCharacteristicBlue.value());
          int light = switchCharacteristicBlue.value();
          analogWrite(BluePin,light);
          Serial.println(light);
      }
      else
      if (switchCharacteristicGreen.written()) {
          Serial.println(switchCharacteristicGreen.value());
          int light = switchCharacteristicGreen.value();
          analogWrite(GreenPin,light);
          Serial.println(light);
      }
      
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

App Inventor 2 project Code

Arduino
AiA Project file for App Inventor
No preview (download only).

Arduino INO

Arduino
/*
  
  This code was modify by Gustavo Reynaga @gsreynaga and base on the job by:
  Andrew McKinney (mckinney@mit.edu)  
  A Simple example that alows you to turn on/off a light connected to a digital relay (switch)  using a Arduino 101 microcontroller.

  

*/
#include <CurieBLE.h>

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService RGBService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedIntCharacteristic switchCharacteristicRed("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic switchCharacteristicBlue("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic switchCharacteristicGreen("19B10013-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);


const int RedPin = 3; // pin to use for the Light
const int BluePin = 5; // pin to use for the Light
const int GreenPin = 6; // pin to use for the Light


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

  // set Color pin to output mode
  
  pinMode(RedPin, OUTPUT);
  pinMode(BluePin, OUTPUT);
  pinMode(GreenPin, OUTPUT);
  //The configuration maybe to change its the led are Anode or Cathode
  digitalWrite(3,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("RGB Service");
  blePeripheral.setAdvertisedServiceUuid(RGBService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(RGBService);
  //blePeripheral.addAttribute(switchCharacteristic);
  blePeripheral.addAttribute(switchCharacteristicRed);
  blePeripheral.addAttribute(switchCharacteristicBlue);
  blePeripheral.addAttribute(switchCharacteristicGreen);

  // set the initial value for the characeristic:
  //switchCharacteristic.setValue(0);
   //The configuration maybe to change its the led are Anode or Cathode
  switchCharacteristicRed.setValue(255); 
  switchCharacteristicBlue.setValue(255);
  switchCharacteristicGreen.setValue(255);

  // begin advertising BLE Light service:
  blePeripheral.begin();

  Serial.println("BLE RGB service.");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the color:
      if (switchCharacteristicRed.written()) {
          Serial.println(switchCharacteristicRed.value());
          int light = switchCharacteristicRed.value();
          analogWrite(RedPin,light);
          Serial.println(light);
      }
      else
      if (switchCharacteristicBlue.written()) {
          Serial.println(switchCharacteristicBlue.value());
          int light = switchCharacteristicBlue.value();
          analogWrite(BluePin,light);
          Serial.println(light);
      }
      else
      if (switchCharacteristicGreen.written()) {
          Serial.println(switchCharacteristicGreen.value());
          int light = switchCharacteristicGreen.value();
          analogWrite(GreenPin,light);
          Serial.println(light);
      }
      
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

Credits

Gustavo Reynaga

Gustavo Reynaga

12 projects • 88 followers
Iam a teacher

Comments