Lesson 2 Bluetooth Car
Lesson 2 Bluetooth Car
com
It is very important and so cool to control your car wirelessly in a certain space
when we learn the Arduino, so in the lesson, we will teach you how to control
a car by Bluetooth.
Learning Parts:
Learn how to use the Bluetooth module and the Bluetooth APP
Learn how to control the vehicle via Bluetooth
Write programs to implement this function
Preparations:
A vehicle (equipped with battery)
A USB cable
A Bluetooth module
An IPhone or tablet
1
https://www.kuongshun.com
Ⅰ. Bluetooth module
This is the schematic diagram of Bluetooth module connected to UNO controller board:
In the experiment we will connect it to UNO board via expansion board V5.
2
https://www.kuongshun.com
Before beginning, connect the HC-08 Bluetooth module to the expansion board and turn on the power.
For Android
There are two ways to install the application.
1. Copy the “KUONGSHUN BLE Tool.apk” file to the Android products and install it.
2. Search “KUONGSHUN BLE Bluetooth Tool” in Google Play Store and install it.
3
https://www.kuongshun.com
For iOS
Search “KUONGSHUN BLE Tool” in Apple APP Store and
install it.
4
https://www.kuongshun.com
If you don't open your Bluetooth, it will show as blow and advise you to turn on Bluetooth function.
For Android, click “Allow” that will open the bluetooth automatically. For iOS, click “Settings” that
will jump to bluetooth setup interface, you should turn on the bluetooth and then return to the
“KUONGSHUN BLE Tool” APP interface.
After turn on Bluetooth, the bluetooth icon appears on the APP interface. Click “Connection”.
5
https://www.kuongshun.com
Then you phone will search Bluetooth equipment nearby. HC-08 device appear.
Click the Bluetooth name “HC-08”, when the connection is successful, the screen will be displayed
“Device Connected”.
6
https://www.kuongshun.com
Returns the main interface of the application. After the Bluetooth connection, the Bluetooth name will
be displayed on the screen.
Then we can slide the screen to the right side by our finger and we can get the key pattern as below:
Finally, we set the definition of each button, we will take “go forward” for example, please see below,
and the rest key-values are set in the same manner.
7
https://www.kuongshun.com
8
https://www.kuongshun.com
Ⅲ. Testing
Open the code file in the path “\kuongshun Smart Robot Car Kit V3.0\bluetooth_blink
\bluetooth_blink.ino” and upload the program to the UNO board.
Code preview: //
www.kuongshun.com
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
//The Bluetooth serial port to receive the data in the function
getstr = Serial.read();
if(getstr == 'a'){
stateChange();
}
}
Disconnect it from the computer, and then switch on the car’s power supply. (TIPS: The Bluetooth
module should be pulled out when you upload the program, or it will be failed to upload the program.)
Open APP
After connecting the phone to the car through Bluetooth, we set data as below:
9
https://www.kuongshun.com
After set-up, press this button. You will find that light on the UNO board changes with the switch.
The code
Serial.begin(9600);
The purpose of this block of code is to set the baud rate of the UNO control board as 9600 and open
the serial port. In this way, they can communicate with each other, because the original baud rate of
the Bluetooth module is 9600.
getstr = Serial.read(); //The Bluetooth serial port to receive the data in the function
if(getstr == 'a'){
stateChange();
}
This function is executed repeatedly within the circulating function. It will first read data from the serial
port and then check the data. If it meets the condition, it will execute the corresponding sub-function.
For example, if it reads the letter ‘a’ from the serial port, it will execute the sub-function responsible for
switching on/off the LED light.
10
https://www.kuongshun.com
When the car turns left or right, it’s not necessary to set the speed too fast. On the contrary, we need
to control the speed of car. But how to control?
The answer is PWM.
PWM is the abbreviation of “Pulse Width Modulation”, is called pulse modulation in short, is an effective
technology to control analog circuit with digital output of microprocessor, car is used to change speed
of motor by altering duty cycle of a square wave. In other words, connect and break circuit between
two sides of motor constantly, is switch of holding motor work, motor will not be off when power is off
because of the fast speed. So we can control speed of car if we control specific value of power on time
and power off time. The speed of car will be max when circuit is holding still. The speed of car will be
minimum if circuit is holding off. The speed of car will be median in half time. PWM is a technology to
get analog quantity through digital method. A square wave is formed by digital control, square wave
signal only have two state of on and off (That is high-low of digital pins).Simulate voltage changing from
0 to 5V by controlling specific value of duration on and off time. Occupied time of on (That is high level
in academy) is called pulse width, so PWM is also called pulse width modulation. Let’s learn about PWM
through five square waves below.
Green vertical line above represent a period of square wave. The value written into every
analogWrite(pin,value) corresponds to the percentage, the percentage is also called Duty Cycle, refer to
the percentage gotten from specific value between duration high level and low level time in a period.
In figure, from top to bottom, the first square wave, duty cycle is 0%, corresponding value is 0. Output
circuit current is minimum, motor hold still. The longer duration time is, the bigger circuit current motor
11
https://www.kuongshun.com
gets, the faster the speed is. So, the final one’s duty cycle is 100%, corresponding value is 255, motor
rotates in full speed. 50% is medium hyponastic rotate speed, 25% is relatively slower, even can’t start
(The circuit current is relatively big to start motor because of static friction). PWM is mostly used to
adjust light of LED and rotate speed of motor, wheel speed controlled by motor is easily be controlled.
The advantage of PWM can be more reflected when you play with some Arduino cars.
analogWrite(pin, value);
analogWrite() is used to write analog value of 0 to 255 for PWM ports. What you need to note is that,
analogWrite() is only used to digital pins with function of PWM. Pins with function of PWM in UNO are
only digital pins of 3,5,6,9,10,11.
Our car’s speed is controlled by connecting pin5 and pin6 of ENA and ENB. The program below, have set
a digital function int carSpeed = 150;
The speed is controlled in below program, so you can control the speed on your own.
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
After learning the basic knowledge, we will upload the program as below to the car, open the code
file in the path “\kuongshun Smart Robot Car Kit V3.0\bluetooth_car\ bluetooth_car.ino” and then
upload the program to the UNO control board.
Code preview:
//www.kuongshun.com
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define LED 13
12
https://www.kuongshun.com
char getstr;
void forward(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("Forward");
}
void back(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("Back");
}
void left(){
analogWrite(ENA,carSpeed);
analogWrite(ENB,carSpeed);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("Left");
}
void right(){
analogWrite(ENA,carSpeed);
analogWrite(ENB,carSpeed);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("Right");
}
void stop(){
digitalWrite(ENA,LOW);
13
https://www.kuongshun.com
digitalWrite(ENB,LOW);
Serial.println("Stop!");
}
void stateChange(){
state = !state;
digitalWrite(LED, state);
}
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
stop();
}
void loop() {
getstr = Serial.read();
switch(getstr){
case 'f': forward(); break;
case 'b': back(); break;
case 'l': left(); break;
case 'r': right(); break;
case 's': stop(); break;
case 'a': stateChange(); break;
default: break;
}
}
Switch on the power supply of the vehicle and put it on the ground.
Open the mobile APP, and set up parameters as follows.
14
https://www.kuongshun.com
15
https://www.kuongshun.com
Now we can control the car by Bluetooth and play with it.
16