Introduction: Talk to an Arduino With an IOS Device Using Bluetooth Low Energy
This is a simple afternoon project to get your iOS device talking with Red Bear Lab's Bluetooth Low Energy Shield.
For this project you will need
an Arduino (we'll use the Leonardo)
an iPhone 4s, 5, iPad 3, 4, mini, or an iPod Touch 5th generation.
Bluetooth Low Energy Shield from Red Bear Labs (http://redbearlab.com/bleshield/)
4 or 5 LEDs
a momentary push switch
a bread board or proto-shield
Red Bear lab's provide an app in the App Store that is general purpose enough to get your BLE-compaitble ios device talking to the shield, but what if we want to take it a little farther, and write our own custom app? Well thats what we are going to cover today.
NOTE!!! TO RUN A CUSTOM APP ON YOUR IOS DEVICE THAT IS NOT IN THE APP STORE YOU WILL NEED TO BE ENROLLED IN THE IOS DEVELOPER PROGRAM WITH APPLE. it is $100 dollars and is not not covered in this tutorial.
Step 1: Wire Up the Arduino.
The hard ware side of things will be simple enough. We're going to use 4 LEDs connected to some of the PWM pins on the Arduino. we're going to use another LED to indicate connectivity to the iPhone, and a push button to trigger some action on the phone.
The BLE shield uses Pin 8 and 9 to do its stuff, so its a good idea to leave those alone.
the positive terminals from our LEDs will be going to the following pins on the Arduino
3, 5, 6, and 10
The negative terminals will all go to a common ground. I did not use any resistors in my project, depending on which LEDs you use, you may want to include them.
I'm using on of the LEDs on my proto-shield connected to pin 4 on the Arduino to indicate connectivity with the phone, and one of the toggle switches connected to pin 7.
Step 2: Arduino Sketch
Red Bear Labs has a ton of great code for iOS, Mac OS X, and the Arduino to browse through on their GitHub page. Included in that is the Bluetooth Shield SDK. You'll want to grab support libraries from the link on their site http://redbearlab.com/bleshield/ and drop them in your Arduino libraries folder, or import them through the Arduino IDE.
After you've got them imported, check out the Sketch here
it is a bare bones sketch that reads from the BLE shield one byte at a time. The First byte it reads is the pin to address, the second byte is the value (0-255) to apply to the LED at that pin.
Step 3: Xcode Project
For the iOS interface we're going to keep it simple and just use a typical Analogue game pad and a connect button. the code for the Analogue stick control comes curtisy of https://www.cocoacontrols.com/controls/jscontroller
should note that the SDK from red bear labs can also be used to Mac OS X applications....
In order to build the project and run it on a physical device you will need to join Apples iOS developer program, and have Xcode installed.
Grab the code from https://github.com/scottCheezem/BlueRCiOS/tree/LED_example
I've included the headers from Red Bear Labs and the Analogue stick controls. It should be ready to build and go, but I've only tested it on my iPhone 5 with iOS 7 beta 3
The project is built from a single view template. Joystick, a label for the signal, and a button to toggle connections - what more could you ask for in a remote control app.
Step 4: Upload Sketch. Build & Run.
If you've gotten this far, you have the LEDs wired up, the Sketch uploaded on your Arduino, and the Xcode project running on your iOS device. After you press the connect button and see the bluetooth icon indicate that you are connected to the Arduino you should be ready to interact with the Sketch. As you move the analogue stick around on the iPhone, the LEDs light up. If you press the toggle button, the iPhone vibrates.
So wouldn't it be neat if someone swapped out the LEDs for some motors and added a bumper to the toggle button?
We have a be nice policy.
Please be positive and constructive.