Introduction: Arduino Android Bluetooth Car

This instructable describes a simple robot car controlled via bluetooth by tilting an Android phone. The accelerometers in the phone measure the angle of tilt in degrees forwards and backwards and left and right, and use this to calculate motor speeds and direction which is sent to the ardunio.

Step 1: Inventory

Inventory:

Boards

(Prices do not include delivery)

Arduino Uno - £20

L298N Dual H Bridge Motor Controller from Sainsmart, I originally bought a different version - a red one from Flux Workshop didn't work and may have been damaged - £8

HC-06 Bluetooth Module - £8

Electronics

2x 300 RPM geared 12v motors - £1.86 each

12v 1.3Ah Lead Acid Battery (the motors draw approximately 0.4 Ah each and the motor driver takes a small amount for its logic) - £11.50.

Arduino wires - £2

Ordinary 9v battery and connector from arduino to battery - £1.80

Electrical wires and crimps for connecting battery to motor driver

USB cable type A male to type B male - for connecting ardunio and PC - £3

Hardware

4 rubber tyre 50mm swivel casters from Toolstation or Screwfix - £13 for 4

2x Lynxmotion HUB-02 Universal Hub - 6mm from robot shop - £5.50

10mm and 6mm thick acrylic plastic - cut on circular saw or laser cutter or bought cut to size from plastic people

Software

Android studio software download

Arduino software download

Phone

An up to date Android 5 or similar phone is required - the Samsung S5360 Galaxy Y (£50 in 2015) uses an old 2.6 version of Android which cannot run the app and has issues connecting via usb. A Motorola moto e (£100 in 2015) with Android 5 works fine. Although the build.grade in Android Studio can be set to run on older versions this will probably cause problems due to the use of up to date accelerometer and bluetooth libraries.

Step 2: Assembling the Box

The box can be bought or made from any material. The one shown is 22cm long and 17cm wide.

Motors

The two motors need to be attached with the drive shaft sticking out the box, and the hubs are attached to these using the grub screws.

Front wheels

The rubber wheels are removed from the casters and two holes are drilled though the wheels so they can be screwed into the hubs.

Back Wheels

The casters are simply unbolted from their swivel plate and re-attached through a hole to the box.

Step 3: Wiring

There will be problems uploading programs to the arduino when the bluetooth module is plugged into pins 0 and 1 which are RX and TX. This is because the RX and TX are the main communications ports which also talk to the computer via USB. This means different pins are required and the software serializable library needs to be included in the ardunio code to let it know that pins 10 and 11 will be communication ports.

The motor driver does not need a separate power supply for the logic as the 12V 1.3Ah battery is enough to power both it and the motors.

The motor driver only has one ground socket so this has to be shared by the battery and the wire to the arduino ground.

The ENA/5V and ENB/5V pins on the motor driver are sometimes covered by jumpers. These jumpers need removing before the ardunio can be attached to the ENA and ENB pins. Nothing needs to be attached to the two 5V pins.

Step 4: Ardunio Code

//For debugging you should use the Arduino Serial Monitor to see the Serial.println("") messages sent from the ardunio while it is plugged into the computer via USB.

#include SoftwareSerial

mySerial(10, 11); // RX, TX

int led = 13;

char myChar = 'a';

String string;

char LorR;

int enA = 3;

int in1 = 4;

int in2 = 5;

int enB = 6;

int in3 = 7;

int in4 = 8;

void setup()

{

// Open serial communications and wait for port to open:

Serial.begin(57600);

pinMode(led, OUTPUT);

Serial.println("Goodnight moon!");

// set the data rate for the SoftwareSerial port

mySerial.begin(9600);

mySerial.println("Hello, world?");

pinMode(enA, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

pinMode(enB, OUTPUT);

pinMode(in3, OUTPUT);

pinMode(in4, OUTPUT);

}

void loop() // run over and over

{

string = "";

//Add to string

while(true){

myChar=mySerial.read();

if(32<=myChar && myChar<=127){

string += myChar;

}

if (myChar==':'){

break;

}

delay(10);

}// While End

//Analyse string

if (string !="off:" && string !="on:"){

if (string != "L0:" && string != "R0:") {

//code for setting motor left or right

if (string[0]=='L'){

LorR= 'L';

}else {

LorR= 'R';

}

string.remove(0, 1);

//code for putting it in reverse

if (string[0]=='-'){

string.remove(0, 1);

string.remove((string.length()-1), 1);

Serial.println(string);

if (LorR== 'L'){

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

analogWrite(enA, string.toInt());

} else{

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

analogWrite(enB, string.toInt());

}

} else {

string.remove((string.length()-1), 1);

Serial.println(string);

if (LorR== 'L'){

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

analogWrite(enA, string.toInt());

} else{

digitalWrite(in3, HIGH);

digitalWrite(in4, LOW);

analogWrite(enB, string.toInt());

}

}

} else {

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

}//End of long if statment

if(string == "on:"){

digitalWrite(led, HIGH);

}

if(string =="off:"){

digitalWrite(led, LOW);

}

}//Loop End

Attachments

Step 5: Android Code

The folder can be browsed or unzipped with winzip and imported into Android Studio. This can then be run and uploaded to the phone as an app. The zip file can also be downloaded from: http://benalper.co.uk/ardunio.php

The key files are:

Paired Devices Activity:

deviceList.java

activity_device_list.xml

LED and Motion Control Activity:

ledControl.java

activity_led_control.xml

Manifest:

AndroidManifest.xml

The Device List activity shows available bluetooth devices to connect to. Once connected it takes you to the LED and Motion Control Activity.

The led buttons send the message 'on:' or 'off:' to the arduino which switches the embedded LED at pin 13 on or off.

The motion control gets the phone's accelerometer x and y sensor data each time there is a change of measurement and uses this to calculate a speed between 0 and 255 and direction for the left and right motors. This is sent to the arduino in the format 'L-255:' or 'R100' etc. The arduino code uses this message to send the speed to ENA for the left or ENB for the right motor and change in direction by changing the polarity of IN1 and IN2 for the left motor or of IN3 and IN4 for the right motor.

Attachments