You are on page 1of 7

Food Living Outside Play Technology Workshop

Andruino: A Simple 2-Way Bluetooth-based Android Controller for Arduino


by techbitar on July 3, 2013

Table of Contents

Andruino: A Simple 2-Way Bluetooth-based Android Controller for Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: Andruino: A Simple 2-Way Bluetooth-based Android Controller for Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1: How to use Andruino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 2: Andruino components & downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 3: Wiring the Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 4: How to modify the Arduino companion program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 5: Control analogWrite pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Step 6: Control digitalWrite pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Step 7: Send text/command from Android to Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Step 8: Get text/command from Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Step 9: Things to consider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/
Author:techbitar
Did I unplug the solder iron?

Intro: Andruino: A Simple 2-Way Bluetooth-based Android Controller for Arduino


ANDRUINO is a simple tool to help you control your Arduino (or clone) from your Android phone. It's both an Android app and an Arduino program. Andruino has a
simple Android user interface to 1) control Arduino's digital and PWM pins 2) send text commands to Arduino 3) and receive data from Arduino over Bluetooth using the
ever popular HC-05 Bluetooth over serial module or its siblings.

Andruino should work with other Bluetooth modules with some tweaking but I have only tested it with the HC-05. This is an alpha version that's running fine on my
Samsung Galaxy S2 Plus. Please share your experience running Andruino on your phone.

I have published a guide before on building and programming a circuit with the HC-05 Bluetooth module and Arduino but I will briefly describe again in this guide how to
wire the circuit using a breadboard and jumper wires.

Project Background

Andruino is an app that evolved during one of my development projects. I am sharing it as an alpha release for those who can make use of it. It's by no means a refined
product. It has been tested and works on a Samsung Galaxy S2+ running Android 4.1.2 and G Nexus 4 running Android 4.2.2 but it was written for Android 2.33 and
higher.

http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/
Step 1: How to use Andruino
You can use Andruino to send commands to Arduino to control a relay to turn electric gadgets on/off, control a robot servo, increase/decrease speed of a motor and
reverse direction, dim a light, speed up a fan, and so on. Furthermore, you can read Arduino pins and sensors then transmit the data back to your android phone.

Step 2: Andruino components & downloads


Software

The Android app: Andruino (0.1 Alpha)

I will move the Andruino app to Google Play once I feel it's ready.

Certificate fingerprints for Anrduino.apk


MD5 : BA:80:C2:6A:68:31:8F:21:D6:FC:08:8E:09:D8:F5:CF
SHA1: 04:EA:2E:47:80:71:BE:D9:D2:ED:86:5F:15:1F:1E:9E:77:62:DB:85
The Arduino program: andruino.ino
Arduino IDE 1.05 or later

Hardware

Android device running version 2.3.3 or higher with Bluetooth.


Arduino Uno or clone.
CD4050 level shifter IC or 2K Ohms & 1K Ohms resistors as voltage dividers.
HC-05 Bluetooth module. Other Bluetooth over serial modules should work with some modifications.
Breadboard & jumper wires.
Power source.

http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/
Step 3: Wiring the Circuit
Please note that while my actual prototype as shown in the video and photo uses the CD4050 IC to level shift from 5V to 3.3V, in the wiring diagram I am using a voltage
divider because I believe it will be simpler for most people to acquire the resistors than the IC. You only need to drop the Arduino's TX voltage to 3.3V to match the RX of
the HC-05. The Arduino's RX pin can handle the HC-05 incoming TX signal which is 3.3V.

I used 2K and 1K ohms resistors in my circuit diagram to drop 5V to 3.3V but you can use different resistor values. Google "voltage divider calculator" and use the myriad
calculators to determine what other resistor values work best for you.

Step 4: How to modify the Arduino companion program


On the Arduino side I have included a skeletal program (andruino.ino) that intercepts and processes the Andruino commands issued from your Android device. You can
update the Arduino code according to your project needs.

Andruino is both an Android app that sends/receives data from Arduino with the help of an Arduino sketch named Andruino.ino In this sketch, there are four code blocks
supporting the four key functions of Andruino. You can modify these code blocks to suit your requirements.

They are labeled according to their function in a comment line at the start of each code block. I inserted a comment "// add your code here" to help you find and place
your code, but you can decide how you wish to modify the functional code blocks.

http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/
Step 5: Control analogWrite pins
This is the code block that receives the PWM value from 0 to 255 that you send from Android to control the PWM designated Arduino Uno pins 11,9,10,5,4,3. You can
add code to control a motor for a robot or fan, for example, to speed it up or slow it down. Or you can send a command to dim a light.

// 3) GET analogWrite DATA FROM ANDRUINO


