Android Controlled Arduino Robot Car: Jie Hou
Android Controlled Arduino Robot Car: Jie Hou
Robot Car
JIE HOU
Table of Contents
1 Introduction ........................................................................................................................................ 2
1.1 Arduino Uno Board ....................................................................................................................... 2
1.2 Arduino IDE .................................................................................................................................. 3
2 Controlling DC Motors ......................................................................................................................... 4
3 Bluetooth Module - HC-06 .................................................................................................................. 9
4 Using Android to Control Arduino ..................................................................................................... 13
5 Final Goal – Arduino Bluetooth Car ................................................................................................... 18
1
1 Introduction
In this post I will write my hands-on experiences about how to use an Android phone to control a robot
car, which is based on an Arduino Uno board. After a couple of weeks I assembled a robot car and
programmed a very simple App that can send commands to the Arduino with the help of Bluetooth.
Let’s dive in!
Hardware Requirements:
After some googling I found a good choice from the DX website. Actually these components can be
ordered on some other websites ex. Sparkfun, Adafruit or Amazon.
Software Requirements:
The complete project will be explained step by step: in the first part I will introduce Arduino very shortly.
L298N Motor Driver Shield will be explained in the second part. Next I will describe how to use our
Bluetooth module. After we know how to program Motor and Bluetooth, we can put them together
to achieve the final goal.
Atmel microcontroller
USB programming interfaces
Voltage regulator and power connections
I/O pins
Power and auxiliary pins
As Figure 1.1 shows, it has 14 digital input/output pins (of which 6 can be used as PWM outputs), which
are located on the top row of Arduino Uno board. These tend to be used for reading on or off values,
or switching actuators on or off. On the bottom left row, you can find two power pins: 3.3V or 5V
supply. This is really useful, because many electrical components need 3.3V or 5V. Some pins labeled
“GND” on the board are ground pins. On the bottom right row, there are 6 analog input pins labeled
from A0 to A5. They can be used to measure things with a range of possible values, for example
temperatures, moisture and so on.
2
Figure 1.1: Arduino Uno Components
3
2 Controlling DC Motors
DC motors are widely used in numerous devices (e.g. DVD player) around your home. They rotate
continuously when a DC voltage is applied across them. By adjusting the voltage you apply to them,
you can change their rotation speed. Furthermore, by reversing the dire direction of the voltage
applied to them, you can change direction of rotation. H-bridge is a useful module for controlling DC
motors. The operation of an H-bridge can best be explained with the following diagram (see Figure
2.1).
As you can see, the H-bridge has four states: open, forward, backward and breaking. In the open state,
all the switches are open and the motor won’t spin. In the forward state, two diagonally opposing
switches are closed, causing a positive voltage through the motor. On the contrary, in the backward
state this voltage is reversed, which leads to reverse operation of the motor. If the H-bridge is put in
the braking state, all residual motion caused by momentum is ceased, and the motor stops.
For this robot car I use a Dual H-bridge motor diver shield, which is based on H-bridge driver chip L298N.
As the Figure 2.2 displays, this motor driver can let us drive two independent DC motors, controlling
the speed and the direction of each one.
On the hardware there are two motor connectors, which are labeled Motor A and Motor B. On this
board it did not tell me which one is the positive or GND terminal, so I just hook it up the same way for
both. In this picture, red is the positive end and black is the GND end for the motors.
In the middle of this board’s right side, there are three pins: Vms, GND, and 5V. Vms and GND are used
to connect external power source, which can be in the range from 6V to 35V. 5V pin can provide 5V
voltage output, which can be used to power some other sensors, e.g. an ultrasonic sensor.
4
IN4 (Input): Motor B Direction 2 pin
ENB (Input): 5V enable Motor B, 0V disable Motor B
GND: Ground
5V (Input): power for this driver board
Note: you may wondering what is Direction 1 and Direction 2. Direction 1 can be forward or backward,
it depend on how you connect to your motor. It is better to get your own hands dirty. Do some
experiments, which bring you some exciting experience.
Now I will show you a small demo. The following table describes how I bind Arduino with L298N.
void setup()
{
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Enable Motor A, Motor B: Constant Speed
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
// Serial communication
Serial.begin(9600);
}
void loop()
{
Serial.println("Motor A & B: Direction 1");
MotorAB_Direction1(1000);
Serial.println("Motor A & B: Brake");
MotorAB_Brake(1000);
Serial.println("Motor A & B: Direction 2");
MotorAB_Direction2(1000);
Serial.println("Motor A & B: Brake");
MotorAB_Brake(1000);
}
6
digitalWrite(IN4, LOW);
if (milliseconds > 0)
delay(milliseconds);
}
Until now I have not explained how to control speed of DC motor. Normally this can be achieved
through PWM (Pulse Width Modulation). What is PWM? The basic idea of PWM is to create a digital
output wave of fixed frequency, but allow the microcontroller to vary its duty cycle. The system is
designed in such a way that (HIGH+LOW) is constant, which means the frequency is fixed. The duty
cycle is defined as the fraction of time the signal is high: duty cycle = HIGH / (HIGH + LOW). Some PWM
signals with different duty cycles are shown in Figure 2.3.
Figure 2.3 PWM signals with varying duty cycles (Source: Exploring Arduino)
On Arduino Uno board Pins 3, 5, 6, 9, 10 and 11 can be used as PWM pins. You can recognize them
easily, because they are marked with ~ on the board. Furthermore, Arduino provide you a function
named analogWrite() to generate PWM signals. The PWM output is an 8-bit value. In other words, you
7
can write values from 0 to 255. Writing a value of 0 with analogWrite() indicates a square wave with a
duty cycle of 0 percent (always low). On the contrary, writing a value of 255 indicates a square wave
with duty cycle of 100 percent (always high).
I also made a small demo about how to control speed based on the above code. And I have upload
these two examples to Github. If you have interest, please jump to this link:
https://github.com/jiehou/ArduinoRobotCar.
8
3 Bluetooth Module - HC-06
It is totally awesome that you are able to control your Arduino using Bluetooth. With help of HC-06
serial port Bluetooth module, this task can be easily achieved. It provides a wireless communication
link between Arduino and any Bluetooth capable device. In this post the following HC-06 is used, which
is shown in Figure 3.1.
As Figure 3.1 shows, it has four pins: VCC, GND, TX and RX. It is not difficult to connect it to Arduino.
The following table describes how to map these pins to Arduino Uno:
Now we have finished connecting HC-06 to Arduino, it is time to test it. In this tutorial I will show you
how to test HC-06 using your computer. It is assumed that your computer has Bluetooth feature. I used
Tera Term (http://ttssh2.sourceforge.jp/index.html.en) on my laptop as the serial terminal.
After Tera Term was successfully installed on your computer, following steps help you set up
connection between your computer and your HC-06 module.
9
Figure 3.2 Paring HC-06 with Computer
10
Figure 3.3 COM Ports
If you pair HC-06 with your computer successfully, under tab (COM Ports) the detailed information
about it will be displayed. In our case, we need a COM Port, whose direction is outgoing.
Open Tera Term, then enable Serial option. After that choose your COM Port in the drop down list and
finally click OK. It will build connection between your computer and your HC-06 Bluetooth Module.
The following demo code lets you send ‘1’ to switch a LED on and ‘0’ to switch a LED off.
/**
* LED Code
* Send 1: Switch LED On
* Send 0: Switch LED Off
*/
// Pin 12: LED
const int LED = 12;
11
char mIncoming;
void setup()
{
pinMode(LED, OUTPUT);
// Set baud rate 9600
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
mIncoming = Serial.read();
Serial.println(mIncoming);
if (mIncoming == '1')
{
digitalWrite(LED, HIGH);
Serial.println("LED On");
}
else if (mIncoming == '0')
{
digitalWrite(LED, LOW);
Serial.println("LED Off");
}
}
delay(100);
}
12
4 Using Android to Control Arduino
In the previous post, I have already talked about how to use Tera Term to communication with
Bluetooth Module HC-06. Nowadays almost each smart handy has embedded Bluetooth feature. In
this post, I will discuss about how to write your own Android APP to control your Arduino project using
HC-06 Bluetooth module.
The Android platform includes support for the Bluetooth network stack, which allows a device running
Android to wirelessly exchange data with other Bluetooth devices. This application framework allows
developers to access to the Bluetooth functionality through the Android Bluetooth APIs. Using these
APIs, an Android application is capable of managing the local Bluetooth adapter, scanning for nearby
Bluetooth devices, querying the local Bluetooth adapter for paired Bluetooth devices, transferring data
between Bluetooth devices and more.
In this post I will show you my own summary about how to start with programming Android Bluetooth
API. Actually the documentation from Android is absolutely a good start point.
In order to work with Bluetooth in your application, you must declare the BLUETOOTH permission in
the AndroidManifest.xml file.
This permission enables you to perform any Bluetooth communication, such as requesting a
connection, accepting a connection and transferring data.
If you want your app to initiate device discovery or manipulate Bluetooth settings, the
BLUETOOTH_ADMIN permission is needed. Many applications needs this permission only for the
ability to discover nearby Bluetooth devices.
BluetoothAdapter is the entry point for all Bluetooth interaction. It is used as a singleton to access the
Bluetooth radio on the Android device. To get the BluetoothAdapter, call the static getDefaultAdapter()
method. If getDefaultAdapter() returns null, it means that the device does not support Bluetooth and
your application should gracefully end here.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device does not support Bluetooth;
}
If the target device supports Bluetooth, next we need to check whether Bluetooth is enabled or not. If
isEnabled() method returns false, then the Bluetooth is disabled. In order to enable it, call
startActivityForResult() with the ACTION_REQUEST_ENABLE action intent.
if (!bluetoothAdapter.isEnabled()) {
startActivityForResult(new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE), BLUETOOTH_ENABLE);
}
13
Through the BluetoothAdapter, you can find remote devices either through device discovery or by
query the list of paired devices. The following section describes how to find devices that have been
already bonded, or discover new devices using device discovery.
14
Until now we have already been able to find a nearby Bluetooth device. Next we can use the
BluetoothDevice to acquire a BluetoothSocket and initiate a connection with the discovered remote
device. When you have successfully connected these two devices, each one will have a connected
BluetoothSocket. Then they can communicate with each other. In this post, I just send some
commands from Android to HC-06 module.
Because connect(), read(byte[]) and write(byte[]) are blocking calls, it is important that a dedicated
thread should be used.
If you want to learn more about Android Bluetooth, the following links are recommended:
From my point of view, the above mentioned project helps me understand Android Bluetooth API a
lot. It is really a good starting point to learn programming Android Bluetooth.
The following figures are screenshots of my little Android APP, which is called Arduino Car Console. It
contains two activities. The first activity (as shown in Figure 4.1) is in charge of finding and listing paired
or new devices. When user clicks on a selected device, then a connection to this device will be built up.
If the connection is successfully set up, the second activity will be used to send commands to HC-06
module. From figure 4.2 we know that, this simple console can send four commands: Forward,
Backward, Left and Right. When user clicks on Forward button, a series of ‘f’ will be sent out. The
sending interval is 50 ms.
Button Command
Forward f
Backward b
Left l
Right r
I have tested this APP on my handy, which is based on Android 4.04. It works very well. Theoretically
it can be used on any Handy, which is based on Android 4.0 or above. Thanks for downloading and
using this APP. It would very nice, if you could give me some feedback.
15
Figure 4.1 Activity for devices and connection
16
Figure 4.2 Activity for sending commands
17
5 Final Goal – Arduino Bluetooth Car
In previous post, I have already explained how to send commands from an Android handy to an
Arduino using HC-06 Bluetooth module. In this post, the complete Arduino Car will be assembled,
which will be described step by step. Because HC-06 Bluetooth module was already introduced, here
it will not be discussed any more.
Step 1: L298N
18
Furthermore, we provide L298N an external 7.5V voltage. MOTORA is in charge of two right side
motors, correspondingly two left side motors are connected to MOTORB. Figure 5.2 describes how to
control directions of this robot car.
Forward Backward
Chasis Chasis
Chasis Chasis
This robot kit contains an ultrasonic distance sensor (HC-SR04), a small servo motor and a plastic
holder, which can be used as a base to hold a servo. Using these three components we can build a
sweeping distance sensor, which is shown in figure 5.3.
19
Unlike DC motors, servo motors have three pins: power (usually red), ground (usually brown or
black), and signal (usually orange).
As figure 5.4 shows, the signal pin of servo is connected to Arduino pin 11. The Arduino IDE contains
a built-in library, which makes controlling servos a breeze. All you have to do is attaching a servo
object to a particular pin and give it an angle to rotate to, which is displayed in the following code
snippet.
#include <Servo.h>
//Servo on Pin 11
const int SERVO = 11;
Servo myServo;
void setup() {
myServo.attach(SERVO);
}
void loop() {
myServo.write(90);
delay(1000);
}
The HC-SR04 ultrasonic module uses sonar to determine distance to an object, just like dolphins do.
It includes ultrasonic transmitter, receiver and control circuit. It works as follows:
20
2) This module automatically sends eight 40kHZ signal and detect whether there is a returned
pulse signal
3) If the signal returns back, time of high output I/O duration is the time from sending
ultrasonic to returning
4) Distance = (high level time * velocity of sound) / 2
The following table describes how this module is connected to Arduino Uno in my case:
To sum up, the following table list the detailed pin assignments for all modules, which are used on this
robot car.
Following figures draw the flowchart of the program that is run by this robot car. The complete source
code for this Android APP and Arduino robot car, and the document about this project can be found in
github. If you have interest, please visit the following link: https://github.com/jiehou/ArduinoRobotCar.
3 L298N: Source code of Demos about how to use L298N motor shield.
4 Documents: The complete document about this project and some program flowcharts.
21
Start
No
Check
Serial.aVailable()?
Yes
Forward
incomingByte == f Yes
Procedure
No
Backward
incomingByte == b Yes StopMove
Procedure
No
No
incomingByte == l Yes Left Procedure
No
22
Forward Procedure
1) stopFlag: global variable, true: Obstacle,
2) false: Safe
3) Safe Distance (Foward): 20cm
4) Safe Distance (Left and Right): 10cm
Start (Forward)
Check stopFlag?
No
Forward()
checkSafe()?
Yes
No
End
23
Backward Procedure
1) stopFlag: global variable, true:
Obstacle, false: Safe
Start (Backward)
Backward()
checkSafe()?
Yes
End
24
Left Procedure
1) stopFlag: global variable, true: Obstacle, false:
Safe
2) Left(): PWM 255
3) slowLeft(): PWM 128
4) Safe Distance (Foward): 20cm
5) Safe Distance (Left and Right): 10cm
Start (Left)
Check
Yes No
stopFlag?
slowLeft() Left()
leftSafe()? checkSafe()?
Brake() and
No set stopFlag No
as true
Yes Yes
End
25
Right Procedure
1) stopFlag: global variable, true: Obstacle, false: Safe
2) Right(): PWM 255
3) slowRight(): PWM 128
4) Safe Distance (Foward): 20cm
5) Safe Distance (Left and Right): 10cm
Start (Right)
Check
Yes stopFlag? No
slowRight() Right()
rightSafe()? checkSafe()?
Brake() and
No No
set stopFlag
as true
Yes Yes
End
26