Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

Make: Projects

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.

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

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.

Steps

Step #1: Wire up the RS232 adapter to the Arduino

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #2: Make sure you're set up for iOS development

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #3: Make sure you can run an app on your iOS device

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #4: Create a new view-based project

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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).

Step #5: Add a switch to your app

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #6: Wire up the switch to your code

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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).

Step #7: Import the Redpark Serial Cable library

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial CableConnect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #8: Import the iOS Accessory Framework

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

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.

Step #9: Add some declarations to the view controller

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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 and @interface
  • Modify the @interface line to match mine.
  • Add the three lines just above UISwitch *toggleSwitch;.
  • Save the file (File→Save).

Step #10: Add some functions to the app delegate

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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).

Step #11: Declare support for the serial cable

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark 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).

Step #12: Deploy and test the Arduino sketch

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #13: Test the Arduino

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #14: Deploy the app

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Step #15: Run the app

PrevNext
Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable
  • 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.

Conclusion

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!

Brian Jepson

I'm a tinkerer and finally reached the point where I fix more things than I break. When I'm not tinkering, I'm probably editing a book for Maker Media.


Comments

  1. Brian Jepson says:

    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

  2. Brian Jepson says:

    Thanks Moritz,

    It’s surprising that it’s not working, because I am sending ASCII ’0′ and ’1′ (0×30 and 0×31) from the iOS app. See:

    https://github.com/bjepson/iPhone-Arduino-Simple-Switch/blob/master/HelloArduino/HelloArduinoViewController.m#L57

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

    Thanks,

    Brian

  3. Brian Jepson says:

    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 [http://www.dailyack.com/2011/07/connect-your-iphone-to-real-world.html|Alasdair Allan's demos].

  4. Brian Jepson says:

    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-your-iphone-to-real-world.html
     
    - Brian

  5. Christina says:

    when you run the app with the iphone connected to your computer does xcode show a 1/0 when you switch from on to off?

    Also Step 11 is critical in order to make the serial cable send the data. The app can compile and run but may not send data if this step is omitted.

  6. Brian Jepson says:

    Hi Nima,

    The Arduino serial monitor is software that runs on your computer, and must be connected to the Arduino to work, but if you follow this example, you need to connect the Arduino’s built-in serial port to the iPhone. I don’t think you can reliably send messages to both the computer and iPhone at the same time, so I would not advise trying this. If you must communicate with both an iPhone and a computer simultaneously, consider using an Arduino with multiple serial ports, such as the Mega, or consider using SoftwareSerial to communicate with the RedPark cable (I do now know if that is supported or if it is reliable though).

    - Brian

  7. Brian Jepson says:

    Hi Mike,

    Which cable are you using? Can you post a photo of the wiring between the Arduino and RedPark cable somewhere?

    Thanks,

    Brian

  8. Brian Jepson says:

    Thanks, Morten! Is everything working OK now?

  9. Brian Jepson says:

    If you can post your code up on https://gist.github.com, I could take a look and ask some other folks to look too.

  10. Brian Jepson says:

    Thanks. I don’t see anything that jumps out at me, but I’ll ask the Redpark folks to have a look!

  11. Brian Jepson says:

    Mark, I got a couple of questions from the Redpark folks for you:

    * Which version of the cable (TTL or RS232) are you using?
    * Did you declare the protocol as shown in http://makeprojects.com/Project/Connect-an-iPhone-iPad-or-iPod-touch-to-Arduino-with-the-Redpark-Serial-Cable/1130/2#s6073

    Thanks,

    Brian

  12. Mark A says:

    Ah, just read on their website that the RJ45 cable only works with the Get Console app, and doesn’t work with other apps. I just assumed the cables were the same, but with different connections. Seems like I might have to get another type of cable.

    Thanks for all the help!
    Mark

  13. Brian Jepson says:

    Oh, yes, that’s right. Unfortunately, you do need the hobby version of the cable to be able to do this.

  14. Brian Jepson says:

    I haven’t seen that problem myself, but it’s possible that the Redpark SDK has changed since this example was written. Sam’s comment (from Oct 6) has some information on changes needed when working with the txbuffer.

  15. Greetings! Very useful advice in this particular post!

    It’s the little changes which will make the most significant changes. Thanks a lot for sharing!

In the Maker Shed