I recently changed home and I wanted to create something different in my new work desk. Months ago I created ambilight for my TV with some meters of APA102 led and ambilight software and I have 2 meter of led unused yet!! 😀
Components used:
- APA102 RGB led strips
- Arduino Nano
- HM10 (this bluetooth is compatible with iOS bluetooth)
- Power supply 5V 50W
My idea is to control light of the desk with a custom iOS app that communicate with the Ardunino/HM10 through bluetooth technology.
APA102
Let’s start to see how APA102 RGB works. Here we have the official datasheet.
As we can see this leds works with 5V of power, and the data (in serial) pass from one led to the consecutive one through DI (Data Input) -> DO (Data Output) with a CI/CO (Clock Input/Output).
The frame is composed by 4 byte. The first byte is splitted in two parts: one with 3 constant bit (111) and other with 5 bit that indicate the brightness of the light (APA102 has 32 brightness level -> 2^5 = 32 ) .
The other two byte are for RGB section, the value of each color between 0-255 (2^8 = 256).
 Arduno Nano – APA102 Connection
D11 (Arduino)Â Â ->Â Â Â DI
D12 (Arduino)Â Â ->Â Â CI
VCC (Led)Â Â ->Â Â Power Supply 5V
5V (Arduino)Â Â ->Â Â Power Supply 5v
GND (Led)Â Â -> Power Supply GND
GND (Arduino)Â Â -> Power Supply GND
HM10 Setup
First step is configure the HM10 module.
You need to connect HM10 with an FTDI and send AT commands through Arduino software (or another serial capable program).
List of most used commands:
AT+NAME? Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return the name of the device
AT+NAMEnew-name             set new name to device
AT+ADDR? Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â get the address of the module
AT+PASS? Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â get the password (default password: 0000)
AT+PASSpassword               set new password
AT+RENEW Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â restore the default factory settings
AT+RESET Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â reset the moduleÂ
Arduino Nano – HM10 Connection
I reccomend using 3.3V, the module support until 6V, but with 3.3V it works great 🙂
Arduino sketch
I used APA102 and FastGPIO library control the led.
This is my arduino sketch 🙂
Github
/* * Created by Luca Soldi on 12/09/18. * Copyright © 2018 Luca Soldi. All rights reserved. */ /* By default, the APA102 library uses pinMode and digitalWrite * to write to the LEDs, which works on all Arduino-compatible * boards but might be slow. If you have a board supported by * the FastGPIO library and want faster LED updates, then install * the FastGPIO library and uncomment the next two lines: */ #include <FastGPIO.h> #define APA102_USE_FAST_GPIO #include <APA102.h> #include <SoftwareSerial.h> #define MAX_BUFFER 10 // Define which pins to use. const uint8_t dataPin = 11; const uint8_t clockPin = 12; // Create an object for writing to the LED strip. APA102<dataPin, clockPin> ledStrip; // Set the number of LEDs to control. const uint16_t ledCount = 84; // Create a buffer for holding the colors (3 bytes per color). rgb_color colors[ledCount]; SoftwareSerial ble(2, 3); // RX, TX rgb_color color; uint8_t brightness; int idx_buffer; uint8_t buffer[MAX_BUFFER]; void setup() { // Open serial port Serial.begin(9600); // begin bluetooth serial port communication ble.begin(9600); } void loop() { if (ble.available()) { buffer[idx_buffer] = ble.read(); if (idx_buffer > 4) { if (buffer[idx_buffer] == 170 && (buffer[idx_buffer-5] == 85)) { // possibly end packet //correct packet color.red = buffer[idx_buffer-4]; color.green = buffer[idx_buffer-3]; color.blue = buffer[idx_buffer-2]; brightness = buffer[idx_buffer-1]; //clean buffer memset(buffer, 0, sizeof(buffer)); idx_buffer = 0; for(uint16_t i = 0; i < ledCount; i++) { colors[i].red = color.red; colors[i].green = color.green; colors[i].blue = color.blue; } ledStrip.write(colors, ledCount, brightness); } } idx_buffer++; if (idx_buffer == MAX_BUFFER) { memset(buffer, 0, sizeof(buffer)); idx_buffer = 0; } } }
To avoid the problem of inconsistent of data I added a marker character at the beginning and at the end of the packet (10101010 and 01010101) that validate the packet and then send correct color to the led strip.
When the packet is received the sketch send in a loop the color RGB and the brightness to the single led.
Here we have an example of packet:
170 Â R[0-255] Â G[0-255] Â B[0-255] Â BRIGHTNESS[0-31] Â 85
iOS Application
The app first looking for BLE devices, and add them in an array if it finds that is enabled the service FFE0 and the characteristic FFE1, defined as custom service in HM10 module. Other devices are rejects.
The communication is not fast enough to send data in real time when user change color in picker, so I decided to send serial data each 200ms.
You can see all my implementation in github projects 🙂
Hi, which IOS app did you use for this proyect?
Hi Carlos, I created my own SWIFT app. 🙂
https://github.com/LucaSoldi/LSLedController