Luca Soldi

  • Home
  • Blog
  • Projects
  • Github
  • About me

My Desk Led Light (APA102 + Arduino Nano + HM10)

    Home Arduino My Desk Led Light (APA102 + Arduino Nano + HM10)
    NextPrevious

    My Desk Led Light (APA102 + Arduino Nano + HM10)

    By admin | Arduino, Bluetooth, iOS | 2 comments | 15 September, 2018 | 0

    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

    Github

     

    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 🙂

     

    Video

    apa102, arduino, ble, bluetooth, hm10, ios, led

    admin

    More posts by admin

    Related Post

    • REST API Best Security Practices [PRANK]

      By admin | 0 comment

      I love to see how internet is a secure place nowadays 😀 Apps and WebApps everywhere that use very reliable REST APIs. This time I found a perfect example to follow if you want toRead more

    • Download ItaloLive media content

      By admin | 0 comment

      Boring long trip on an Italian high-speed train? It’s time to update your home media library 😀 Connecting to the train Wi-Fi you can access to an internal nginx server that serves see some mediaRead more

    • How to get duration of Wav file in React Native

      By admin | 0 comment

      I’m writing an audio app in React Native that plays WAV file from local storage. I was looking for a simple and little module to get the duration of WAV files, but I didn’t findRead more

    • How to get artworks for your music library

      By admin | 0 comment

      I’m working on a script to automate tagging and ordering my music library. I love artworks so I had to find a way to take them from the net. I found 2 ways to getRead more

    • [POC] How to travel for free with Italian public transports

      By admin | 0 comment

      Hey remember! It’s a proof of concept 🙂 Before publishing this article I verified that the company not use this technology anymore. Some years ago I did some experiments with my rechargeable card used inRead more

    • Unbrick Proxmark3 with a Raspberry Pi and OpenOCD

      By admin | 4 comments

      Months ago I bricked my Proxmark3 while I was experimenting with the creation of a new antenna for a tag. The leds blinked in a strange way at boot and my mac not recognized anymore theRead more

    2 comments

    • Carlos Ornelas Reply 3 October 2019 at 1:19

      Hi, which IOS app did you use for this proyect?

      • admin Reply 20 November 2019 at 13:28

        Hi Carlos, I created my own SWIFT app. 🙂
        https://github.com/LucaSoldi/LSLedController

    Leave a Comment

    Cancel reply

    Your email address will not be published. Required fields are marked *

    NextPrevious

    Categories

    • Android
    • Arduino
    • Bluetooth
    • iOS
    • Random Stuff
    • Raspberry Pi
    • RFID
    • Scraping

    Archives

    • December 2020
    • June 2019
    • January 2019
    • October 2018
    • September 2018
    • May 2017
    • January 2017
    Copyright 2017 Luca Soldi | All Rights Reserved
    • About me
    • Blog
    • Home
    • Projects

    Luca Soldi