Project tutorial
Arduino 101 BLE RGB Lamp

Arduino 101 BLE RGB Lamp © GPL3+

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

  • 2,312 views
  • 2 comments
  • 10 respects

Components and supplies

Apps and online services

About this project

Overview

Looking to make an attractive and useful project to use the hardware sponsored to Hackster Live, I decided for a Pretty RGB controlled Lamp with Arduino 101 and BLE, using an interface in App Inventor 2.

I hope you like it.

The Project

In this project, the code is modified code from Andrew McKinney (mckinney@mit.edu), which allows manipulating a Led using PWM under the BLE protocol (which is interesting).

The modifications are very simple, just add the extra features to the BLE service, as well as the missing pins for an RGB LED.

PWM Controls LEDs

Remember that the colors in an RGB Led, change when modifying the Pulse Width Modulation (or PWM) is a technique for controlling power. We also use it to control the brightness of each of the LEDs.

You can check out this excellent Adafruit article: https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/theory-pwm.

The Circuit

Next make the circuit:

The Code

Now upload the program to Arduino.

Testing with the nRF Connect application.

Yes, I know that this App is very tedious to use, so I put to work to create a simple aplicicon in App Inventor 2 much more friendly to operate the lamp.

And that's it, I hope you enjoy it as much as I did, I added the source code and the schematic.

Until next time.

Regards.

Extra pics

Code

Arduino codeArduino
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 CodeArduino
AiA Project file for App Inventor
No preview (download only).
Arduino INOArduino
/*
  
  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());
  }
}

Schematics

Fritzing Squematic
rgb_ble_lamp_hVrjjjL8Gc.fzz

Comments

Similar projects you might like

Arduino 101 Home BLE System

Project tutorial by Alexis Santiago Allende

  • 2,424 views
  • 0 comments
  • 11 respects

Arduino 101 BLE App

Project in progress by Alexis Santiago Allende

  • 10,794 views
  • 26 comments
  • 51 respects

Arduino/Genuino 101 BLE Thermometer With TMP102 and Blynk

Project tutorial by Konstantin Dimitrov

  • 7,876 views
  • 1 comment
  • 26 respects

Control RGB LED by Dragging – Arduino 101 & App Inventor

Project tutorial by DFRobot and CAVEDU Education

  • 4,437 views
  • 0 comments
  • 12 respects

SMS alerts for arduino 101 BLE

Project tutorial by Alexis Santiago Allende

  • 1,907 views
  • 0 comments
  • 7 respects
Add projectSign up / Login