Introduction: Arduino Android Bluetooth Car

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Step 5: Android Code

Picture of 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.

Comments

sanjith.tt (author)2016-03-30

can i use l298md.plzz help me fast

Souvikavi (author)2016-03-24

how to download the app.. link please

GrapesP (author)2015-12-21

Hello, is there any other way to make this without using the SainSmart L298N Dual H Bridge Stepper Motor Driver Board For Arduino Robot?

mohamed4data (author)GrapesP2016-03-21

You can use a transistor H-breidge. . search for that in Google.

olivasvince (author)GrapesP2016-01-09

You could probably use an arduino shield or a different h bridge(:

TanmayM1 (author)2015-10-23

what is the name of the app

TanmayM1 (author)2015-10-23

what is the name to be downloaded

Thiago C (author)2015-08-24

hello , will be who can help me I can not install the program on my android

Thiago C (author)2015-08-24

ela, sera quepod me ajudar n consigo instalar o programa em meu android


smandal13 (author)2015-08-15

cool!

Slk_Stephane (author)2015-08-09

My l298n gets really hot and is there anything wrong with it.

I am using 6v 4.0Ah/20Hr

Also something is wrong with the code.

sketch_aug09a.ino:1:10: error: #include expects "FILENAME" or <FILENAME>

sketch_aug09a.ino:3:9: error: expected constructor, destructor, or type conversion before '(' token

sketch_aug09a.ino: In function 'void setup()':

sketch_aug09a.ino:39:1: error: 'mySerial' was not declared in this scope

sketch_aug09a.ino: In function 'void loop()':

sketch_aug09a.ino:67:8: error: 'mySerial' was not declared in this scope

Error compiling.

Samuel_Alexander (author)2015-06-17

Nice instructables!

amberrayh (author)2015-06-04

Oh, do you have a video of it working?

BenAlper (author)amberrayh2015-06-05

Yes there's a video in the introduction.

amberrayh (author)BenAlper2015-06-05

I can't download a file that big on my phone. Can you embed the video?

BenAlper (author)amberrayh2015-06-06

Ok, I've embedded it as a youtube video.

amberrayh (author)BenAlper2015-06-07

Thank you! That's really cool how it drives :)

amberrayh (author)2015-06-04

This sounds so fun! Thank you for sharing your project. I hope we see more from you in the future!

About This Instructable

20,357views

246favorites

License:

More by BenAlper:Arduino Android Bluetooth Car
Add instructable to: