If you have an device running iOS or Android you probably know that it can do a lot of things with all the apps available on Apple’s app store or Android’s app market. However, one less known use is to control your computer and even an Arduino or other serial device with TouchOSC (it’s $4.99 on iOS, free for Android). Today’s post will walk you through the theory, setup, and use of this app to get you started with implementing OSC in your projects.
First of all, let’s talk about about what we want to accomplish. In my project, I want a virtual button on the TouchOSC interface on my iPod touch to toggle my Arduino’s onboard LED on pin 13.
Before we go into interfacing Arduino with TouchOSC, let’s have a brief introduction to TouchOSC.
What is TouchOSC?
TouchOSC is an app for both iPad and iPhone/iPod Touch that sends and receives Open Sound Controller messages from/to your computer over WiFi (using UDP packets). Basically, it means that you can press virtual buttons and change “soft” sliders on TouchOSC and it will send their values to your computer. From the introduction page of the OSC website:
Open Sound Control (OSC) is a protocol for communication among computers, sound synthesizers, and other multimedia devices that is optimized for modern networking technology. Bringing the benefits of modern networking technology to the world of electronic musical instruments, OSC’s advantages include interoperability, accuracy, flexibility, and enhanced organization and documentation.
This simple yet powerful protocol provides everything needed for real-time control of sound and other media processing while remaining flexible and easy to implement.
OSC allows you to send the data from the interface on your portable Apple/Android device to your computer in real time, and for your computer to sync back with TouchOSC. But the real strength of this framework lies in the ease of controlling hardware and software with these commands. For example, it is very easy to connect the values of the controls on TouchOSC to a music composition program like Apple Logic (built-in support) or Ableton Live using an OSC interpretation program. This is great if you want to have a flexible, wireless MIDI controller without the limits of a physical interface or cords. There are several tutorials on the app’s site with suggested programs to use with it. In this tutorial, we will use Processing to intermediate the data between TouchOSC and the Arduino.
Setting up TouchOSC
Now let’s setup TouchOSC to sync OSC information with your computer. Both your Apple/Android device and your computer must be connected to the same WiFi network. (Note that this network doesn’t have to be in infrastructure mode [like from a wireless router], so if you’re somewhere where there’s no WiFi network, you can create a network on your computer and use it in ad-hoc mode to initiate a connection.) Start up the TouchOSC app and click the box under Network. Here you have to enter your connection details as follows:
Configuration details
Host: the IP address or hostname of the computer with which to sync
Port (outgoing): port to send OSC packets to computer, I use 8000
Port (incoming): port to receive OSC packets from computer, I use 9000
If you need help with the network setup process, I suggest you take a look at the app’s manual. If you don’t know your computer’s local IP address, you can always ask your trusty friend Google for help. As for which ports to use, I used 8000 for outgoing and 9000 for incoming because they are in the app’s manual and it seems the de facto standard. However, some firewalls might block these ports, so if you can’t seem to get your computer to recieve OSC packets, try a different port.
Check your Firewall settings if something doesn’t work.
Let me warn you to check your firewall settings before going further. I spent several hours being frustrated because of a simple firewall misconfiguration. If you follow these steps and nothing’s working, I would check your firewall before continuing to troubleshoot.
Okay, so now we’ve established the link between computer and app, we can move onto the layout configuration. Click on the Layout box and then select Simple on the Layout panel. This is a good starting layout for testing. Now click the TouchOSC back button. On the main config screen you will find more options, like Stay connected and Accelerometer. Right now we don’t need these, but they are useful for more advanced projects (like this, a Processing-based accelerometer visualizer).
A note about the layouts; TouchOSC installs with a few default layouts for quickly starting your development. But after a while you might wish for something more customized. Well, good news: the developer of the app has released a layout editor for every major operating system, which can be downloaded here at the app’s webpage. When the design is finished, you can press the Sync button on the layout editor and the Add button under the Layout panel on TouchOSC. It will attempt to sync and transfer.
Experimenting with TouchOSC
Now we’re ready to play with the buttons. Click Done on the main config panel and you will be brought to the control interface. If you need to go back, just click the info button at the top right. The page tabs are at the top of the screen.
Update on OSC interpreting code
There is now a library for the Arduino program to natively receive OSC packets. Instead of routing through Processing, you can now interface directly between Arduino and TouchOSC. However, you might want to continue to use the Processing route if you want some feedback from the computer. I’d recommend trying both ways, and seeing what’s better for you. Find out more on recotana.com.
Setting up the Arduino
Toggling controls is (somewhat) fun (as in “Hello, World!” type of fun), but it’s even better to make them actually do something. Now we’re going to setup the other side of our project, the Arduino. I have an Arduino Duemilanove connected to my computer via USB. Nothing is connected to its pins because we will use its integrated LED connected to pin 13 to show the status of the toggle switch.
As for the software, the Arduino is running the StandardFirmata example sketch (what is Firmata?) provided with the Arduino program. Upload it to the Arduino and we’re done.
Normally the Arduino would have to be programmed to react to each message it receives over a serial port. This Firmata library, however, allows for a more direct approach; with this library, other applications can control the Arduino directly over serial, no longer needing the Arduino to interpret code.
Interfacing the Arduino and TouchOSC
So now that we have both ends of our project setup, we can move onto the intermediary program that will receive the messages sent by TouchOSC and redirect them to the Arduino. Processing is a Java-based program that allows for quick prototyping… just like the Arduino program but for running software on your computer instead of uploading code to the Arduino hardware. If you don’t already have it installed, you can download it from the Processing download page. The Processing code will receive the OSC packets sent via the UDP protocol on the network. It will then translate these values to commands that can be understood and acted upon by the Arduino. However, in order for Processing to understand the OSC packets, you will need to download the oscP5 library and install it. (More information is available in the library’s INSTALL.txt file)
Here’s the code I used. The places you will need to edit depending on your configuration will be noted in the comments.
// import oscP5 libraries and Arduino serial libraries import oscP5.*; import netP5.*; import processing.serial.*; import cc.arduino.*; // make a new Arduino object Arduino arduino; // make a new oscP5 object OscP5 oscP5; // setup the value for the button float v_toggle1 = 0.0f; // run the setup loop void setup() { // configure the screen size and frame rate size(200,200); frameRate(30); // start oscP5, listening for incoming messages at port 8000 // if your outgoing port on TouchOSC is not 8000, edit it here oscP5 = new OscP5(this,8000); // establish a serial connection with the Arduino at 57600 baud // this will use the first listing in the serial list, change if necessary arduino = new Arduino(this,Serial.list()[0],57600); // setup Arduino pin 13 as output arduino.pinMode(13, Arduino.OUTPUT); } // when OSC packets are received void oscEvent(OscMessage theOscMessage) { // set the current address to the control name String addr = theOscMessage.addrPattern(); // get the value of the control float val = theOscMessage.get(0).floatValue(); // if the control toggled is toggle1, then toggle float value if(addr.equals("/1/toggle1")) { v_toggle1 = val; } } // the program loop void draw() { // if toggle = off, then make black background // if toggle = on, then make orange background if ((int) v_toggle1 == 0){ background(0); } else { background(255, 165, 0); } // write the int value of toggle button to pin 13 on Arduino arduino.digitalWrite(13, (int) v_toggle1); // print the status in serial debugger println(v_toggle1); }

The final result: the button on TouchOSC controls the LED status and the Processing window background color.
Copy this into the Processing window, make the necessary changes, then run it with the Run button. Wait till a small black window opens up. Now go to the TouchOSC app on the Simple layout and press the first button on the bottom left of the interface. This will send a signal to Processing to change the color of the window, and then Processing will relay this message to the Arduino to toggle its LED.
There are endless possibilities on how to use this control chain. Since the interface is editable, you can customize it to suit your needs. That means the software you use to control your project can be modeled after the hardware, creating a really user-friendly and efficient interface.
I hope this tutorial has been useful to you. You might be interested in other tutorials, like the one from SparkFun or innerqube. For more about OSC, look on Hackaday.
Popularity: 100% [?]






