0% found this document useful (0 votes)
62 views52 pages

... For Dummy: "Controlling Your Arduino Mobile Robot Using Android Bluetooth App"

Here is the code to control the DC motor: ```c #include <Servo.h> int enA = 5; int in1 = 7; int in2 = 6; void setup() { pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); } void loop() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(enA, 255); delay(2000); digitalWrite(in1, LOW); digitalWrite(in2, LOW); delay(1000); } ```

Uploaded by

amir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views52 pages

... For Dummy: "Controlling Your Arduino Mobile Robot Using Android Bluetooth App"

Here is the code to control the DC motor: ```c #include <Servo.h> int enA = 5; int in1 = 7; int in2 = 6; void setup() { pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); } void loop() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(enA, 255); delay(2000); digitalWrite(in1, LOW); digitalWrite(in2, LOW); delay(1000); } ```

Uploaded by

amir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Arduino Bluetooth Mobile

Robot ...for dummy


“Controlling Your Arduino Mobile Robot Using Android Bluetooth App”

Version 2.0
Cytron Technologies Sdn Bhd - https://www.cytron.io
Arduino Bluetooth Mobile Robot Module Content

No TOPIC DURATION

1. Mobile Robot Introduction 10 minutes

2. Getting Started with Maker UNO 30 minutes

3. Assemble the Mobile Robot 1 hour

4. Control the DC Motor 10 minutes

5. Set the Robot Movement 20 minutes

6. Serial Communication 20 minutes

7. Bluetooth HC-05 AT Command 10 minutes

8. Arduino BT Joystick Android App 20 minutes

9. Arduino Bluetooth Mobile Robot Let’s Play! 30 minutes

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Mobile Robot Introduction
What is Mobile Robot?

Robots that can move around in a physical environment. Mobile robots are generally
controlled by software and use sensors and other gear to identify their
surroundings.

In general, we can divide mobile robot into

● Autonomous
○ Robot moves automatically based on the sensors and other gear.
● Manual
○ Robot moves depend on the command received from the instructor.

Basic thing to develop mobile robot

1. Robot chassis.
2. Motor, motor driver and wheels.
3. Controller.
4. Battery.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Mobile Robot Introduction
List of mobile robot parts for this workshop

Quantity Description

1 set 2WD Smart Robot Car Chassis

1 Maker UNO (Arduino Uno compatible)

1 L298N Dual H Bridge Motor/Stepper Driver

1 set Male to Female Jumper Wire

1 set Male to Male Jumper Wire

4 PCB Stand

1 sets 4 x AA Battery

1 Bluetooth Serial Transceiver HC-05

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Getting Started with Maker UNO
What is Maker UNO?

Maker UNO is an Arduino Uno compatible board which is specially designed for
those who interested in learning electronic and programming for the first time.

What is ?

Arduino is an open-source electronics


prototyping platform based on flexible,
easy-to-use hardware and software. It’s
intended for artists, designers,
hobbyists, and anyone interested in
creating interactive objects or
environments.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Getting Started with Maker UNO
USB Micro B Connector
Mini Slide Switch Main supply, program and
Switch to connect/disconnect debug purpose.
piezo buzzer to pin 8.
On-board Piezo Buzzer
Piezo buzzer is connected to
Reset Button
Press to restart program.
pin 8 through slide switch.

Main Microcontroller On-board Programmable


Same as uC inside Arduino Uno Push Button
- ATmega328P. This button is connected to pin
2 without a pull up.

Power Pin USB to UART IC


USB 5V, regulated 3.3V 500mA Equipped with CH340G for USB
and GND. to UART conversion.

Analog Input Pin On-board Series of LEDs


All digital pins is equipped with
This pin can be used for reading
LED. This LED can be controlled
analog voltage.
or act as signal indicator.

Serial Pin
Arduino ISP Pin Hardware Serial (UART) is
This pin can be used for SPI and labeled with RX and TX pin.
bootloader programming.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Getting Started with Maker UNO
Arduino IDE

Arduino IDE is an open source software and it is free. Maker UNO can be
programmed using Arduino IDE. To get the latest version, you can download at

https://www.arduino.cc/en/Main/Software

