Electronics for iPhone Developers Tutorial: Control a LED from your iPhone!

Dani Arnaout
Control a LED from your iPhone!

Control a LED from your iPhone!

This is a post by iOS Tutorial Team Member Dani Arnaout, a Computer Engineering Undergraduate at BAU,Lebanon. Dani has a YouTube channel full of programming & iOS related videos.

Welcome back to our Electronics for iOS Developers tutorial series!

In the previous tutorial you learned how to create a little project simulating a traffic lights system. That was cool, but we’re iPhone developers after all – wouldn’t it be cool to control this via an app?

That’s exactly what you’ll do in this tutorial! You’ll write a simple iPhone app that can turn an LED connected to an Arduino and and off, as simply as tapping the screen!

It might look silly to read through two tutorials to finally control a LED with your iPhone. Oh and it would be useless too. So why are you doing that?

Well, an instructor at my university once said: “If you can control a LED, you can control ANYTHING”.

And guess what, he’s right! Basically, turning a LED on or off is just like switching a Fan, TV, Dishwasher, or any other device. So for simplicity we’re gonna use a LED. But if you can get this working, then you can control anything.

So keep reading to expand your iOS (and hardware) powers!

Material Needed

Just like last time, you need some materials:

  • Arduino Uno.
  • Arduino Ethernet Shield.
  • 200 ohm resistor (x1).
  • LED (x1).
  • breadboard.
  • Wires.
  • RJ-45 cable (ethernet cable).
  • Your router.

You can get your materials from any electronics shop. As for the Arduino Uno & the Ethernet Shield, here’s a link to their official distributors page.

Note from Ray: If you bought the stuff that I mentioned from Sparkfun Electronics in the previous tutorial, you should have everything you need (apart from the router and ethernet cable, which hopefully you have around).

Let’s watch the video!

Also similar to last time, I made a video so you can easily see all the steps needed to create this project.

The video shows you all the steps needed to create this project. The hardware part tells you about the material needed and how to do all the wiring. And the software part into two parts:

  1. The code written into the Arduino Uno using C language.
  2. The code written for the iPhone app using Objective-C.

So grab your materials and follow along with the video below to make an LED-controlling iPhone app! :]

Note from Ray: Here’s how my setup worked after doing this with my Sparkfun Inventor’s Kit. Note I chose to use the breadboard rather than twist the wires together to make things easier. If you get confused on how to do this, check out the CIRC-01 exercise from the inventor’s kit, it’s very similar.

Setup with Sparkfun Inventor's Kit

Wait – how did that work?

You should understand how the hardware works, because it was the same as we did for the traffic light app (but actually a little simpler).

But we kind of glossed over the Arduino web server code in the video. How does that work?

The code you used is turning the Arduino into a web server. It is waiting for an HTTP request with a particular path name ($1 or $2), meaning “turn the LED on” or “turn the LED off”.

Let’s go over the code section by section.

  Web Server Demo
 Allows you to turn on and off a LED by entering different urls.
 To turn it on:
 To turn it off:
 Based almost entirely upon Web Server by Tom Igoe and David Mellis
 Edit history: 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
modified 3 Jul 2012 (for this tutorial)
 by Dani Arnaout

Just the header here – explains what’s going on.

#include <SPI.h>
#include <Ethernet.h>

Includes the header files we need.

boolean incoming = 0;

Defines a variable which we’ll use while parsing the URL sent to us. Once we read a “$” character we’ll set incoming to true, so we know the next character is the one we care about (1 or 2).

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress ip(192,168,0,2); //<<< ENTER YOUR IP ADDRESS HERE!!!
// Setting up a MAC address and IP address for your controller.
// The IP address will be dependent on your local network.

This assigns a mac address and an IP address to the Arduino. This is the part you'll have to modify to an IP address on your local network.

EthernetServer server(80);
// Initializing the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP).

This initializes the built-in Ethernet server library, which starts a basic web server.
It creates a server that listens for incoming connections on the specified port.
Since you're going to send HTTP requests, so port number 80 is the one for you.
Now if you want to know more about the Ethernet Library, you can check this link.

void setup()
  pinMode(2, OUTPUT);
 // Setting the 2nd pin to output.
  Ethernet.begin(mac, ip);
  // starting the Ethernet connection using the MAC & IP addresses that we have set.
 //starting the server.

Here we set pin 2 (where you connected the LED) as an output pin, and start the Ethernet server on the specified Mac address and IP.

void loop()
<p><em>Checking for a request</em> 
  EthernetClient client = server.available();
 // listening for incoming clients
  if (client) {
    boolean currentLineIsBlank = true;
   // an http request ends with a blank line
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply

        if(incoming && c == ' ')
        //reads URL string from $ to first blank space
          incoming = 0;
        if(c == '$'){ 
        // if we received a $ sign then we have received a request
          incoming = 1; 

Up to this point, we wait for a connection, and then keep reading characters until we find a $. When we do, we set incoming to true.

        //Check for the URL string $1 or $2
        if(incoming == 1){
          // printing the result on the serial port (on your computer)
          if(c == '1'){
            // printing "ON" to your screen.
            digitalWrite(2, HIGH);
            // setting the 2nd pin state to HIGH (turning on the LED)
          if(c == '2'){
            // printing "OFF" to your screen.
            digitalWrite(2, LOW);
            // setting the our LED state to off (turning it off)

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;

Here we check to see if the next character is a 1 or 2, and set the current to HIGH or LOW appropriately (i.e. turning the LED on or off).

    // give the web browser time to receive the data
    // if a lot of requests are sent, the Arduino will freeze
    // closing the connection:

Here we give the web server time to receive data and shut down the connection. That's it!

Where to go from here?

Here's our project. It consists of the Arduino Code & Xcode Project (iLamp).

As mentioned in the first tutorial. This is an open-source community. So you’ll be able to find all free tutorials, documentation, resources, and examples on the official Arduino website.
The website is user friendly and easy to use.

Oh and just one more thing. Here are some interesting iPhone-Arduino projects that I have developed with my friend "Mohammad Issa".
First is the iHouse & second is the iCar.

  1. The iHouse is simply a complete automated “smart” house that can be controlled throughout your iPad. That was done with the use of the Arduino Uno & the Ethernet Shield. Here’s a screen shot of what the iPad app looks like.

  2. The second one is the iCar, which is an iPhone controlled car. Below is a screen shot of what the app looks like.There’s a gear box for “forward/backward” motion, the gas pedal, and of course, the steering that uses the accelerometer of the iPhone. Interesting isn’t it?

Hope you enjoyed this two parts tutorial. Bye!

Note from Ray: Let me know if you like to see more Electronics for iPhone Developers tutorials on this site in the future!

This is a post by iOS Tutorial Team Member Dani Arnaout, a Computer Engineering Undergraduate at BAU,Lebanon. Dani has a YouTube channel full of programming & iOS related videos.

Dani Arnaout

Dani Arnaout is a Systems Engineer working at Lextech Global Services.
You can find him on Twitter.

Other Items of Interest

Save time.
Learn more with our video courses.

raywenderlich.com Weekly

Sign up to receive the latest tutorials from raywenderlich.com each week, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

PragmaConf 2016 Come check out Alt U

Our Books

Our Team

Video Team

... 27 total!

iOS Team

... 83 total!

Android Team

... 44 total!

Unity Team

... 16 total!

Articles Team

... 4 total!

Resident Authors Team

... 32 total!

Podcast Team

... 8 total!

Recruitment Team

... 8 total!

Illustration Team

... 4 total!