When a group of engineers at Ericsson invented in 1994 the Bluetooth technology, probably no one could not have imagined the impact on connecting people and things. They don’t imagine that makes happy millions of makers. They don’t even know how happy the roboticists are. Anyone can use the technology to build a robot controlled at a touch of a button while nobody worries about wires.
Like many of you, I want to prototype things. Robots that make me happy. One of them is a remote controlled tank robot.
Let’s start to learn how to use the Bluetooth technology to control the speed and direction of a tank robot platform at a touch of a button.
The hardware parts
The key part of this project is the Bluetooth module. Since I use an Arduino UNO, I need a Bluetooth module Arduino compatible. A few months ago, I wrote an article about the Bluetooth modules Arduino compatible with a range of several meters.
Since I have to be in a proximity area to control the robot, a Bluetooth module with a range of 5 meters is enough. I don’t have large rooms in my apartment, so any of these wireless modules can reach this range.
For this project, I use an HC-06 Bluetooth module. This is a slave module that works perfectly with Arduino UNO.
Next, I have to focus on the mobile platform.
The robot tank is a hacked TS – 50 Mini Bluetooth Tank Robot Smart Car Kit. I remove the original Bluetooth module (a Keyestudio Bluetooth module) and the servo motor with the ultrasonic sensor. I don’t need these parts anymore.
The motor driver is a Keyestudio shield with an L298P H-Bridge. You can use any driver capable of feeding the DC motors.
Most motor drivers use two pins for directions of a DC motor. The Keyestudio motor drive shield uses one pin for the direction of the motor.
If you will use a motor driver with two pins to set the direction of a DC motor, you have to add two more lines in the Arduino sketch.
My Arduino code is like this:
C++
1
2
3
4
5
|
analogWrite(speedPinA, 255);
analogWrite(speedPinB, 255);
digitalWrite(dirPinA, HIGH);
digitalWrite(dirPinB, LOW);
Serial.println(“Robot Tank Forward”);
|
The code for two pins to set the direction of a DC motor should be like this:
C++
1
2
3
4
5
6
7
|
analogWrite(speedPinA, 255);
analogWrite(speedPinB, 255);
digitalWrite(dir1PinA, HIGH);
digitalWrite(dir2PinA, LOW);//one more line for motor 1
digitalWrite(dir1PinB, HIGH); //one more line for motor 2
digitalWrite(dir2PinB, LOW);
Serial.println(“Robot Tank Forward”);
|
The kit has two rechargeable 3.7V Li-Ion batteries. These two batteries can feed the DC motors, the motor driver, the Arduino board and the HC-06 Bluetooth module.
How to connect the Bluetooth module and Arduino
The motor driver shield doesn’t complicate the connection between Arduino UNO and HC-06. I use four male-to-male jumper wires to setting up the hardware.
I connect the VCC pin to the 3.3V output on the Arduino board, the GND pin to a GND pin of the Arduino, the TX pin of the Bluetooth module to pin 9 of Arduino UNO, and the RX pin of HC-06 to pin 10 of the Arduino board.
This is how the connection looks:
The Android application
The remote control is an Android smartphone. The commands that I sent from the smartphone is usually done via an Android application. The application is designed to enable the Bluetooth features of the device and make the connection with the Bluetooth module attached to the robot.
I use the same Android application that I used to control a servo motor with a Bluetooth module, Arduino, and Android. It is called Arduino Bluetooth Controller.
You can download the Android application for free. Open the application, scan for Bluetooth devices, enter the PIN number of the Bluetooth module and make the connection between smartphone and robot.
For this tutorial, I use in application the “Controller Mode” for commands.
I set up the application to send commands like “1” to move forward, “2” to move backward, “3” to move left, and “4” to move the robot tank to the right.
The Arduino sketch
The Arduino sketch is simple and uses four blocks to control forward, backward, left and right the robot tank. The default case is to stop the robot.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/**
* @file Bluetooth Controlled Robot Using Arduino and Android
* @author Calin Dragos for intorobotics.com
* @version V1.0
* @date 19.12.2016
* @description This is an Arduino sketch to control a robot tank with the Bluetooth module HC-06 and Arduino UNO
*/
#include “SoftwareSerial.h”
SoftwareSerial mySerial(9, 10); // RX | TX
char command;
// Motor 1
int dirPinA = 12;
int speedPinA = 3; // Needs to be a PWM pin to be able to control motor speed
// Motor 2
int dirPinB = 13;
int speedPinB = 11;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println(“You’re connected via Bluetooth”);
pinMode(dirPinA,OUTPUT);
pinMode(speedPinA,OUTPUT);
pinMode(dirPinB,OUTPUT);
pinMode(speedPinB,OUTPUT);
}
void loop() {
if (mySerial.available())
{
command=(mySerial.read());
switch (command) {
case ‘1’: // Robot Tank Forward
analogWrite(speedPinA, 255);
analogWrite(speedPinB, 255);
digitalWrite(dirPinA, HIGH);
digitalWrite(dirPinB, LOW);
Serial.println(“Robot Tank Forward”);
break;
case ‘2’: // Robot Tank Backward
analogWrite(speedPinA, 255);
analogWrite(speedPinB, 255);
digitalWrite(dirPinA, LOW);
digitalWrite(dirPinB, HIGH);
Serial.println(“Robot Tank Backward”);
break;
case ‘3’: // Robot Tank Left
analogWrite(speedPinA, 255);
analogWrite(speedPinB, 255);
digitalWrite(dirPinA, LOW);
digitalWrite(dirPinB, LOW);
Serial.println(“Robot Tank Left”);
break;
case ‘4’: // Robot Tank Right
analogWrite(speedPinA, 255);
analogWrite(speedPinB, 255);
digitalWrite(dirPinA, HIGH);
digitalWrite(dirPinB, HIGH);
Serial.println(“Robot Tank Right”);
break;
default: //Stop The Robot Tank
analogWrite(speedPinA, 0);
analogWrite(speedPinB, 0);
digitalWrite(dirPinA, LOW);
digitalWrite(dirPinB, LOW);
Serial.println(“Robot Tank Stop”);
}
}
}
|