Note: For Windows, we recommended to choose Windows Installer.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Getting Started with Maker UNO

Menu Bar
Button Bar Serial Monitor
Sketch Name

Code Area

Status Bar
IDE Output
Board Name and Port

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Getting Started with Maker UNO
Verify
Compiles and approves your code. It will detect errors in syntax (e.g. missing semi colon
or parentheses).

Upload
Compiles and send your code to the Maker UNO. When you click it, you should see the
lights (labelled RX & TX) on your board blink rapidly.

New Sketch
This button opens up a new code window tab.

Open
This button will let you open an existing sketch.

Save
This saves the currently active sketch.

Serial Monitor
Open Serial Monitor.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Getting Started with Maker UNO
Maker UNO Driver

In order to make your Maker UNO board can be detected by PC/laptop, you need to
install CH340G driver. Download the driver from the following link.

● Windows - https://cdn.cytron.io/makeruno/CH341SER.EXE
● Mac - http://www.wch.cn/download/CH341SER_MAC_ZIP.html
● Linux - Normally is readily installed

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Assemble the Mobile Robot

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Assemble the Mobile Robot
Wiring and connection

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Assemble the Mobile Robot
Wiring and connection

Maker UNO Pin Connection Maker UNO Pin Connection


0 NC 10 Motor Driver IN3

1 NC 11 Motor Driver IN4

2 Internal Push Button 12 NC

3 Bluetooth Transmit 13 NC

4 Bluetooth Receive A0 NC

5 Motor Driver ENA A1 NC

6 Motor Driver ENB A2 NC

7 Motor Driver IN1 A3 NC

8 Internal Buzzer A4 NC

9 Motor Driver IN2 A5 NC

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Control the DC Motor
What is DC Motor?

Any of a class of rotary electrical machines that converts direct current electrical
energy into mechanical energy. There have 2 types if DC motor:

1. Brush
2. Brushless

How brush DC motor works?

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Control the DC Motor
Moving DC Motor: Write and Upload this code to your Maker UNO.
#define BUTTON 2 Define pin 2 as internal push button.
#define L298N_ENA 5 Define pin 5 as motor driver ENA.
#define L298N_ENB 6
#define L298N_IN1 7 Define pin 6 as motor driver ENB.
#define L298N_IN2 9 Define pin 7 as motor driver IN1.
#define L298N_IN3 10 Define pin 9 as motor driver IN2.
#define L298N_IN4 11 Define pin 10 as motor driver IN3.
Define pin 11 as motor driver IN4.
void setup()
{
pinMode(BUTTON, INPUT_PULLUP);
pinMode(L298N_ENA, OUTPUT);
pinMode(L298N_ENB, OUTPUT); Set push button as input with internal pull up.
pinMode(L298N_IN1, OUTPUT); Set motor driver ENA as output.
pinMode(L298N_IN2, OUTPUT);
pinMode(L298N_IN3, OUTPUT); Set motor driver ENB as output.
pinMode(L298N_IN4, OUTPUT); Set motor driver IN1 as output.
} Set motor driver IN2 as output.
Set motor driver IN3 as output.
Set motor driver IN4 as output.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Control the DC Motor
Moving DC Motor: Write and Upload this code to your Maker UNO.
void loop()
{
if (digitalRead(BUTTON) == LOW) {
digitalWrite(L298N_ENA, HIGH); If button is pressed, move the motor.
digitalWrite(L298N_IN1, HIGH); Enable channel A
digitalWrite(L298N_IN2, LOW); Set the direction for motor 1

digitalWrite(L298N_ENB, HIGH);
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, LOW); Enable channel B
} Set the direction for motor 2

else {
digitalWrite(L298N_ENA, LOW);
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, LOW); If not pressed, both motors will stop
Disable channel A
digitalWrite(L298N_ENB, LOW);
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, LOW);
}
} Disable channel B

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Control the DC Motor
Result:

When the push button is pressed, both motors will move to certain direction. So
how to change the motor direction?

This table shows the relationship between driver signal at channel A and motor 1
direction. Same applies to channel B and motor 2.

Motor 1 ENA IN1 IN2

