Saturday, August 8, 2015

Bluetooth HC05

How to pair HC-05 Bluetooth on an Arduino with an Android phone:
by J.B. Wylzan


Project 24:  Bluetooth HC-05

This project pairs the HC05 transceiver with an android phone
and lights an LED on and off.




WARNING: Before you proceed, please read the HC-05 datasheetFrom this time on forward, specially when we start connecting wireless through our bluetooth, ethernet, motorshield, and internet, make sure to read first the warning signs. There are times you might change your modem settings automatically accidentally, burn your PC as your internal temperature gets higher unknowingly, or even hack your neighbors internet unintentionally. So please BEWARE. If you are not sure, the best place to get help is the Arduino Forum.


HC-05 Circuit Wiring:  
   HC05 Gnd      - Arduino pin Gnd
   HC05 3.3        - Arduino pin 3.3v
   HC05 Tx         - Arduino pin 1 (Tx)
   HC05 Rx         - Arduino pin 0 (Rx)
  
Block Diagram:

Circuit config for AT commands
Hardware:
HC05 bluetooth RF

Arduino phone (samsung galaxy) 9v battery (for pairing wireless)
connecting wires
breadboard
Arduino R3 UNO board

Code # 21a:
/*Pairing HC05 with a bluetooth Android phone or tablet

  using numerical inputs   The example code is public domain.*/

int ledPin = 13; 
int state = 0;
int flag = 0; 

void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
 Serial.begin(9600); 
}

void loop() {

 if(Serial.available() > 0){
 state = Serial.read();
 flag=0;
 }

 if (state == '0') {
 digitalWrite(ledPin, LOW);
 if(flag == 0){
 Serial.println("LED: off");
 flag = 1;
 }
 }

 else if (state == '1') {
 digitalWrite(ledPin, HIGH);
 if(flag == 0){
 Serial.println("LED: on");
 flag = 1;
 }
 }
}
// end of the sketch


Code # 21b:

/* Pairing HC05 with a bluetooth Android phone or tablet
using characters inputs  The example code is public domain. */

int ledPin = 13; 
char kim;
String inputString="";

void setup() {
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600); 
}

void loop() {
 if(Serial.available()) {
   while(Serial.available())
   {
 char inChar = (char)Serial.read();
 inputString += inChar;
 }
Serial.println(inputString);
while(Serial.available()>0)
{kim=Serial.read();}
 if (inputString == "q") {
 digitalWrite(ledPin, HIGH);
 }else if (inputString == "p"){
   digitalWrite(ledPin, LOW);
 }
 inputString = "";
 }
}
// end of the sketch

Actual Setup:
Serial Monitor and Bluetooth Terminal
Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #21 above
5. Paste Code #21
6. Click File > Save
7. Click Verify
8. Click Upload
9. Open Serial Monitor
10. Type 1 or 0 / q or p on the serial motor command line 
11. Press Enter - you should see the Led turns On or Off

How to pair your Bluetooth with your android phone:
1. Go to Settings and select Bluetooth
2. Click Scan to detect your bluetooth
3. Select HC05 on the available devices list
4. Enter the device pin (0000 or 1234)
5. Click OK and the led blinking will settle down in series of two counts
6. Download the Bluetooth Terminal apps from Google Play
7. Open and select HCO5 on the list
8. If ask to connect, click Connect
9. The codes are transmitted wirelessly on your phone.

Challenge:
Now do the opposite. Use your phone to control the led on and off wireless.

Common AT Commands:
1. AT+VERSION
2. AT+NAME
3. AT+PSWD
4. AT+UART
5. AT+ROLE

Resources: AT Commands PDF


Project # 22:  MicroServo SG90


// Type 1,2,3,on the serial monitor to turn two leds on or off. (watch2AFNL)

// Read data from the serial and turn ON or OFF a light depending on the value
//#include <SoftwareSerial.h>

//SoftwareSerial mySerial(0,1); // RX, TX


char val;                                              // Data received from the serial port
int ledPin1 = 4;                
int ledPin2 = 10;

void setup() {
  pinMode(ledPin1, OUTPUT);         // Set pin as OUTPUT
  pinMode(ledPin2, OUTPUT);
  Serial.begin(9600);                         // Start serial communication at 9600 bps
}

void loop() {
  if (Serial.available()) {          
    int data = Serial.read();            
    switch (data) {
case '1':                  
    digitalWrite(ledPin1, HIGH);    //led1 is ON
    break;
case '2' :          
    digitalWrite(ledPin2, HIGH);    //led2 is ON
    break;
 
case '3' :          
    digitalWrite(ledPin1, LOW);    //led1 is OFF
    digitalWrite(ledPin2, LOW);    //led2 is OFF
    break;

default :
Serial.println("TYPE 1, 2 or 3 ");
break;

}
  }
}

Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 




No comments:

Post a Comment