Exercise 37 - Bluetooth car
1. INTRODUCTION
This project has as objective you can to practise with Bluetooth communication and actuators control. When you finish this project, you can manage a car using your smart phone. You can control it on all directions and manage lights.
Materials:
· Arduino UNO board.
· Protoboard.
· Structure kit
· Some Jumpers.
· H-bridge.
· 2xDC motors.
· Some LEDs or RGB LEDs.
· Battery holder/Power supply
· HC-06 Bluetooth module
· Some 220 ohms resistors
To start to this project you have to mount the car structure. You can do this project without print 3D models, but not is recommended. You can retire brown protector of the car structure or no. To build it you have to follow the next steps:
![]() |
![]() |
---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
2. HOW TO CONNECT?
This project has many components, to connect all you need to be tidy. You should start by H- Bridge. First, connect the motors to OUTs in H-bridge and then connect the correspondents inputs to Arduino. To energize the system, connect a 6 to 12 Volts power supply to +12 and GND terminals on H-bridge which also has a regulator to 5V, you can energize Arduino board with this terminal, connecting 5V and GND to Vin and GND on Arduino board.
Secondly, energize the Bluetooth module and connect TxD pin to pin 12 and RxD pin to pin 2 on Arduino board.
Finally, on this example, the LED is connected to pin 13, but you can connect it to any other pin
EXERCISE_A |
3. PROGRAMMING
To use this program, you have to download the app from the repository. Do not forget to include SoftwareSerial and Servo library. It is important to declare global variables before setup.
#include <SoftwareSerial.h> // Include SoftwareSerial library
#include <Servo.h>
SoftwareSerial BT(12, 2); /*Define pins Rx and Tx on Arduiono, which are connected to Bluetooth.*/
boolean lastState = LOW; // Last state of the button
boolean currentState = LOW; // Current state of the button
int antiReboundTime = 100;
int counter = 0; //Variable to increment the counter
String value = "";
int velocity = 0;
int motor1 = 6;
int motor1r = 5;
int motor2 = 11;
int motor2r = 10;
On Setup, all pins are set as OUTPUT because this Project not has any sensor. On the other hand, you have to initialize created software serial with BT.begin(9600).
void setup() {
pinMode(13, OUTPUT);
pinMode(motor1, OUTPUT);
pinMode(motor1r, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor2r, OUTPUT);
BT.begin(9600); //Initialize serial port which has been created.
}
This function is a modification of the function debounce in SENSORS_ON_LCD exercise. In this case, you can use the function with BT serial to avoid repeated character, when you want to turn on the LIGHT for example.
boolean debounce(boolean last) // Call anti-rebound function
{
boolean current ;
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
if (value == "i") {
current = 1;
}
else {
current = 0;
}
if (last != current)
{
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
if (value == "i") {
current = 1;
}
else {
current = 0;
}
}
return current;
}
On loop, the characters that you send it are read and then it is compared with conditions to set the velocity.
void loop() {
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
/*******************/
/*DIRECTION CONTROL*/
/*******************/
if (value == "a") {
velocity = -200;
}
else if (value == "b" || value == "h") {
velocity = 0;
}
else if (value == "c") {
velocity = 50;
}
else if (value == "d") {
velocity = 100;
}
else if (value == "e") {
velocity = 150;
}
if (velocity >= 0) {
analogWrite(motor1, velocity);
analogWrite(motor2, velocity);
analogWrite(motor1r, 0);
analogWrite(motor2r, 0);
}
else {
analogWrite(motor1, 0);
analogWrite(motor2, 0);
analogWrite(motor1r, velocity);
analogWrite(motor2r, velocity);
While Arduino is receiving f o g character the car must be rotate but, it must can do two rotation types one for velocity = 0 and another for velocity different of zero. The last type is different when the car, go forwards or backwards.
The first two whiles is inside else condition. It is used when the car go backwards. It is necessary to read the character received another time inside the whiles, because when you touch up a turn key the app sends another character to Arduino and then, Arduino stop rotates the car.
while (value == "f") {
analogWrite(motor1, 0);
analogWrite(motor2, 0);
analogWrite(motor1r, 0);
analogWrite(motor2r, velocity);
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
}
while (value == "g") {
analogWrite(motor1, 0);
analogWrite(motor2, 0);
analogWrite(motor1r, velocity);
analogWrite(motor2r, 0);
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
}
}
while (value == "f") {
analogWrite(motor1, 0);
analogWrite(motor2, velocity);
analogWrite(motor1r, 0);
analogWrite(motor2r, 0);
if (velocity == 0) {
analogWrite(motor1, 0);
analogWrite(motor2, 200);
analogWrite(motor1r, 200);
analogWrite(motor2r, 0);
}
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
}
The second two whiles i used to turn, when the car, go forwards.
while (value == "g") {
analogWrite(motor1, velocity);
analogWrite(motor2, 0);
analogWrite(motor1r, 0);
analogWrite(motor2r, 0);
if (velocity == 0) {
analogWrite(motor1, 200);
analogWrite(motor2, 0);
analogWrite(motor1r, 0);
analogWrite(motor2r, 200);
}
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
}
/*******************/
/*LIGHTS CONTROL*/
/*******************/
On the light control you are going to use another time the debounce function. When you are pressing light button on the app the counter is changing between 0 and 1.
currentState = debounce(lastState); // Eliminates rebound
if ( lastState == LOW && currentState == HIGH)
{
counter = counter + 1; //Increments counter
}
if (counter != 1) {
counter = 0;
}
if (counter == 0) {
digitalWrite(13, LOW);
}
else if (counter == 1) {
digitalWrite(13 , HIGH);
}
lastState = currentState; // Update the state
}