Stop LOW Don’t care Don’t care

Clockwise HIGH LOW HIGH

Counter Clockwise HIGH HIGH LOW

HIGH HIGH HIGH


Brake
HIGH LOW LOW

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Control the DC Motor
Controlling Motor Speed: You can control the motor speed by applying different
voltage at enable pin by using analogWrite() function.

For example:
if (digitalRead(BUTTON) == LOW) { If button is pressed, move the motor.
analogWrite(L298N_ENA, 200); Set voltage at pin ENA to ~4V (200/255 x 5V)
digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, LOW); Set the direction for motor 1

analogWrite(L298N_ENB, 200);
digitalWrite(L298N_IN3, HIGH); Set voltage at pin ENB to ~4V (200/255 x 5V)
digitalWrite(L298N_IN4, LOW); Set the direction for motor 2
}

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Basic Mobile Robot Movement

You already know how to control the motor. By combining the movement of both
motors, you can set the robot movement!
How to
Below is the basic movement of the robot: move? ???

1. Stop/Brake
2. Forward
3. Reverse
4. Turn Left Which direction I need
to rotate so my body
5. Turn Right will move forward?

Note: Everyone will have a different set of program to produce the same
movement. This is because we don’t have the same connection especially on
motor terminal. So find yours!

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Robot Movement: FORWARD

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Robot Movement: REVERSE

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Robot Movement: TURN RIGHT

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Robot Movement: TURN LEFT

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Create function: Once you have finalize your robot movement, create function for
each movement - forward, reverse, turn right, turn left and stop.
void robotForward(int robotSpeed) Function for robot move forward, speed = 0 - 255
{
analogWrite(L298N_ENA, robotSpeed);
digitalWrite(L298N_IN1, LOW); Enable channel A (Right motor)
digitalWrite(L298N_IN2, HIGH); Set the direction for motor 1 to move forward

analogWrite(L298N_ENB, robotSpeed);
digitalWrite(L298N_IN3, HIGH); Enable channel B (Left motor)
digitalWrite(L298N_IN4, LOW);
} Set the direction for motor 2 to move forward

void robotReverse(int robotSpeed)


{
analogWrite(L298N_ENA, robotSpeed); Function for robot go reverse, speed = 0 - 255
digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, LOW);
Enable channel A (Right motor)
analogWrite(L298N_ENB, robotSpeed); Set the direction for motor 1 to reverse
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, HIGH);
}
Enable channel B (Left motor)
Set the direction for motor 2 to reverse

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Create function: Once you have finalize your robot movement, create function for
each movement - forward, reverse, turn right, turn left and stop.
void robotTurnRight(int robotSpeed) Function for robot turn right, speed = 0 - 255
{
analogWrite(L298N_ENA, robotSpeed);
digitalWrite(L298N_IN1, HIGH); Enable channel A (Right motor)
digitalWrite(L298N_IN2, LOW); Set the direction for motor 1 to go reverse

analogWrite(L298N_ENB, robotSpeed);
digitalWrite(L298N_IN3, HIGH); Enable channel B (Left motor)
digitalWrite(L298N_IN4, LOW);
} Set the direction for motor 2 to move forward

void robotTurnLeft(int robotSpeed)


{
analogWrite(L298N_ENA, robotSpeed); Function for robot turn left, speed = 0 - 255
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, HIGH);
Enable channel A (Right motor)
analogWrite(L298N_ENB, robotSpeed); Set the direction for motor 1 to move forward
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, HIGH);
}
Enable channel B (Left motor)
Set the direction for motor 2 to reverse

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Set the Robot Movement
Create function: Once you have finalize your robot movement, create function for
each movement - forward, reverse, turn right, turn left and stop.
void robotStop() Function for robot stop
{
digitalWrite(L298N_ENA, LOW);
digitalWrite(L298N_IN1, LOW); Enable channel A (Right motor)
digitalWrite(L298N_IN2, LOW); Motor 1 stop

digitalWrite(L298N_ENB, LOW);
digitalWrite(L298N_IN3, LOW); Enable channel B (Left motor)
digitalWrite(L298N_IN4, LOW);
} Motor 2 stop

