Arduino with Ethernetshield and a Servo connected to an Android Phone

controlling a Servo with an Android Phone, using the Arduino and an Ethernetshield

After playing with the Arduino Ethernetshield for some days, I wanted to control a Servo with my Androidphone via the local Ethernet.

As you can see the Servo has a little delay when dragging the slider. This is because the Android Slider onchange Eventhandler fires to often for our Arduino. But to test the connection it’s okay.

Check the sources below how it is done.

Android TCP Client App

I hope you’re already familliar with Android development. If not, check out some of the great “Hello Android” Tutorials.

First of all I created the layout for the application.


The main.XML looks like this:

The App consists only of one Class as shown below:


Dont’t forget to set the rights for Internet/TCP-IP Communication in the Android manifest.

Arduino TCP Server

I modified the TCP Server I used in my previous Arduino TCP Server Tutorial, to recieve Commands.

You have to add an external power supply to your Arduino, because the USB-Port woun’t be enogh to power the Arduino+Ethernetshield+Servo.

understanding and testing

If the Android Client is connected and the seekbar is changed, it will send a command like “set: valueOfTheSeekbar \n”. To set the Servo to 90 degrees, the command would be “set:90\n”. The Server recieves this String character by character. If the newline character “\n” is recieved, the server starts to interpret the command. It checks if the Commandstring starts with “set:” and removes this part. A function will then convert the String “90″ to the integer 90 and move the Servo to this position.

The communication is very simple. You could easily add new commands and Servos.


possible Problems

If you get strange Errormessages from the Arduino Environment or you can’t connect to your Arduino Server (“Transport endpoint is not connected”) you might forgot to power the Arduino with an external power supply.

If the App outputs something like “Permission denied” you forgot to set the Android Manifest Permissions.

6 thoughts on “controlling a Servo with an Android Phone, using the Arduino and an Ethernetshield

  1. djedjica

    Hey great article, I think you saved my life. I only have one question, I’m doing a similar thing only just to test communication I used led light, so my arduino side is fine but I cant seem to send socket from android, this is a bait of code from which it stops doing anything:
    InetAddress serverAddr = InetAddress.getByName(serverIpAddr);
    socket = new Socket(serverAddr, serverPort);
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
    Logs before the line socket write on logcat as they should but after, nothing, and no exceptions are thrown, but socket is not sended, I dont have more ideas what I’m doing wrong, so if you have some tip I would be gratefull

    1. Laurid Post author

      Try my Android Code above and modify it to your needs. I’m not using a PrintWriter, because this didn’t worked for me. Try using a DataOutputStream instead. If you want to do more than just controlling a servo you should definately use Tasks for Socket connections.

      1. djedjica

        Hey I managed to change code by my needs, but now before connecting I get Connection timed out exception, I know server is working, I tested arduino over web browser, but my app is unable ti reach it, do you have any ideas what might be a problem?? Thanks in advance

  2. thereseJ

    i have the same problem here, android app in tablet successfully connect to the arduino server, but suddenly the connection failed. in arduino, the client stop. while in android the program hangs and i cant click connect/disconnect button.
    hope you can share what solution i can make to resolve this.

    Thank you in advance

  3. Carlos Gandarez

    I’m getting exception when initialize Socket() class.
    Exactly here: socket = new Socket(“″, 8008);//create a socket

    About exception? Nothing returned, null :(
    Any idea?

  4. Keith Rowell

    I’m also getting a connection timed out. But I’m not sure how to test that the Arduino server is working first. I don’t see it listed on my windows 7 network > view devices.


Leave a Reply

Your email address will not be published. Required fields are marked *

6 × seven =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">