Overview
Bluetooth is a wireless technology capable of transmitting and receiving data over short distances. This technological platform is utilized on mobile phones, laptops, and other devices. This project uses Bluetooth to control a robot car.
In this project, the AlphaBot 2-Ar is controlled by an Android application. This mobile robot development platform can be programmed using the Arduino Uno.
Hardware Used
You can buy all this Hardware at Createlabz.
Software Used
- Arduino IDE
- Android GamePad Application – click to download
Libraries Used
- SoftwareSerial.h
Application Description
This project uses Bluetooth to control a mobile robot platform. The robot moves forward, backward, left, or right. Also, there is a button which halts the robot when running. Lastly, the robot’s speed can be modified using the three buttons of the gamepad. The three buttons are designated as low speed, medium speed, or high speed.
Set-up The Hardware
AlphaBot Schematics
The AlphaBot 2-Ar contains two major parts. These are the chassis and the adapter board.
The parts contained on the chassis are shown below.
- Ultrasonic module interface
- AlphaBot2 control interface: for connecting sorts of controller adapter board
- Obstacle avoiding indicators
- Omni-direction wheel
- ST188: reflective infrared photoelectric sensor, for obstacle avoiding
- ITR20001/T: reflective infrared photoelectric sensor, for line tracking
- Potentiometer for adjusting obstacle avoiding range
- TB6612FNG dual H-bridge motor driver
- LM393 voltage comparator
- N20 micro gear motor reduction rate 1:30, 6V/600RPM
- Rubber wheels diameter 42mm, width 19mm
- Power switch
- Battery holder: supports 14500 batteries
- WS2812B: true color RGB LEDs
- Power indicator
On the other hand, the parts on the AlphaBot adapter board are shown below.
- AlphaBot2 control interface: for connecting AlphaBot2-Base
- Arduino expansion header: for connecting Arduino shields
- Arduino interface: for connecting Arduino compatible controller
- Xbee connector: for connecting dual-mode Bluetooth module, remotely control the robot via Bluetooth
- IR receiver
- PC8574: I/O expander, SPI interface
- Arduino peripheral jumpers
- TLC1543: 10-bit AD acquisition chip
- Buzzer
- 0.96inch OLED SSD1306 driver, 128×64 resolution
- Joystick
HC-05 Assembly Diagram
The image shown above is the connections for the HC-05 Bluetooth module. The VCC and GND pins of the module are connected to the VCC and GND pins of the Arduino Uno.
Furthermore, the TX pin of the HC-05 module is connected to the pin 2 of the Arduino. Also, there is a resistor configuration in this schematic diagram. The resistors in series form a voltage-divider circuit, wherein the 5 volts from the Arduino is divided into three equal parts. The circuit was created since the RX pin of the HC-05 Bluetooth module can handle 3.3 volts only. Establishing 5 volts into the RX pin of the Bluetooth module puts it at risk of damage.
Set-up The Software
Code Breakdown
#include <SoftwareSerial.h> SoftwareSerial BT(2, 7); #define PWMA 6 //Left Motor Speed pin (ENA) #define AIN2 A0 //Motor-L forward (IN2). #define AIN1 A1 //Motor-L backward (IN1) #define PWMB 5 //Right Motor Speed pin (ENB) #define BIN1 A2 //Motor-R forward (IN3) #define BIN2 A3 //Motor-R backward (IN4) String str = ""; int Speed = 50; int speedturn = 20; void setup() { // put your setup code here, to run once: Serial.begin(9600); BT.begin(9600); pinMode(PWMA, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(PWMB, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); } void loop() { // put your main code here, to run repeatedly: while (BT.available()) { delay(20); char c = BT.read(); str += c; } if (str.length() > 0) { Serial.println(str); if (str == "u") { analogWrite(PWMA, Speed); analogWrite(PWMB, Speed); digitalWrite(AIN1, LOW); digitalWrite(AIN2, HIGH); digitalWrite(BIN1, LOW); digitalWrite(BIN2, HIGH); str = ""; } else if (str == "d") { analogWrite(PWMA, Speed); analogWrite(PWMB, Speed); digitalWrite(AIN1, HIGH); digitalWrite(AIN2, LOW); digitalWrite(BIN1, HIGH); digitalWrite(BIN2, LOW); str = ""; } else if (str == "l") { analogWrite(PWMA, speedturn); analogWrite(PWMB, speedturn); digitalWrite(AIN1, HIGH); digitalWrite(AIN2, LOW); digitalWrite(BIN1, LOW); digitalWrite(BIN2, HIGH); str = ""; } else if (str == "r") { analogWrite(PWMA, speedturn); analogWrite(PWMB, speedturn); digitalWrite(AIN1, LOW); digitalWrite(AIN2, HIGH); digitalWrite(BIN1, HIGH); digitalWrite(BIN2, LOW); str = ""; } else if (str == "s") { analogWrite(PWMA, 0); analogWrite(PWMB, 0); digitalWrite(AIN1, LOW); digitalWrite(AIN2, LOW); digitalWrite(BIN1, LOW); digitalWrite(BIN2, LOW); str = ""; } else if (str == "low") { Speed = 50; speedturn = 20; str = ""; } else if (str == "mid") { Speed = 120; speedturn = 50; str = ""; } else if (str == "hi") { Speed = 190; speedturn = 80; str = ""; } } }
The snippet shown above is the code needed for this project. The code is divided into three major parts. These are the initialization of variables, the setup function, and the loop function. Each part will be explained thoroughly below.
#include <SoftwareSerial.h> SoftwareSerial BT(2, 7); #define PWMA 6 //Left Motor Speed pin (ENA) #define AIN2 A0 //Motor-L forward (IN2). #define AIN1 A1 //Motor-L backward (IN1) #define PWMB 5 //Right Motor Speed pin (ENB) #define BIN1 A2 //Motor-R forward (IN3) #define BIN2 A3 //Motor-R backward (IN4) String str = ""; int Speed = 50; int speedturn = 20;
The first major part of the code is the initialization of variables. This is where the variables used in the code is first given a value and/or a data type. Also, this is where the library SoftwareSerial is included in the code. The SoftwareSerial library is used to incorporate serial communication onto the different pins of the Arduino Uno rather than using pins 0 and 1 for serial communication. Serial communication is a type of communication wherein data is transmitted and received using a communication channel one bit at a time.
To use the different pins of the Arduino Uno as the RX and TX pins of the serial communication, the Arduino pins must be declared. The second line of the code shown above states pins 2 and 7 as the RX and TX pins, respectively.
Furthermore, the pins for the different motors of the AlphaBot are defined. These pins are used to control the different motors of the mobile robot. This allows the robot to move in any direction and change to different speeds, depending on the program,
void setup() { // put your setup code here, to run once: Serial.begin(9600); BT.begin(9600); pinMode(PWMA, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(PWMB, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); }
The second major part of the code is the setup function. This is where the different Arduino pins needed are assigned as either inputs or outputs. Also, this is where the baud rate is defined in both the hardware and software serial communication. Baud rate is defined as the rate of transfer of information in a communication channel. In this project, a baud rate of 9600 is used. This means that the serial port is capable of sending and receiving a maximum of 9600 bits per second.
void loop() { // put your main code here, to run repeatedly: while (BT.available()) { delay(20); char c = BT.read(); str += c; } if (str.length() > 0) { Serial.println(str); if (str == "u") { analogWrite(PWMA, Speed); analogWrite(PWMB, Speed); digitalWrite(AIN1, LOW); digitalWrite(AIN2, HIGH); digitalWrite(BIN1, LOW); digitalWrite(BIN2, HIGH); str = ""; } else if (str == "d") { analogWrite(PWMA, Speed); analogWrite(PWMB, Speed); digitalWrite(AIN1, HIGH); digitalWrite(AIN2, LOW); digitalWrite(BIN1, HIGH); digitalWrite(BIN2, LOW); str = ""; } else if (str == "l") { analogWrite(PWMA, speedturn); analogWrite(PWMB, speedturn); digitalWrite(AIN1, HIGH); digitalWrite(AIN2, LOW); digitalWrite(BIN1, LOW); digitalWrite(BIN2, HIGH); str = ""; } else if (str == "r") { analogWrite(PWMA, speedturn); analogWrite(PWMB, speedturn); digitalWrite(AIN1, LOW); digitalWrite(AIN2, HIGH); digitalWrite(BIN1, HIGH); digitalWrite(BIN2, LOW); str = ""; } else if (str == "s") { analogWrite(PWMA, 0); analogWrite(PWMB, 0); digitalWrite(AIN1, LOW); digitalWrite(AIN2, LOW); digitalWrite(BIN1, LOW); digitalWrite(BIN2, LOW); str = ""; } else if (str == "low") { Speed = 50; speedturn = 20; str = ""; } else if (str == "mid") { Speed = 120; speedturn = 50; str = ""; } else if (str == "hi") { Speed = 190; speedturn = 80; str = ""; } } }
The third and final major part of the code is the loop function. This is where the Arduino does its processes and repeats it continuously. The first section of the loop function is the acquisition of data from the mobile app. In this section, the Arduino reads the string that was sent by the mobile app.
After reading the string, a system of if-else statements was used to compare the string to different parameters. Once the string and the given parameter are equal, the Arduino performs the function. This concept is very familiar to programming, as it uses if-else statements to execute different lines of code for every situation.
Android Application
There are different Android applications found on the Google Play Store which allows its users to control different modules and kits using Bluetooth. The download link for the Android application was shown in the earlier parts of this tutorial.
After installing the application from the Play Store, launch the app. Once launched, a pop-up will appear and asks to allow itself to turn on Bluetooth. Click Yes to allow.
After allowing the app to turn on Bluetooth, a list appears containing different available Bluetooth devices. In this project, the HC-05 Bluetooth module was utilized.
Next, a pop-up appears when the HC-05 was chosen as the Bluetooth module to connect to. Since the project aims to control the mobile robot platform via Bluetooth, click Controller mode.
A gamepad screen appears after selecting controller mode. To activate this gamepad, click the gear button at the right-hand side.
The Settings tab of the gamepad displays its buttons and its corresponding strings. These strings are what the Bluetooth module receives from the application. It was explained earlier that the Arduino uses if-else statements to compare the acquired string and the different parameters of the if-else statements.
Robot Control
The image shown above is the controls for the robot car. It is imperative to know that the robot car moves until the brake button is pressed, as shown in the featured image. The speed of the robot can be changed through the three buttons shown above. The user may change the speed allotted for low, medium, and high speeds in the code.
References
- https://www.waveshare.com/wiki/AlphaBot2-Ar
- https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-05-bluetooth-module-tutorial/
The post Bluetooth-Controlled Smart Robot Car (AlphaBot 2) using HC-05 Bluetooth Module appeared first on CreateLabz.