Color Mixing Lamp Controlled by iPhone

Intro

I love the thought of manipulating the outside world with code. This project uses Arduino, Red Bear BLE Shield and iPhone (Swift) to control the color and brightness of a light.

Ingredients

  • Arduino (Genuino) Uno, Microcontroller
  • Red Bear Labs BLE Shield 2.1
  • Breadboard
  • RGB LED
  • 3 x 220 Ohm Resistors
  • Power Source (computer via USB or 9V battery)
  • Jumper Wires

Set Up The Circuit

The circuit is a straight forward connection to a LED. Not very different from a typical ‘Hello World’ in electronics, just using a single RGB color mixing LED and not a single color LED. As seen in the diagram below, the Red, Green and Blue pins are connected to PWM output pins on the controller and the common ground runs to the ground on the controller. The controller is then supplied power via a 9V battery (or the USB port can provide power to the controller).

Program The MicroController

The circuit is set and now we need to teach the controller how / when it should power our various color pins. First, we include the necessary libraries for the BLE shield. Then we define which pin is connected to which color of our RGB LED.

Next step is to do the setup, telling the controller of our output pins, giving them initial values and firing up the bluetooth.

Now that we have the set up it is time to define the Loop, what the controller will be running on repeat as long as it is powered. We set up some debugging to make sure our Bluetooth is connected, and if so we read in the passed values and if the value matches one of our LED pins we will assign the next given value as that pins value.

Program The App

Time to fire up Xcode and throw together a quick app to control our light. The easiest way is to simply download the Read Bear app on the iTunes store and presto it works. However, I don’t learn anything this way so I want to build my own (plus I only need a front end that controls 3 pins, not every pin). This task isn’t too daunting thanks to the lovely open source framework provided by Read Bear. Thus the task at hand is making a simple view to connect to the device and a view to set the values I want and use the provided framework to send them to the Bluetooth shield. I decided to put the main controlling screen first and have a ‘connect’ button that will take us to a table view that will list all BLE devices advertising themselves for connection.

Our viewcontroller swift file for the main view…

*Don’t forget to make the right connections between elements in the storyboard to the code.

Then the simple tableViewController to show all devices advertising bluetooth services and giving us a mechanism to connect to a device.

That is it. Build the app and deploy to device (make sure bluetooth is on for the phone).

Result

To soften the light I did some advanced hardware packaging (I put a Tupperware container over top of it). The opaque casing helps soften and distribute the light. The light off to the side is the ‘on’ light for the BLE shield. Sorry for the poor videography, I’m not too good at recording with one hand and using an App with the other :).

Father, Husband, Tinkerer, Learner, Project Manager, Programmer, Engineer, Expat