How To Build A Robot Controlled By Your Android Smartphone

People have been fascinated by robots as far back as the 4th Century BC when Greek mathematician Archytas hypothesized about a mechanical bird named ‘The Pigeon’. Today with the advent of the internet and the proliferation of smartphones, numerous apps can be found that use the inbuilt hardware of Android smartphones to control devices such as home-built robots for cleaning floors, waterways or just for entertainment. Here we take a look at how to build a robot that can be controlled through an app on your smartphone.

Robot Parts

Initially, you will need to build your robot using common components including wheels, arms, pincers or lights and a base to fit the parts to. Along with these you will require an Arduino UNO board, an ultrasonic sensor so your robot can detect its distance from objects in a similar way to bats – either of these options should work, a Bluetooth module JY MCU BT, a 16-pin IC motor driver to control two DC motors simultaneously in all directions – a typical L293D should do the job -two 9V 100 RPM DC motors and two 9V batteries.

Building Your Android Controlled Robot

The circuit is built around the Arduino UNO board and uses two batteries – one for the motors and the other for the Arduino board. The Arduino board will provide a regulated 5V power supply to the rest of the circuit, that supply is indicated by an LED on the board.

The Arduino board is operated with a 16MHz crystal oscillator that is designed to handle off-chip crystals with a frequency of 4 – 16MHz. The board also contains everything it needs to support the microcontroller contained within it.

Pins eight and nine of the Arduino board should connect to the Bluetooth module, ten and eleven to the ultrasonic module and pins two through to seven should connect to motors one and two, along with the supporting battery for those motors.

Circuit Diagram:

Creating The Software To Control Your Robot

The Arduino Uno board is relatively simple to use. It needs to be connected to a battery, powered with an AC/DC adaptor, or connected to a computer with a USB cable to start. The board is programmed with Arduino language within the Arduino open-source development environment.

Software for the Arduino board can be loaded using the boot loader or an external hardware programmer using STK500 protocol for communication. However,the boot loader program is a quick and easy option.

There are a number of choices for creating the software needed to control your robot. Instructions for building a robot controlling app for your smartphone are easy to find on the internet and MIT’s App Inventor has some great tutorials. Once done, this can be downloaded from the link provided or installing the APK file.

Source Code:



// RX is digital pin 10 (connect to TX of other device)
//TX is digital pin 11 (connect to RX of other device)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(8,9);// RX, TX
const int triggerpin=10;//connect trigger pin of  ultrasonic sensor to 10th pin of arduino uno
const int echopin=11;//connect echo pin of  ultrasonic sensor to 11th pin of arduino uno
long duration;// establish variables for duration
long distanceCm;// establish variables for distance
int val;// establish variables for reading received 2 byte data
int speeds;// establish variables for reading received 2 byte data
int m1f=2;
int m1b=4;
int m2f=5;
int m2b=7;
int m1=3;
int m2=6;
void setup()
{
pinMode(m1f, OUTPUT);//set mlf pin as output pin
pinMode(m1b, OUTPUT);//set mlb pin as output pin
pinMode(m2f, OUTPUT);//set m2f pin as output pin
pinMode(m2b, OUTPUT);//set m2b pin as output pin
pinMode(triggerpin,OUTPUT);//<wbr />set trigger pin as output pin
pinMode(echopin,INPUT);//sets echo pin as input pin
mySerial.begin(57600); // set the data rate for the SoftwareSerial port
}
void loop() // run over and over
{

if (mySerial.available())//if number of bytes available to read greater than 0
{
val=mySerial.read();// read the incoming byte:
speeds=mySerial.read();// read the incoming byte:

digitalWrite(triggerpin,LOW);/<wbr />/sets trigger pin off for 2 microseconds
delayMicroseconds(2);
digitalWrite(triggerpin, HIGH);//sets trigger pin on for 10 microseconds
delayMicroseconds(10);
digitalWrite(triggerpin,LOW);/<wbr />/sets trigger pin off for 2 microseconds
delayMicroseconds(2);
duration = pulseIn(echopin, HIGH); //how much time the echo pin is high
distanceCm = microsecondsToCentimeters(<wbr />duration);// microsecondsToCentimeters function is called to convert the time into a distance


if((val==2)&&(distanceCm>8.00)<wbr />)  //if received value is 2 and distance between robot and obstacle greater than 8cm then moveforward function is called,
{                             <wbr />    //we can change the limiting distance below which robot stops.                        <wbr />
<wbr />       //if we changed to if((val==2)&&(distanceCm>10.<wbr />00)) then robot stops if obstacle is less than 10cm
moveforward(speeds,distanceCm)<wbr />;
}
else if((val==2)&&(distanceCm<8.00)<wbr />)// change here also
{                             <wbr />      //if received value is 2 and distance between robot and obstacle lessthan 8cm then stops function is called
stops(speeds,distanceCm);
}
else if(val==8)//if received value is 8 then movebackward function is called
{
movebackward(speeds,<wbr />distanceCm);
}
else if(val==4)//if received value is 4 then leftturn function is called
{
leftturn(speeds,distanceCm);
}
else if(val==6)//if received value is 6 then rightturn function is called
{
rightturn(speeds,distanceCm);
}
else if(val==0)//if received value is 0 then stopped function is called
{
stopped(speeds,distanceCm);
}
else// if data is not received then prints "press buttons or say commands" below in the "robot status" label in the app
{
mySerial.print("press buttons or say commands");
}
}

}

