Control an Arduino With Android and USB

This instructable is just a very quick demonstration of the "Arduino USB Serial Extension" that was created by Thunkable developer, Pavitra. It's still being developed so think of this as a sort of beta version.

In a nutshell, you can now create a custom Android app and use it to communicate via USB with many different Arduino boards.

I've prepared a simple demo where the app can switch an RGB LED between 5 different states (Red, Green, Blue, White and Off) but I'm really interested to see what ideas the Instructables community comes up with.

Step 1: What You Need

Prerequisites

  1. Some basic knowledge of block-based programming, such as Scratch or App Inventor
  2. A basic understanding of Arduino - i.e you have successfully run the "Blink" sketch

Hardware

  1. An Arduino + USB Cable. I've tested this with an Uno, Leonardo and Micro.
  2. An Android Phone
  3. A USB OTG connector
  4. Some electronics parts for testing

Software

  1. The Arduino IDE
  2. Any "App Inventor 2" compatible software, I've opted for Thunkable
  3. A copy of Pavitra's Arduino USB Serial Extension

Step 2: Build Your Circuit

You could easily get away with just the Arduino itself here and use the on-board LED, but to make it a little bit more interesting I've used an RGB LED.

The circuit is very simple:

The ground pin of the LED is connected to any of the Arduino's ground pins [Black Wire],

The red, green and blue pins are connected to PWM pins via Ω resistors. You can recognise PWM pins by the ~ symbol beside the pin numbers.

The red, green and blue wires in the diagram are there simple to illustrate which leg of the LED corresponds to which colour. In some LEDs the blue and green pins might be reversed.

Step 3: Write Your Code

All of the code below can be found on Github

Start by setting up some variable names for your Arduino Pins

//variable for Serial input 
int input = 0; 

//Pins for LEDs
const int LED = 13;
const int redPin = 11;
const int greenPin = 9;
const int bluePin = 10;

Next, add this to the setup() function:

void setup() {  

//Start the serial monitor at 9600 baud  
  Serial.begin(9600);    

//Declare the LEDs to be outputs  
  pinMode(LED, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}


Then check for Serial input:

void loop() {  

//check if there's incoming data,  
if(Serial.available() > 0){    
    //if so, then read the incoming data.    
    input = Serial.read();        

    //make different colours depending on the input value    
    if(input == '1'){      
	make_colour(255,0,0);    
    }    
    else if(input == '2' ){      
	make_colour(0,255,0);
    }
    else if(input == '3' ){
      make_colour(0,0,255);
    }
    else if(input == '4' ){
      make_colour(0,0,0);
    }
    else if(input == '5' ){
      make_colour(255,255,255);
    }
  }
}

Finally, make the appropriate colour:

void make_colour(int r, int g, int b){

  //just for testing
  Serial.println("Making Colour");

  //sets the duty cycle for each pin
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
}

Step 4: Create Your App

To get started with the app as quickly as possible you can clone it directly to Thunkable by clicking here.

Design

The design of this app is intentionally basic. It's just a few buttons and, of course, the Arduino extension file.

If you're unfamiliar with installing .aix files you can read Conor's quick start guide here.


Code

In the blocks, I've created a procedure called lighsOn which is called every time a button is pressed.

The red, green and blue buttons send the characters '1', '2' and '3' to the Arduino, respectively.

The characters '4' and '5' are used to turn the LED white and off.

It's also a good idea to include a reset button that closes and then re-opens the connection to the Arduino

Step 5: Try It Out

Now you're ready to test it out.

1. Upload the sketch to your Arduino

2. Install the .apk on your Phone

3. Connect your phone to your Arduino with the OTG Cable and you're good to go.

Troubleshooting.


When you first run the app with the Arduino connected, you need to give it permission to access the USB device (the Arduino)

If you see the run time error, try closing the connection and then opening it again. This should fix your problem, but bear in mind that this is still being tested so please leave a comment if it doesn't work out for you.

Share

    Recommendations

    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest

    4 Discussions

    0
    None
    HamzaB54

    1 year ago

    didn't work and i don't have the ask for permission to access the USB device ​what is it the solution

    0
    None
    ElectrifyElectrify

    Reply 1 year ago

    I don't know why emoji turned to a "?" mark. Also I post the comments just now, but it says "13 hours ago"!