void robotBrake()
{
digitalWrite(L298N_ENA, HIGH); Function for robot brake
digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, HIGH);
Enable channel A (Right motor)
digitalWrite(L298N_ENB, HIGH); Motor 1 brake
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, HIGH);
}
Enable channel B (Left motor)
Motor 2 brake

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Serial Communication
What is Serial Communication?

It is a most popular communication protocol (also known as UART) used to


communicate between the Arduino board and a computer or other devices.

● Bluetooth: HC-05, HC-06.


● GSM: SIM900A, SIM800L.
● WiFi module: ESP8266.
● RFid reader: 125kHz, MiFare.
● GPS, RF module, and more...

Hardware Serial

It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via
USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital
input or output.

Note: Please disconnect pin 0 and 1 during uploading sketch.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Serial Communication
Software Serial

Due to limited number of hardware serial (only at pin 0 and 1) in Arduino Uno,
SoftwareSerial library has been developed to allow serial communication on other
digital pins of the Arduino Uno board.

However, there still have limitation on SoftwareSerial library for Arduino Uno:

● If using multiple software serial ports, only one can receive data at a time.
● Current maximum RX speed is 57600 bps.
● RX doesn't work on pin 13.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Bluetooth HC-05 AT Command
What is AT Command?

AT commands are instructions used to control a modem. AT is the abbreviation of


ATtention. Every command line starts with "AT" or "at", as a prefix that informs the
modem about the start of a command line.

Bluetooth HC-05 AT Command

AT Command Description

AT Check the connection.

AT+VERSION Check bluetooth version. For example, it returned VERSION:3.0-20170609

AT+NAME=.... Set bluetooth name. For example AT+NAME=Maker UNO

AT+PSWD="...." Set bluetooth pin number. For example AT+PSWD=”8888”

AT+ORGL Restore factory setting

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Bluetooth HC-05 AT Command
Upload this sketch to Maker UNO.

#include <SoftwareSerial.h> Include SoftwareSerial function


SoftwareSerial HC05Serial(3, 4); Set pin 3 as RX and 4 as TX
void setup()
{
Serial.begin(9600);
HC05Serial.begin(38400); Initialize Serial with 9600bps
} Initialize bluetooth serial with 38400bps
void loop()
{
if (Serial.available()) {
HC05Serial.write(Serial.read());
} If AT command received from Serial Monitor
if (HC05Serial.available()) {
Serial.write(HC05Serial.read()); Send AT command to bluetooth
}
} If response received from bluetooth
Display on Serial Monitor

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Bluetooth HC-05 AT Command
To enter AT command mode in HC-05 bluetooth module, please follow these
steps:

1. Turn off the bluetooth module.


2. Press and hold button on the bluetooth module.
3. Turn on the bluetooth module.
4. Release the button.

Open Serial Monitor and set to


9600 baud and Both NL & CR.
Next, configure bluetooth with
AT command.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Bluetooth HC-05 AT Command
Now set your own name and pin number at your bluetooth module. For example:

Send Received
AT OK
VERSION:3.0-20170609
AT+VERSION
OK
AT+NAME=Maker UNO OK
AT+PSWD=”8888” OK

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
Upload this sketch to Maker UNO. Then, we can see the status of the button on the
Arduino BT Joystick app at the Serial Monitor when the bluetooth is connected.

#include <SoftwareSerial.h> Include SoftwareSerial function


SoftwareSerial HC05Serial(3, 4); Set pin 3 as RX and 4 as TX
void setup()
{
Serial.begin(9600);
HC05Serial.begin(9600); Initialize Serial with 9600bps
} Initialize bluetooth serial with 9600bps
void loop()
{
if (Serial.available()) {
HC05Serial.write(Serial.read());
} If AT command received from Serial Monitor
if (HC05Serial.available()) {
Serial.write(HC05Serial.read()); Send AT command to bluetooth
}
} If response received from bluetooth
Display on Serial Monitor

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

In this workshop, we will use Arduino BT Joystick Free. Why? Because it is free,
easy to use and have a nice layout. So let’s install and open it.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