void moveforward(int x,long y)//x,y are passed from the function call
{
digitalWrite(m1f,LOW);
digitalWrite(m2f,LOW);
digitalWrite(m1b,HIGH);
digitalWrite(m2b,HIGH);
analogWrite(m1,x);
analogWrite(m2,x);
mySerial.println("Moving Forward");//prints "moving forward" below in the "robot status" label in the app
mySerial.print("obstacle is at nearly");//prints "obstacle is at nearly" below in the "robot status" label in the app
mySerial.print(y);//prints "y value" below in the "robot status" label in the app
mySerial.print("cm");//prints "cm" below in the "robot status" label in the app
}

void movebackward(int x,long y)//x,y are passed from the function call
{
digitalWrite(m1f,HIGH);
digitalWrite(m2f,HIGH);
digitalWrite(m1b,LOW);
digitalWrite(m2b,LOW);
analogWrite(m1, speeds);
analogWrite(m2, speeds);
mySerial.println("Moving Back");//prints "moving back" below in the "robot status" label in the app
mySerial.print("obstacle is at nearly");//prints "obstacle is at nearly" below in the "robot status" label in the app
mySerial.print(y);//prints "y value" below in the "robot status" label in the app
mySerial.print("cm");//prints "cm" below in the "robot status" label in the app
}
void leftturn(int x,long y)//x,y are passed from the function call
{
digitalWrite(m1f,LOW);
digitalWrite(m2f,LOW);
digitalWrite(m1b,LOW);
digitalWrite(m2b,HIGH);
analogWrite(m1,0);
analogWrite(m2,speeds);
mySerial.println("Turning left");//prints "Turning left" below in the "robot status" label in the app
mySerial.print("obstacle is at nearly");//prints "obstacle is at nearly" below in the "robot status" label in the app
mySerial.print(y);//prints "y value" below in the "robot status" label in the app
mySerial.print("cm");//prints "cm" below in the "robot status" label in the app
}
void rightturn(int x,long y)//x,y are passed from the function call
{
digitalWrite(m1f,LOW);
digitalWrite(m2f,LOW);
digitalWrite(m1b,HIGH);
digitalWrite(m2b,LOW);
analogWrite(m1,speeds);
analogWrite(m2,0);
mySerial.println("Turning Right");//prints "Turning right" below in the "robot status" label in the app
mySerial.print("obstacle is at nearly");//prints "obstacle is at nearly" below in the "robot status" label in the app
mySerial.print(y);//prints "y value" below in the "robot status" label in the app
mySerial.print("cm");//prints "cm" below in the "robot status" label in the app
}

void stopped(int x,long y)//x,y are passed from the function call
{
digitalWrite(m1f,LOW);
digitalWrite(m2f,LOW);
digitalWrite(m1b,LOW);
digitalWrite(m2b,LOW);
analogWrite(m1, 0);
analogWrite(m2,0);
mySerial.println("Stopped!!");<wbr />//prints "stopped!!" below in the "robot status" label in the app
mySerial.print("obstacle is at nearly");//prints "obstacle is at nearly" below in the "robot status" label in the app
mySerial.print(y);//prints "y value" below in the "robot status" label in the app
mySerial.print("cm");//prints "cm" below in the "robot status" label in the app
}
void stops(int x,long y)//x,y are passed from the function call
{
digitalWrite(m1f,LOW);
digitalWrite(m2f,LOW);
digitalWrite(m1b,LOW);
digitalWrite(m2b,LOW);
analogWrite(m1, 0);
analogWrite(m2,0);
mySerial.println("reached near obstacle");//prints "reached near obstacle" below in the "robot status" label in the app
mySerial.print("obstacle is at nearly");//prints "obstacle is at nearly" below in the "robot status" label in the app
mySerial.print(y);//prints "y value" below in the "robot status" label in the app
mySerial.print("cm");//prints "cm" below in the "robot status" label in the app
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Teresa Tomas writes for DO Supply, Inc., an industrial equipment supplier based in Cary, NC. Some of the topics she writes about include robotics, machine learning, and the future of automation for industries.

Related posts

Arduino Opta PLC Pros & Cons

Breakthrough of Ardino-Pro Opta: Micro-PLC with industrial IoT proficiency

Top Arduino sensors – the ultimate list 2021