Arduino Tutorial: Integrating Bluetooth LE and iOS

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.

Leave a rating/review
Save for later
Share

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 create your own Bluetooth LE device using standard off-the-shelf components that you can control wirelessly with a simple iOS app. 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.

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:

A look at the parts you’ll need to complete this project.

All Parts Needed

A look at the parts you’ll need to complete this project.

Here’s the list of parts you’ll need to complete the project, and where to obtain them:

Arduino UNO

Servo

Jumper Wires

BLE Shield

Note: You can use a standard Arduino Uno R3 with a USB A to B cable if you already have one. Both will work with this BLE device.

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 BLE-Shield v2.0.0 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.

That takes care of the hardware side of things – there’s 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.

Note: Depending on the operating system and version of your Arduino board, you might need to install additional drivers in order to communicate with the Arduino. To see if you need to perform this extra step, check the Getting Started with Arduino page, select which operating system you’re using and follow the step-by-step instructions provided.

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:

  1. The user of the iOS app moves the slider to the middle position.
  2. The app sends the number 90, which represents 90 degrees in this case, to the BLE Shield module using the active Bluetooth connection.
  3. The BLE Shield transfers the number 90 to the Arduino board.
  4. 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.

Note: The BLE Shield should not be assembled onto the Arduino board at this time! You may encounter difficulty programming the Arduino if the BLE Shield is installed. As long as you remove it before programming the Arduino, everything should go smoothly. When you assemble or disassemble the BLE shield module or any of the wires, it’s a good idea to remove power from the board by unplugging the USB cable first.

Programming the Arduino

Start the Arduino IDE; you’ll see the editor appear with a blank document, or “sketch”, as shown below:

Arduino IDE 001

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.

Note: Don’t rename Arduino folders unless you also rename the .ino file contained within. The Arduino IDE requires that the .ino file reside in a folder with the same name.

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:

Arduino Kit

This lets the Arduino IDE recognize the serial port to which the Uno is connected.

Note: The USB cable provides not only communication to the Arduino, but power as well. If you’re working on a future project that requires more than 500mA, you’ll have to use some other power source besides USB.

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. Once you enter loop() you never exit, but continually execute the code within in linear order until the board is reset or powered down.

Click the Verify button (the checkmark icon) to ensure everything compiles correctly. If so, you’ll see a confirmation message similar to below:

BLE-Success-Build

Now that you have the basic framework of the Arduino program in place, it’s time to add some logic to control the board.

Owen L Brown

Contributors

Owen L Brown

Author

Over 300 content creators. Join our team.