FYI, Arduino BT Joystick Free is no longer available in Play Store. But don’t worry,
we have prepared the APK file. You can download here (click using your phone)
from Google Drive.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

Finish download, click on the installer (APK) file. By default your phone setting is
not allowed any installation except from Play Store. Unless you’re sure the
sources, you can allow the installation on your phone setting.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

Proceed with the installation. Once complete, the app icon will appear on your
phone - home screen. To open it, just tap on the icon as usual.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

Now we need to configure a bit on


this app.

1. Click on the Menu and choose


Settings.

2. Click Define the send delay.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

3. Update value in Define the send


delay (ms) from 20 to 200. Click
OK and Save.

4. It will return to the main screen.


Now click CONNECT.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

5. Choose your bluetooth name.


Make sure your bluetooth name
is unique. Else, other participant
will connect to your bluetooth. :)

6. Put your bluetooth pin number


and click OK.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

7. If the connection successful, the


CONNECT button will update to
DISCONNECT. If you click on the
DISCONNECT button, it will
disconnect the bluetooth
connection.

8. Then, if you notice, there have a


set of data display on Serial
Monitor (during bluetooth is
connected). This is the status of
the button on the Arduino BT
Joystick app.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App

Every 200ms (set in Define the send delay), this app will send a set of data as
shown in the picture above. If no key have been pressed, it sends #b=0#.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
After knowing the data set, now we need to grab the data and do comparison in
the program, so that we can make specific action for specific key.
#include <SoftwareSerial.h> Include SoftwareSerial function
SoftwareSerial HC05Serial(3, 4); Set pin 3 as RX and 4 as TX
char inChar;
String inString; Global char variable for inChar
Global string variable called inString
void setup()
{
Serial.begin(9600);
HC05Serial.begin(9600);
} Initialize Serial with 9600bps
Initialize bluetooth serial with 9600bps

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
void loop()
{
if (HC05Serial.available()) {
delay(100); If bluetooth received the data from the app
inString = ""; Wait for 100ms so that all the data is received
while (HC05Serial.available()) { Clear inString variable
inChar = HC05Serial.read(); While the data set is available
inString = inString + inChar; Read and store to inChar variable
}
Combined characters to form a single string
if (inString == "#b=0#") {
Serial.println("None");
} Do comparison, if inString = "#b=0#"
else if (inString == "#b=1#") { Print on Serial Monitor “None”
Serial.println("Up");
}
} Do comparison, if inString = "#b=1#"
} Print on Serial Monitor “Up”

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino Bluetooth Mobile Robot
This is the final stage to create Arduino Bluetooth Mobile Robot. Make sure the
previous chapters is working.

1. Set the robot movement together


