Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable

You'll learn how to use the Redpark serial cable to connect an iOS device to Arduino, and send messages back and forth between them.

  • Author: Brian Jepson
  • Time required: 30 minutes
  • Difficulty: Moderate
  • Add Note x
  • x 7

The Redpark Serial Cable, which is part of the Redpark Breakout Pack for Arduino and iOS available from the Maker Shed, lets you connect the iPhone to Arduino without jail breaking. This guide shows you one of the simplest examples: connecting an iOS device to an Arduino and turning an LED on and off from an iOS app.

(This guide written with lots of help from Alasdair Allan), the author of a book on this very topic, iOS Sensor Apps with Arduino.

first image

Add Note Edit Step 1 — Wire up the RS232 adapter to the Arduino  ¶ 

  • Connect the RS232 adapter's power and ground to the Arduino's 5V and GND pins, respectively.

  • Connect the RS232 adapter's TX to the Arduino's RX, and the RS232 adapter's RX to the Arduino's TX.

first image

Add Note Edit Step 2 — Make sure you're set up for iOS development  ¶ 

  • To develop apps for the iPhone, iPad, or iPod touch, you'll need to register as an iOS developer. Although you can register for free, all that gets you is the development tools. You won't be able to deploy your apps to a real device. It's $99 a year for an individual developer, $299 for Enterprises (this option lets you deploy apps directly to your team), and free for educational institutions (which also lets you deploy apps to a team).

  • For more information, see the iOS Developer Program.

first image

Add Note Edit Step 3 — Make sure you can run an app on your iOS device  ¶ 

  • If you've never programmed for the iPhone before, you can't count on this guide to help you. There are many learning resources, from Stanford's free Developing Apps for iOS courses to Matt Neuburg's Programming iOS 5 or Alasdair Allan's Learning iOS Programming, both of which are available in print or ebook form.

  • Before you go any further, please make sure you can create and run simple apps on your iPhone, iPad, or iPod touch. It will also be helpful if you familiarize yourself thoroughly with the Xcode development environment.

first image
thumb image 1
thumb image 2