if (ard_command == CMD_ANALOGWRITE) {
analogWrite( pin_num, pin_value );
// add your code here
return; // Done. return to loop();
}

Step 6: Control digitalWrite pins


This is the code block that receives from Android the pin number and the ON/OFF (HIGH/LOW) value to toggle the corresponding pin. You can turn a relay on /off to
switch an electrical device. Please note that this code block calls a function set_digitalwrite() which has a Switch/Case structure to give you room to write code to act on
each pin separately.

// 2) GET digitalWrite DATA FROM ANDRUINO


if (ard_command == CMD_DIGITALWRITE){
if (pin_value == PIN_LOW) pin_value = LOW;
else if (pin_value == PIN_HIGH) pin_value = HIGH;
else return; // error in pin value. return.
set_digitalwrite( pin_num, pin_value); // Uncomment this function if you wish to use
return; // return from start of loop()
}

// 2a) select the requested pin# for DigitalWrite action


void set_digitalwrite(int pin_num, int pin_value)
{
switch (pin_num) {
case 13:
pinMode(13, OUTPUT);
digitalWrite(13, pin_value);
// add your code here
break;
...
...

...

case 2: pinMode(2, OUTPUT);


digitalWrite(2, pin_value);
// add your code here
break;
// default:
// if nothing else matches, do the default
// default is optional
}}

http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/
Step 7: Send text/command from Android to Arduino
This is the code block that handles the text you type in the field next to Send Data button. For example, you can send a password to activate/login into a system. Or you
can send text to an LCD connected to Arduino to display something.

// 1) GET TEXT COMMAND FROM ANDRUINO


if (ard_command == CMD_TEXT){
inText =""; //clears variable for new input
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
delay(5);
if (c == END_CMD_CHAR) { // if we the complete string has been read
// add your code here
break;
}
else {
if (c != DIV_CMD_CHAR) {
inText += c;
delay(5);
}}}}

Step 8: Get text/command from Arduino


This code block sends anything you want from Arduino to your Android. In my sample code below, I send the value of analog pin 0 whenever I press the Get Data button.
Since analog pin is not connected to anything in the demo video, I was getting ambient noise values. Please note that there is no error detection/correction in my code
block. If accuracy is critical to your app, make sure you add error detection such as visual markers to enclose the data being sent or a checksum.

// 4) SEND DATA TO ANDRUINO


if (ard_command == CMD_READ_ARDUINO) {
// char send_to_android[] = "Place your text here." ;
// Serial.println(send_to_android); // Example: Sending text
Serial.print(" Analog 0 = ");
Serial.println(analogRead(A0)); // Example: Read and send Analog pin value to Arduino
return; // Done. return to loop();
}}

Step 9: Things to consider


Alternatives to Andruino

There are a quite a few solutions to enable Android-Arduino integration. The more expensive and complex ones involve specialized and costly hardware such as
Google's ADK, but by far the cheapest and in my opinion the simplest is the Bluetooth integration using the HC-05 transceiver. I expect WiFi to eventually replace
Bluetooth with the help of WiFi Direct (point to point WiFi communications without a router) but his is a couple of years down the road.

As for Bluetooth-based solution for Android to Arduino communication, you can find a few approaches to pick from depending on your project needs and the degree of
abstraction you are looking for. Some approaches use a terminal app on the Android such as Blueterm to send and receive raw text data from Arduino over Bluetooth
SPP. Others solutions employ a comprehensive library such as Amarino . My Andruino sits in the middle.

Notes on support and updates

I will do my best to support Andruino and I am hoping that those of you who find this tool useful to share your expertise with other users. Please let me know if you run
into any bugs or problems running Andruino on your device. Kindly note the device model and OS version as well as other useful info. I am considering a follow up
release of Andruino with smarter features. It all depends if there's a need for something like this.

You can drop me a note via the contact form or email techbitar at gmail dot com.
http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/
Related Instructables

Android talks to Control


Cheap 2-Way Keyboard & iAndroidRemote
Arduino by Androino!
Bluetooth Mouse w/ Arduino - Control
Connection circuit_breaker Controlled/Voice Android mobile Control an
Android app via
Between TV Remote by using an Apple Arduino from
Arduino by your Android
Arduino and PC jordantallent Remote by sudar
plowdk51 device using a
by techbitar
cheap bluetooth
module. by
metanurb

Advertisements

Comments
1 comments Add Comment

jptrsn says: Jul 3, 2013. 4:54 PM REPLY


Very cool! I will look to use this on a future project (once stage 1 is completed and functioning). If and when I do, expect some feedback as well as a huge
amount of gratitude! Favourited!

http://www.instructables.com/id/Andruino-A-Simple-2-Way-Bluetooth-based-Android-C/

You might also like