Control Arduino Using Android App

About: I'm an embedded ICT student.

In this instructables I'm going to explain how to control the arduino IO pins using an android app, which we'll write here.

The communication will be over ethernet and we'll use the GET request to activate the IO's.

Let's get started!

Step 1: Parts

For this instructables you'll need several parts.

  • An arduino
  • An android smartphone or tablet (I'm using android 5.0.1)
  • An ethernet shield
  • 3 Led's
  • 3 220 ohm resistors
  • Some jumper wires
  • a breadboard
  • A computer with android studio installed

Step 2: The Ethernet Shield

I got this Ethernet shield from

It worked instantly on my arduino mega (also from

On the shield you have 2 SPI devices. The SD card reader and the W5100 IC for Ethernet.
In this instructables we'll only be using the Ethernet part.

Step 3: Schema

We need to connect the 3 led's to the arduino. You can use every pin except pin 0, 1, 10 to 13 and 50 to 53.
I'm using pin 22, pin 23 and pin 24.

You also need to connect your arduino to your local network. Internet isn't needed.

Step 4: Arduino Sketch

For the arduino sketch I started from the example webserver sketch.

I tried documenting every thing, but if you have a question feel free to ask!

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

// Set the MAC address
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// Set the IP address
IPAddress ip(192, 168, 1, 177);

// Start a server at port 80 (http)
EthernetServer server(80);

void setup() {
  // Open serial communications

  // start the Ethernet connection and the server
  Ethernet.begin(mac, ip);

  // Pin 22 - 24 output (leds)
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);

void loop() {
  // Check if client connected
  EthernetClient client = server.available();
  if (client) { // If there is a client...
    boolean currentLineIsBlank = true;
    String buffer = ""; // A buffer for the GET request
    while (client.connected()) {

      if (client.available()) {
        char c =;// Read the data of the client
        buffer += c; // Store the data in a buffer
        if (c == '\n' && currentLineIsBlank) {// if 2x new line ==> Request ended
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println(); // Blank line ==> end response
        if (c == '\n') { // if New line
          currentLineIsBlank = true;
          buffer = "";  // Clear buffer
        } else if (c == '\r') { // If cariage return...
          //Read in the buffer if there was send "GET /?..."
          if(buffer.indexOf("GET /?led1=1")>=0) { // If led1 = 1
            digitalWrite(24, HIGH); // led 1 > on
          if(buffer.indexOf("GET /?led1=0")>=0) { // If led1 = 0
            digitalWrite(24, LOW); // led 1 > off
          if(buffer.indexOf("GET /?led2=1")>=0) { // If led2 = 1
            digitalWrite(22, HIGH); // led 2 > on
          if(buffer.indexOf("GET /?led2=0")>=0) { // If led2 = 0
            digitalWrite(22, LOW); // led 2 > off
          if(buffer.indexOf("GET /?led3=1")>=0) { // If led3 = 1
            digitalWrite(23, HIGH); // led 3 > on
          if(buffer.indexOf("GET /?led3=0")>=0) { // If led3 = 0
            digitalWrite(23, LOW); // led 3 > off
        } else {
          currentLineIsBlank = false;

That was the code for on the arduino.

Pretty easy, right? Let's go to the app!

Step 5: App Layout

For creating an android studio project I'll redirect you here. The start is the same, choose a name and create the main activity, but after you deleted the "hello world" textview you need to add 3 buttons of any kind. I'm using a switch, a toggle button and a normal button, but you can choose what you like most.

When you get a render error, on the top of the window, change Apptheme to Appcompat.NoActionBar

! Attention !
A normal button will only lit it's led when pressed. The led will go out when the button is released.

In res/values/styles.xml you need to change parent to: "Theme.Appcompat.NoActionBar"

Ok, now we can start coding the app!

Step 6: App Coding

For coding the app, I made it easier for you. You need to copy this code to and change package laurens_wuyts.arduinoiocontrol to company.appname.

package laurens_wuyts.arduinoiocontrol;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.ToggleButton;


public class MainActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == {
            return true;

        return super.onOptionsItemSelected(item);

       /*  This is a background process for connecting      */
      /*   to the arduino server and sending               */
     /*    the GET request withe the added data           */

    private class Background_get extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... params) {
            try {
                 /* Change the IP to the IP you set in the arduino sketch */
                URL url = new URL("" + params[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder result = new StringBuilder();
                String inputLine;
                while ((inputLine = in.readLine()) != null)

                return result.toString();

            } catch (IOException e) {
            return null;

In this code you only need to change the IP to the IP of your arduino.

For checking the buttons you'll need to do 2 things:

  • Define the button
  • Add an onclick/onchange listener for every button.

Define buttons:

/* For a switch */
Switch led1 = (Switch) findViewById(;

/* For a toggle button */
ToggleButton led2 = (ToggleButton) findViewById(;

/* For a normal button */
Button led3 = (Button) findViewById(;

Add onclick/onchange:

Place the onclick/onchange listeners in the onCreate function.

/* For a switch you'll need an "OnCheckedChangeListener" like this */
led1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            /* Switch is led 1 */
            new Background_get().execute("led1=1");
        } else {
            new Background_get().execute("led1=0");

/* For a toggle button you also need a "OnCheckedChangeListener" */
led2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            /* Toggle button is led 2 */
            new Background_get().execute("led2=1");
        } else {
            new Background_get().execute("led2=0");

/* For a button you'll need a "OnTouchListener" */
led3.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            /* button is led 3 */
            new Background_get().execute("led3=1");
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            new Background_get().execute("led3=0");
        return true;

That's all the coding that needed to be done! Now we need to add permissions to your app.

Step 7: Adding Permissions to Your App

For your app to work you'll need to give it permissions. We only need 1 permission: Internet access. To give this permission you need to open the manifest file and add:

<uses-permission android:name="android.permission.INTERNET" />
Just before

Step 8: Resume

In this instructables I showed you how to control arduino IO pins over the network from an android phone.
I also included the full app directory for those who would like to use it.

I hope you liked it please leave comments and feel free to ask questions (There are no stupid questions ;) )




  • Beauty Tips Contest

    Beauty Tips Contest
  • Games Contest

    Games Contest
  • Sew Tough Challenge

    Sew Tough Challenge

32 Discussions


2 years ago

I want to make the same thing but want to use bluetooth(HC-05) instead of ethernet/wifi shield. Could you please provide the codes regarding the same?

1 reply

Reply 4 months ago

i wanna do the same thing .. if u resolve that can u help please ?


Question 6 months ago on Step 4

where goes the wifi name and password?


Question 11 months ago

Hei! amm... if it runs for few hours then it freezes or something. if i press buttons on phone then it doesent respond, then i have to restart arduino and it works again, but i want it to work withour restart

Sakshi googal

Question 1 year ago on Step 1

I wanted to use ESP8266 in place of Ethernet shield what changes I will have to do in arduino sketch and android app code...plz tell me

1 answer
nihelSakshi googal

Answer 1 year ago

I also use ESP8266 do you have resolve the problem? If you have resolve the problem please help me


Question 1 year ago

Could I make it use Bluetooth on an Arduino uni


Question 1 year ago on Introduction


My name is Raymond Audet

In (Control arduino using android app)

1- I don't have a mega board but a uno board. What connectors should I use on my board.

2- I would like to opperate it on internet with my cell phone.

Is it possible?

Thank you in advance

Raymond Audet



1 year ago


Great work :D


1 year ago

Hi!!! Can you help me please? I did all this and it was great! But now I need to read with the app and write it so I can create a notification


1 year ago

Great work. I've download the whole package to see how it works but unfortunately I got around 19 errors while compiling :O Is it the obsolete code?


2 years ago

i am getting error in app

if (id ==

action setting cannot resolve

what to do


2 years ago

I'm using pin 5 and i wanna switch on just one le, but it stil don't work for me? any helps please!


2 years ago

is it usefull for arduino uno with changing number of pins ?

1 reply

Reply 2 years ago

Yes, this should work on an arduino uno.
If you, like you said, change the pins for the LED's.



2 years ago

Hey man

Thanks for your amazing instructable "Control Arduino using android app"

I have learnt a lot reading your code and have tried implementing it for my project.

Everything works great. I have added a Write to EPROM to save the states of LED.

I want to send a String to my Phone after i switch on the LED. So that when i receive the string, my switch gets toggled.

I am doing it because when i close the app, it makes the switch to off state, i want the App to read the state of the LED from the arduino.



2 years ago

Could you please help me with this error. I don't know what I am missing.


public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(, menu); return true;}

- I am having a problem with regards to the it says it can't resolve symbol menu

- I am also having an error with the

I thinnk maybe it's because they have not been defied as yet or am i wrong?/

please do help me ASAP

1 reply

Reply 2 years ago

This is because of an update of android studio. You can just remove the functions onCreateOptionsMenu and onOptionsItemSelected

This should fix the error messages.



3 years ago

Just an observation... in the line where you give the permission on the manifest, there was a ttping mystake:

<uses-permision android ......

it should be: <uses-permission android...

(the word "permission" is tiped with one "s" in this tutorial, instead of two )

I fixed that and it worked for me.

1 reply

Reply 3 years ago

Yes, my bad. I've changed it.

Thanks for letting me know.