Add Note Edit Step 4 — Create a new view-based project  ¶ 

  • In Xcode, create a new view-based project.

  • Give you project the name HelloArduino, and choose the appropriate device family (iPhone, iPad, or if you're feeling ambitious, Universal, though this will create a bit more work for you).

first image

Add Note Edit Step 5 — Add a switch to your app  ¶ 

  • On the left pane of the screen, expand your project, and open the folder HelloArduino.

  • Next, locate the HelloArduinoViewController.xib and click on it. This will bring up your app's main view, which is blank at the moment.

  • Bring up the Object Library (View→Utilities→Object Library), and drag a Switch to the center of your view.

  • Next, show the Attributes Inspector (View→Utilities→Attributes Inspector) and set the switch's State to Off.

first image
thumb image 1
thumb image 2
thumb image 3

Add Note Edit Step 6 — Wire up the switch to your code  ¶ 

  • Now you need to tell the app's code what to do with the switch.

  • If you need room on the screen, you can hide the Utilities Pane (View→Utilities→Hide Utilities).

  • Next, open the Assistant Editor (View→Editor→Assistant), and locate the toolbar above the window that appears.

  • Click the icon to the right of the left/right arrows and make sure the Assistant Editor is set to automatic. It should be showing the HelloArduinoViewController.h file.

  • Control-click the switch, and drag from it to the code that appears in the Assistant Editor. Hold it just above the "@end" in the code and release. Add a new outlet and name it toggleSwitch as shown.

    • Do this once again, but instead, add a new action, and name it toggleLED. Save the file (File→Save).

first image
thumb image 1
thumb image 2

Add Note Edit Step 7 — Import the Redpark Serial Cable library  ¶ 

  • The Redpark Serial Cable installation will create a folder in your home directory named Redpark Serial SDK.

  • You can download the SDK from the Redpark web site.

  • Open this in the Finder, and select the inc and lib folders, then drag them to your project as shown.

  • A dialog will appear; make sure you check the option "Copy items into destinations group's folder (if needed)" and click Finish.

first image

Add Note Edit Step 8 — Import the iOS Accessory Framework  ¶ 

  • Next, you need to import Apple's Accessory Framework. To do this, click the Project in the left pane, then choose Build Phases, and open the "Link Binary With" section. Click +, locate the ExternalAccessory.framework, and add it.

first image

Add Note Edit Step 9 — Add some declarations to the view controller  ¶ 

  • Select the HelloArduinoViewController.h file to open it for editing.

  • Visit my modified version of the file at GitHub.

  • Edit your copy of the file:

    • Add the two lines between #import <UIKit/UIKit.h> and @interface

    • Modify the @interface line to match mine.

    • Add the three lines just above UISwitch *toggleSwitch;.

  • Save the file (File→Save).

first image

Add Note Edit Step 10 — Add some functions to the app delegate  ¶ 

  • Select the HelloArduinoViewController.m file to open it for editing.

  • Visit my modified version of the file at GitHub then edit your copy of the file:

    • Locate the viewDidLoad method and replace it with my version of it.

      • Be sure to Remove the comment delimiters (/* and */) before and after it.

    • Implement the methods required by the RscMgrDelegate protocol (add everything from my file from the #pragma mark - RscMgrDelegate methods to the end of the file.

    • Locate the toggleLED method and replace it with my version of it.

  • Save the file (File→Save).

first image

Add Note Edit Step 11 — Declare support for the serial cable  ¶ 

  • This step is optional, but it will avoid the problem of you seeing an error message ("This accessory requires an application...") each time you plug in the cable.

  • In Xcode, expand the Supporting Files group and click on Hello-Arduino.plist to open it.

  • Right-click the bottom row, and choose Add Row. Click the up/down pointing arrows to the right of the new row's key (it will probably default to "Application Category") and choose "Supported external accessory protocols".

  • Click the triangle to the left of the key name you just selected to open up the list. In the value field for Item 0, type com.redpark.hobdb9.

  • Save the file (File→Save).

first image

Add Note Edit Step 12 — Deploy and test the Arduino sketch  ¶ 

  • Grab a copy of the sketch from GitHub.

  • Connect your Arduino to your computer.

  • Run the Arduino IDE and upload the RedparkToggle sketch to your Arduino.

  • Temporarily disconnect the jumper wire going to the Arduino's RX pin (the green cable shown in the first step), and upload your sketch.

  • You must disconnect it before uploading a sketch because the RS232-TTL adapter will otherwise interfere with the upload process. Leave the jumper wire disconnected until you complete the next step.

first image

Add Note Edit Step 13 — Test the Arduino  ¶ 

  • Open the Arduino's serial monitor (Tools→Serial Monitor), and make sure it's set to 9600 baud.

  • Wait a few seconds for the sketch to restart, then type 1 and click Send. The LED should come on. Try that again with 0 instead of 1, and the LED should go off.

  • Reconnect the jumper wire when you're done.

first image

Add Note Edit Step 14 — Deploy the app  ¶ 

  • Connect your iPhone, iPad, or iPod touch to your Mac with a dock cable.

  • Make sure Xcode is configured to run it on the device you just connected (see the figure).

  • Click the Run button in Xcode.

  • If you get any errors, review the preceding steps to make sure you followed them exactly. If you still have problems, you can try downloading the project from GitHub.

first image

Add Note Edit Step 15 — Run the app  ¶ 

  • The app is running on your iOS device, but you need to swap cables next, so click Stop in Xcode and disconnect the dock cable.

  • Plug the Redpark Serial Cable into your device and into the RS232 serial adapter.

  • Locate the HelloArduino app on the home screen, and run it.

If everything is working correctly, you'll be able to turn the Arduino's built-in LED on and off by tapping the switch button!

For more information, check out the Arduino category page.

Did you successfully follow this guide?

This guide has been completed 7 times.

Related Products

Comments Add Note Comments are onturn off

Guide Add Note

Any chance of adding support for the SkyWire cable in the SDK soon?

RobotGrrl, · Reply

You should ask Redpark, but my understanding is that it is unlikely that they can do this. Apple will typically only approve devices for specific uses, and each different product (their SkyWire cable, their Cisco router cable) identify themselves as different devices, and actually need different software to enable them--even though they are the same hardware inside!

But Redpark could say for sure.

- Brian

Brian Jepson,

Please note that the Arduino Code is wrong! It should be:

if(cmd == 0) //Without ' ' around the zero

{

...

}

if(cmd == 1) //Without ' ' around the one

{

...

}

It caused me some thinking. But happy are the lucky one's. Greating from Switzerland ;-)

MK

Moritz Kemper, · Reply

Thanks Moritz,

It's surprising that it's not working, because I am sending ASCII '0' and '1' (0x30 and 0x31) from the iOS app. See:

https://github.com/bjepson/iPhone-Arduin...

But if your iOS code is sending 0 or 1 as values, then your change is necessary.

Thanks,

Brian

Brian Jepson,

With the Arduino sending bytes back to ios, how do you read those bytes?

In readBytesAvailable:numBytes, the RscDemo uses:

int bytesRead = [rscMgr read:(rxLoopBuff+loopbackCount) Length:numBytes];

How should I convert this int to chars?

thanks!

j e, · Reply

bytesRead is the count of bytes that were read. In that demo, I think that the rxLoopBuff is the buffer that contains the actual value read. That array would contain a bunch of ints so you'd have to convert them to chars. But you might also want to look at Alasdair Allan's demos.

Brian Jepson,

It's not working for me. :( Everything works until the last step and then: LED does not light. Maybe it's the P4?

Krista Palen, · Reply

Hi Krista,

 

Here are a few things that you might want to look into:

 

Did Step 13 work OK? Were you able to blink the LED using the Serial Monitor?

 

Did you try running the app with the Arduino plugged into the computer and with the serial monitor running? This will enable you to view part of the conversation between the cable and the Arduino, but will probably prevent the app from working (but it will be an instructive test). The goal here is to confirm that the iPhone is indeed sending the messages we think it's sending.

 

Have you run the Rsc Demo app that comes with the RedPark serial cable (see the Serial Cable SDK README)? There is a loopback test you can run in that app. You'll need to connect the loopback adapter that was supplied with the cable. This will let you confirm that the cable is working OK.

 

Also, one of our authors (the guy who turned me on to this all), has some good example code you might want to look at: http://www.dailyack.com/2011/07/connect-...

 

- Brian

Brian Jepson,

Thank you for the guide, worked great!

Pierre Baillargeon, · Reply