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
some wire
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

https://github.com/scottCheezem/BlueRCSketch/tree/LED_example


i
t 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?

Comments

author
LeaV5 (author)2017-06-14

Hey man this for this project! I am trying to run this on IOS 10.3 and I get this error "Property 'UUID' not found on object of type 'CBPeripheral *'" in the BLE.m file. When I look at the location of these error messages they refer to p.UUID. Do you how I can fix this?

author
AbishekM4 (author)2016-07-18

Is it possible to receive messages from an arduino on iOS using this method?

author
ShivaganeshguptaS (author)2016-07-07

is it work for HM-10 BLE , i am using HM-10 BLE

author

I am not familiar with that board, so my guess is no. The code and the libraries it references are specific to the RBL BLE board. The principles and design pattern of the iOS app and Arduino sketch might be instructive in adapting to the board you are using, but will not work as it with no changes.

author

Hi sir, thank you for your reply please in my country avaialbe only BLE HM10. any other way to that app.

i want send 1 or 0 value to ardiuno . can u please any alternate thing.

ThankYou.

author
JoshH17 (author)2015-12-30

Is there a away to have the arduino auto pair to the iOS device whe in range?

author
thehand (author)JoshH172015-12-30

well...kind of. For BLE connections like the one featured in this tutorial, the iOS device is actually the one doing the initiating for connections. The arduino, specifically the Red Bear Labs Shield, acts more like a WebSite (read that as web server if that makes sense). Its always ready to talk and take requests from the Browser (in this case your iOS app), but can't initiate the connection itself. The iOS app can be configured to maintain the connection, and possibly try to (re)connect automatically in the background. For more information on that you should look in to "Capabilities" and "Background modes" for Xcode. There are several options related to Bluetooth there that I believe are what you are looking for.

author
TheRealDryErase (author)2015-12-17

I have managed to get the everything running but it's not connecting b/c it's not finding the arduino. I am using tinyduino and its ble module. Could me using different arduino cause the connection error.

Does anyone know what I have to change to get it working?

(https://www.tiny-circuits.com/tiny-shield-bluetooth-low-energy-nordic.html) - this is the ble module i am using

author
thehand (author)TheRealDryErase2015-12-18

Unfortunately, almost definitely. The code included from this 'structable is quite specific to the Red Bear Labs Shield. Other arduino&ble all in one modules or other arduino ble shields almost certainly won't work with this code.

author
SanjayN8 (author)2015-11-20

Can i use BLE-Mini instead of BLE-Shield for communicate with IOS device?

author
bome (author)2015-10-22

Hi!

Great project! Is it possible to connect multiple iOS devices at the same time?

Thanks!

author
thehand (author)bome2015-10-22

No, the kind of communication in this project is board to one iOS device.

author
bome (author)thehand2015-10-22

Thank you! Do have any idea how I could make it work with multiple devices?

author
thehand (author)bome2015-10-22

Well I guess the question is "What are you trying to do?" Bluetooth (and really bluetooth serial here) may not be the right tool for the job as its really designed for a one to one pairing. if you need multiple concurrent devices communicating in both directions either with each other or the arduino, you might try something over a proper ad-hoc wireless network. if the goal is to allow multiple iOS devices to control or take part in controlling some hard ware then there are a couple of flavors of arduino or arduino-shields that allow for tcp/ip over the standard 802.11. of course there is also the Raspberry pi as well, you would need an over the counter wifi dongle, and some server software - most of which comes with the standard RPi distro.

author
ArneG1 (author)2015-09-12

author
ArneG1 (author)ArneG12015-09-12

I got it to work by using the following #include Statements.

#include <boards.h> // "boards.h" is needed for RBL_nRF8001

#include <RBL_nRF8001.h>

author
MaddyMaxey (author)2015-06-10

Can't wait to give this a shot!

author
thehand (author)2015-05-07

It has been a while since I ran this project, but I believe that that specific header file comes from the Red Bear Labs Github for the BLEShield.

https://github.com/RedBearLab/BLEShield

When I originally authored this intractable, Red Bear Labs had put out the first version of their arduino shield and a stand alone mini BLE board. It looks like they have since then expanded their product line and subsequent library of example code. Not to mention that there are now a bunch of other BLE boards on the market right now. My guess (based on a cursory glance at their commit history, and last updated date) is they probably changed the name of the header to keep namespace conflicts from occurring.

tldr: if you're downloading RBL support libraries for the first time now try #include<ble_sheild.h>

also. if you get this working on a fork of my BlueRcSketch, and issue a pull request, I'd happily merge it in.

author
greenonline (author)2015-05-06

The ble.h header file, which is referenced in your BleRC.ino file, no longer appears to be part of the NordicSemiconductor BLE Arduino library. There is only a ble_assert.h and a lot of aci(lib) header files. Has the BLE library changed the header files or have I downloaded the wrong Arduino library?

author
marcmans941 (author)2015-04-30

Ok. I've been going crazy using the Redbear lab board and trying to modify the advertising packets. my problem is I am creating a unique UUID service and I want to advertise it like it says in this Bluetooth Low Energy Advertising Tutorial but I can't get the packets to show it. Has anyone done this? I have the LEDs working but I also have other sensors.

author
thehand (author)marcmans9412015-04-30

hmm...I'm not positive, so don't quote me on it, but the RBL BLE shield that I used in this tutorial might not support the iBeacon packet advertisement - if that is what you are trying to do. which is to say, that I believe this board is basically just serial over BLE.

author
mrtibs2000 (author)2014-05-30

In my circuit, only one of the LEDs light up (connected to pin #3). Any idea what I could have messed up? I'm looking at the Arduino project and I see:

int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3

int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11

int dir_a = 12; //dir control for motor outputs 1 and 2 is on digital pin 12

int dir_b = 13; //dir control for motor outputs 3 and 4 is on digital pin 13

Should I change my pins from 3/5/6/10 to 3/11/12/13?

author
thehand (author)mrtibs20002014-05-30

duuuude, thanks for bringing this to my attention...yea that code won't work - even if you switch some values around. I was trying some stuff out based on the example code...yadayadayada...the main git branch is really more like the experimental branch right now. I will swap things around in the not too distant future. In the mean time, you just want to switch to the "LED_experiement" branch and check that out instead. Sorry for the confusion. Let me know how it turns out.

author
yotto (author)thehand2014-05-31

Similar problem, pin 10 LED always on. Where can we find that "LED_experiement" to check it out? Could you give us an update? Many thanks.

author
thehand (author)yotto2014-06-01

I've updated the links to the Arduino and iOS projects respectively to point at the right branches. Download the code from the updated links and give it another go.

author
yotto (author)thehand2014-06-02

Thanks, worked like a charm. You might want to update the pins that you documented to 7,3,1,5. Its pretty cool. Thanks.

author
thehand (author)2013-08-17

unfortunately not using the code I provided. The select apple devices I referenced are the only ones that support ble. which is the only bluetooth spec for people to develop this kind of project. with an android device you could achieve something similar but it would be different arduino boards and different code.

author
yaly (author)2013-08-16

can't I do it with normal bluetooth ?
as BLE isn't available for my country