Arduino Tutorial: Integrating Bluetooth LE and iOS with Swift
Learn how to control a servo wirelessly from your iPhone in this tutorial with Arduino, Bluetooth LE (low energy) and iOS. By Owen L Brown.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Arduino Tutorial: Integrating Bluetooth LE and iOS with Swift
30 mins
Update 04/19/2015: Updated for Xcode 6.3 / Swift 1.2
Update 11/17/2015: Updated for Xcode 7.1 / Swift 2.
Update 10/19/2016: Updated for Xcode 8.0 / Swift 3.
Creating machines that interact with the physical world is an incredibly satisfying thing. Controlling them via Bluetooth with your iOS device is just plain awesome!
In the past, the only way to create an Apple-approved Bluetooth device was by being part of the MFi program. With Bluetooth Low Energy 4.0, individuals and small companies can develop their own products and market them without the overhead of Apple’s MFi regulations. That means you can talk to devices over Bluetooth LE with very little configuration and code, which opens up an entire world of opportunity for iOS developers.
This tutorial will teach you how to integrate Bluetooth LE and iOS using standard off-the-shelf components and an Arduino. Since the focus of this project is building a BLE device, you’ll be using the iOS Core Bluetooth framework. If you’re unfamiliar with Core Bluetooth, check out our Introduction to Core Bluetooth: Building a Heart Rate Monitor tutorial.
Aside from the Arduino and BLE shield components listed below, you’ll also need to run the companion app on a real iOS device – that means you’ll need a paid iOS developer account.
Let the building begin!
Getting Started
Using off-the-shelf components for this build makes creating a Bluetooth device a snap. Here’s an image showing the basic elements you’ll use in this project:
Here’s the list of parts you’ll need to complete the project, and where to obtain them:
- iPhone 4s or newer with Bluetooth LE
- Arduino Red Board, $19.95 at SparkFun.com
-
USB Mini-B Cable, $3.95 at SparkFun.com
- Servo Motor, $8.95 at SparkFun.com
- Jumper Wires, $1.95 at SparkFun.com)
- Black Widow Long Range BLE Shield based on Bluegiga’s BLE121LR Module, $79.00 at www.back-40.com.
The Arduino, servo and jumper wires can be purchased together from SparkFun.com in the SparkFun Inventor’s Kit for Arduino. This kit comes with many useful components and tutorials to get you started in the hardware world.
You can find less-expensive BLE Shields other than the one listed in this tutorial, but be aware that many of them sacrifice flexibility to save on cost. The Black Widow BLE Shield allows you to program your own custom BLE Services and Characteristics onto the module. This means you can take full advantage of the Bluetooth 4.0 data structure. Plus, it gives you extra long range with the built-in BLE121LR module.
That takes care of the hardware side of things – there are a few extra software pieces to take care of as well.
Download the Xcode starter project here. The starter project includes the view controller and base Core Bluetooth implementation to save you time. You’ll add more code to interface with the BLE Shield later on in the tutorial.
Next, download and install the Arduino IDE from the Arduino Download page; it’s available for Windows, Mac OS X and Linux platforms. You’ll use the Arduino IDE to write and compile the code for the Arduino. Ensure you grab the latest release version, not the beta or nightly builds.
The Basic Design of your App
Your finished project will consist of an iOS app that will send messages via Bluetooth to the BLE Shield module. The module will then send the message to the Arduino board to tell the servo which position it should rotate to.
Here’s an example use-case of your project:
- The user of the iOS app moves the slider to the middle position.
- The app sends the number 90, which represents 90 degrees in this case, to the BLE Shield module using the active Bluetooth connection.
- The BLE Shield transfers the number 90 to the Arduino board.
- The Arduino board rotates the servo to the 90 degree position.
Seems pretty straightforward! To begin, you’ll work with the Arduino IDE and program the board logic.
Programming the Arduino
Start the Arduino IDE; you’ll see the editor appear with a blank document, or “sketch”, as shown below:
Before doing anything else, click File\Save in the top menu and save the current file to a convenient location as Arduino_Servo_Controller. This creates a folder in your save location that contains a file with a .ino
file extension.
Before you start writing code, you’ll need to set up the IDE to communicate with the Arduino Uno board.
Select Tools\Board\Arduino Uno to let the IDE know what kind of board you’ll be dealing with. Next, connect the Uno to your computer with the USB cable as shown below:
This lets the Arduino IDE recognize the serial port to which the Uno is connected.
Once that’s done, select Tools\Serial Port\… and select the USB port the Arduino is connected to. Generally it’s similar to /dev/tty.usbserial… or /dev/tty.usbmodem….
At this point the Arduino IDE is ready for programming.
Add the code below to your project using the Arduino IDE:
// Arduino Bluetooth LE Servo Controlled by iOS
void setup() // Called only once per startup
{
}
void loop() // Continuous loop
{
}
Arduino programs are typically split into two main functions: setup()
and loop()
.
The setup()
function does exactly what it is says: it’s called only once on startup and is a great place for setting up your hardware and software. The loop()
function is called once setup()
is done. loop()
will be called over and over again until you reset or power down the board.
Click the Verify button (the checkmark icon) to ensure everything compiles correctly. If so, you’ll see a confirmation message similar to below:
Now that you have the basic framework of the Arduino program in place, it’s time to add some logic to control the board.