with functions.
2. Configure bluetooth using AT
command (name, pin #).
3. Read data set from the app.
4. Compare and do specific action.
5. Now, combine no 1 and 4.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
#define RX 3 Define pin 3 as received pin = HC05 TX.
#define TX 4 Define pin 4 as transmit pin = HC05 RX.
#include <SoftwareSerial.h>
SoftwareSerial HC05Serial(RX, TX); Include SoftwareSerial function
Set pin 3 as RX and 4 as TX
#define BUTTON 2
#define L298N_ENA 5 Define pin 2 as internal push button.
#define L298N_ENB 6 Define pin 5 as motor driver ENA.
#define L298N_IN1 7
#define L298N_IN2 9 Define pin 6 as motor driver ENB.
#define L298N_IN3 10 Define pin 7 as motor driver IN1.
#define L298N_IN4 11 Define pin 9 as motor driver IN2.
Define pin 10 as motor driver IN3.
char inChar; Define pin 11 as motor driver IN4.
String inString;
Global char variable for inChar
Global string variable called inString

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
void setup()
{
Serial.begin(9600);
HC05Serial.begin(9600); Initialize Serial with 9600bps
Initialize bluetooth serial with 9600bps
pinMode(BUTTON, INPUT_PULLUP);
pinMode(L298N_ENA, OUTPUT); Set push button as input with internal pull up.
pinMode(L298N_ENB, OUTPUT); Set motor driver ENA as output.
pinMode(L298N_IN1, OUTPUT);
pinMode(L298N_IN2, OUTPUT); Set motor driver ENB as output.
pinMode(L298N_IN3, OUTPUT); Set motor driver IN1 as output.
pinMode(L298N_IN4, OUTPUT); Set motor driver IN2 as output.
} Set motor driver IN3 as output.
Set motor driver IN4 as output.

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
void loop()
{
if (HC05Serial.available()) {
delay(100); If bluetooth received the data from the app
inString = ""; Wait for 100ms so that all the data is received
while (HC05Serial.available()) { Clear inString variable
inChar = HC05Serial.read(); While the data set is available
inString = inString + inChar; Read and store to inChar variable
}
Combined characters to form a single string
if (inString == "#b=0#") {
robotStop();
} else if (inString == "#b=1#") { If inString = "#b=0#"
robotForward(200); Robot stop
} else if (inString == "#b=2#") {
robotReverse(200); If inString = "#b=1#"
} else if (inString == "#b=3#") { Robot move forward with PWM 200
robotTurnLeft(150); If inString = "#b=2#"
} else if (inString == "#b=4#") { Robot go reverse with PWM 200
robotTurnRight(150); If inString = "#b=3#"
}
} Robot turn left with PWM 150
} If inString = "#b=4#"
Robot turn right with PWM 150

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
void robotForward(int robotSpeed) Function for robot move forward, speed = 0 - 255
{
analogWrite(L298N_ENA, robotSpeed);
digitalWrite(L298N_IN1, LOW); Enable channel A (Right motor)
digitalWrite(L298N_IN2, HIGH); Set the direction for motor 1 to move forward

analogWrite(L298N_ENB, robotSpeed);
digitalWrite(L298N_IN3, HIGH); Enable channel B (Left motor)
digitalWrite(L298N_IN4, LOW);
} Set the direction for motor 2 to move forward

void robotReverse(int robotSpeed)


{
analogWrite(L298N_ENA, robotSpeed); Function for robot go reverse, speed = 0 - 255
digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, LOW);
Enable channel A (Right motor)
analogWrite(L298N_ENB, robotSpeed); Set the direction for motor 1 to reverse
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, HIGH);
}
Enable channel B (Left motor)
Set the direction for motor 2 to reverse

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
void robotTurnRight(int robotSpeed) Function for robot turn right, speed = 0 - 255
{
analogWrite(L298N_ENA, robotSpeed);
digitalWrite(L298N_IN1, HIGH); Enable channel A (Right motor)
digitalWrite(L298N_IN2, LOW); Set the direction for motor 1 to go reverse

analogWrite(L298N_ENB, robotSpeed);
digitalWrite(L298N_IN3, HIGH); Enable channel B (Left motor)
digitalWrite(L298N_IN4, LOW);
} Set the direction for motor 2 to move forward

void robotTurnLeft(int robotSpeed)


{
analogWrite(L298N_ENA, robotSpeed); Function for robot turn left, speed = 0 - 255
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, HIGH);
Enable channel A (Right motor)
analogWrite(L298N_ENB, robotSpeed); Set the direction for motor 1 to move forward
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, HIGH);
}
Enable channel B (Left motor)
Set the direction for motor 2 to reverse

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


Arduino BT Joystick Android App
void robotStop() Function for robot stop
{
digitalWrite(L298N_ENA, LOW);
digitalWrite(L298N_IN1, LOW); Enable channel A (Right motor)
digitalWrite(L298N_IN2, LOW); Motor 1 stop

digitalWrite(L298N_ENB, LOW);
digitalWrite(L298N_IN3, LOW); Enable channel B (Left motor)
digitalWrite(L298N_IN4, LOW);
} Motor 2 stop

void robotBrake()
{
digitalWrite(L298N_ENA, HIGH); Function for robot brake
digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, HIGH);
Enable channel A (Right motor)
digitalWrite(L298N_ENB, HIGH); Motor 1 brake
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, HIGH);
}
Enable channel B (Left motor)
Motor 2 brake

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io


End of Workshop

Thank you and Let’s Play!


Q & A Session

Arduino Bluetooth Mobile Robot by Cytron Technologies - https://www.cytron.io

You might also like