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

Our academic pursuits, along with a range of extracurricular activities, help in honing a child's skills and ensuring that he/she grows to be a mature and responsible citizen.
top public school in greater noida
secure school in noida

I have noted down the code which helped me a lot to make my Arduino based robot bluetooth capable. It is best to read australianwritings.com.au review to get papers which let me know a lot of things about Arduino that I wasn't before.

Apple products are revered for its quality, precision and great design. SRSG started its operations as Apple technology partners in the year 1997.
apple reseller bhubaneswar
macbook reseller ahmedabad
system integration service providers in india
apple ipad mini reseller in delhi
macpro service center in Kolkata
ipad reseller delhi

Your texts on this subject are correct, see how I wrote this site is really very good. Text Chemistry

Gives you the best website address I know there alone you'll find how easy it is. Halki Diabetes Remedy Review

You bear through a awesome vacancy. I sanity definitely quarry it moreover personally suggest to my buddys. I am self-possessed they determination be benefited from this scene. His Secret Obsession

It is especially decent, though look into the tips during this home address. Make Him Worship You

I wrote about a similar issue, I give you the link to my site. YouTube Secrets

You should never rely on one channel of traffic to survive anyway, especially organic traffic my company

My planner is endeavoring to convince me to move to .net from PHP. I have always despised the idea in perspective on the expenses. However, he's troublesome none the less. I've been using Movable-type on a couple of destinations for about a year and am tense about changing to another stage. I have heard unprecedented things about blogengine.net. Is there a way I can move all my WordPress posts into it? Any help would be esteemed. eHealtHow

We provide the cheapest and most reliable csgo accounts compared to anywhere on the internet. This site is run by people who have boosted their way up the ladder.
csgo accounts kaufen

Our approach brings together best-in-class virtualized compute, storage, and networking infrastructure.
azure partner in uae
TrendMicro Partner in UAE
mobility partner in uae
endpoint security in uae

Welcome to the world of gaming, where life is fast paced and interesting. There is always an adventure waiting for you online.
buy csgo smurf accounts
csgo bundle accounts

Provides multiple learning environment of International standard with holistic system of education at an affordable cost for the successful life of young generation.
paramedical college north india
best placement college in dehradun
The goal of the society is to create professionally well skilled students To achieve the said goal, arrangements have been made with the pioneers and front runners both in India and abroad.
best school in premnagar

 

 

In any event, considering different components (the recurrence of s3xual relations, the character attributes of ladies, the utilization of oral contraceptives, the quality of the relationship, and so on.), BioVirexagen