Arduino 03 – Bluetooth Functionality

  • Posted on: 25 September 2017
  • By: Jack Chiang
Tools: 
PC (Windows or Mac)
Griffith Innovate Arduino Kit
Arduino IDE
Difficulty: 
Beginner
Prerequisites: 
Arduino 01 - Introduction to Arduinos

Bluetooth Functionality

This tutorial is the third installment of the Arduino Workshops. In this tutorial, we will be achieving the following objectives:

  • How to set up Bluetooth Functionality
  • Returning data and controlling your Arduino Car through a smartphone device.

1 - Bluetooth Setup

First we need to to download an Arduino Bluetooth Controller from the Google Play Store and install it to your smartphone device.

(https://play.google.com/store/apps/details?id=com.giumig.apps.bluetoothserialmonitor&hl=en)

Next, connect your Arduino to your computer and copy  and paste this code into your ardunio IDE.

void setup(){
   Serial.begin(9600);

   Serial.println("Test");
}

void loop(){
}

Compile the code and if you have no errors, upload it to your arduino. If it finishes uploading your arduino should now have Bluetooth functionality. If you have uploading errors or freezing issues, refer to the ‘Potential Problems’ down below.

2 - Establishing Connection

We will now test the arduino Bluetooth connection.

Ensure that your Bluetooth is enabled on your smartphone, then open up the ‘Arduino Bluetooth’ application on your device. Look for a Bluetooth device with the name ‘HC-06’ and connect to it; the default PIN should be ‘1234’. Once connected, you should be prompted with a window that displays the different connection modes, go ahead and select ‘Terminal Mode’. If you have successfully connected to your arduino, click the red reset button next to the usb connector on your ardunio and the terminal should return the text ‘Test.’

3 - Control Setup

Next, connect your Arduino to your computer and copy  and paste this code into your ardunio IDE.

// connect motor controller pins to Arduino digital pins
// motor one
int left = 10;
int in1 = 9;
int in2 = 8;
// motor two
int right = 5;
int in3 = 7;
int in4 = 6;

const int ultrasonictrigPin = 12;
const int ultrasonicechoPin = 13;

String readString;
int power =200;
void setup()
{

  // set all the motor control pins to outputs
  pinMode(left, OUTPUT);
  pinMode(right, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Set all ultrasonic pins to outputs
  pinMode(ultrasonictrigPin, OUTPUT);
  pinMode(ultrasonicechoPin, INPUT);
  
  Serial.begin(9600);

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
  
}
void loop(){
 
  delay(100);
  String input="";
  while (Serial.available()) {
    //delay(3);  
    char c = Serial.read();
    readString += c; 
  }
  readString.trim();
  if (readString.length() >0) {
    if (readString == "left"){
      Serial.println("left");
      analogWrite(left, 0);
      analogWrite(right, power);  
    }else if (readString == "right"){
      Serial.println("right");
      analogWrite( left, power);
      analogWrite(right, 0);  
    }else if (readString == "forward"){
      Serial.println("forward");
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);  
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW); 
      analogWrite(left, power);
      analogWrite(right, power);     
    }else if (readString == "start"){
      Serial.println("start");
      analogWrite(left, power);
      analogWrite(right, power);
    }else if (readString == "stop"){ 
      Serial.println("stop");
      analogWrite(left, 0);
      analogWrite(right, 0);
    }else if (readString == "up"){
      Serial.println("up");
      power = min(power + 5, 255);
    }else if (readString == "down"){
      Serial.println("down");
      power = max(power - 5, 150);
    }else if (readString == "reverse"){
      Serial.println("reverse");
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);  
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH); 
      analogWrite(left, power);
      analogWrite(right, power);
    }
    readString="";
  } 
  digitalWrite(ultrasonictrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonictrigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ultrasonictrigPin, LOW);

  long duration = pulseIn(ultrasonicechoPin, HIGH);

  long inches = msToInches(duration);
  long cm = msToCentimeters(duration);
  
  Serial.println(cm);
  if (cm <= 10){
    analogWrite(left, 0);
    analogWrite(right, 0);
  }

}

long msToInches(long duration){  
  return duration/ 74 / 2;
}

long msToCentimeters(long duration){  
  return duration/ 29 / 2;
}

Compile the code and if you have no errors, upload it to your arduino. If it finishes uploading you can now begin setting the controls on your smartphone device.

Open up the ‘Arduino Bluetooth’ application on your device again and re-connect to your arduino. Connect to your arduinio in 'Controller Mode,' and click the settings icon in the top right hand corner to setup the controls.

After setting the controls, return to the controller and voilà, you should now be able to manually and remotely control the movement of your arduino.

Potential Problems

  • If you cannot upload to the board, try clicking on tools -> port and selecting a com port to talk to the ardunio.
  • If your upload is talking too long and freezes/times out, unplug the power pin for your Bluetooth module (the BT module is talking on the same port as the USB, and is clashing) and try upload again. Remeber to plug the pin back in after successfully uploading your code.

Category Terms: 
Tutorial Weight: 
300

Comments

Bluetooth is actually a system that connect your devices without any kind of wire or cable and if you want to understand the functioning of its working read this article. I write my dissertation help about the topic in short days.