Introduction: How to Add Bluetooth Control to Your Lightning Cloud
We'll be adding Bluetooth Low Energy control to the Lightning Cloud we made earlier. You can use this method to add Bluetooth control to any Arduino project, just omit the last few steps.
Step 1: Gather Materials
1. Arduino. For this Instructable I'm using a Pro Trinket from Adafruit, but I would expect the code to work on almost any Arduino. My Pro Trinket has the LiIon Battery Backpack already soldered on, and a switch added.
2. Bluetooth module. I'm using a Bluefruit UART Friend from Adafruit. They're pretty small, and use the nRF51822 BLE chip from Nordic Semiconductors. Adafruit wrote their own firmware for them, so they're very easy to use.
3. Breadboard
4. Wires
5. Neopixel LED strand. I connected 4 of these Breadboard Friendly ones by soldering male-male jumper wires between them (optional, but useful for testing).
6. Cloud structure from previous Instructable (optional).
Step 2: Connect the Power and Ground From the Arduino to the BLE Module
You'll connect the BATT+ and GND lines from the Pro Trinket to the appropriate pins on the Bluefruit module.
Pro Trinket -----> Bluefruit
BAT+ ---------------> Vin
GND ----------------> GND
GND ----------------> CTS (CTS means "Clear To Send". If we connect this pin to ground, the Bluefruit module knows it is always ok to send).
Step 3: Connect TX and RX
Remember, the TX of one module connects to the RX of the other. One side receives what the other side transmits.
Pro Trinket -------> BLE
TX ------------------> RX
RX ------------------> TX
Step 4: Connect the LEDs (optional)
Connect the LED strand, so we can test whether the control is working or not.
Neopixel strand ------------> Breadboard
V+ ---------------------------> VBATT output power line
GND ------------------------> GND
IN ----------------------------> pin #4
Step 5: Upload the Code
The simple test code is available here:
Step 6: Test With the Adafruit BLE App
Download the Bluefruit LE app.
Available for iOS:
https://learn.adafruit.com/bluefruit-le-connect-fo...
And Android:
https://play.google.com/store/apps/details?id=com....
As soon as you open the app, it begins to scan for devices. If you need to scan again, you can pull down on the list or hit the "scan" button to trigger another scan. You should see your Bluefruit device listed there as a "UART Capable" device. Click on the name, and a menu with options will appear. We are going to be communicating over UART, so click on "UART" to get to the next screen.
Once you select "UART," the app has paired with the BLE module. The BLE module should have a solid blue LED to indicate that the connection has been made. Now you can send serial commands. Looking at the code, we've programmed a response to the letter "p" and to the letter "b".
If you send the letter "p" you should see the pixels glow pink:
https://github.com/molecule/ble-control/blob/maste...
And if you send the letter "b" you should see the pixels glow blue:
https://github.com/molecule/ble-control/blob/maste...
Further reading:
https://forums.adafruit.com/viewtopic.php?f=53&t=7...
https://forums.adafruit.com/viewtopic.php?f=52&t=7...
Step 7: Upload the Lightning Control Code (optional)
I made some simple edits to the code that controls the lightning animation so we can trigger a lightning strike from a phone.
We're going to be talking with the Bluetooth Low Energy module via Hardware Serial commands. Serial is a built-in capability Arduino, so it is very easy to add to the code. You'll add these lines to the setup() method:
https://github.com/molecule/cloud-lightning/blob/v...
Then I added a new method that waits for something to be available over the Serial interface, and break on a "\n" or "\r" character (those are newline characters). Once we get a newline, we return whatever String we saw so far:
https://github.com/molecule/cloud-lightning/blob/v...
Then, in the main loop, I examine that String to see if it matches the String I expect. In this case, I have the lightning effect trigger on the letter "f" (for "flash"):
https://github.com/molecule/cloud-lightning/blob/v...
Those are the only code changes needed to get this to work!