Lecture 19
Lecture 19
Bluetooth
• Bluetooth is a wireless technology standard used for exchanging data between fixed and mobile
devices over short distances using UHF radio waves in the industrial, scientific and medical radio
bands, from 2.402 GHz to 2.480 GHz, and building personal area networks.
• HC05 module is a Bluetooth module using serial communication, mostly used in electronics
projects.
• Working voltage: 3.6V – 5VInternal antenna: YesAutomatic connection to the last device: Yes
2
RX/ TX
Serial Communication
3
Sketch for Displaying Data Received Via Bluetooth On Serial Monitor
#include<SoftwareSerial.h>
/* Create object named bt of the class SoftwareSerial */
SoftwareSerial bt(2,3); /* (Rx,Tx) */
void setup() {
bt.begin(9600); /* Define baud rate for software serial communication */
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
if (bt.available()) /* If data is available on serial port */
{
Serial.write(bt.read()); /* Print character received on to the serial monitor */
}
}
4
Code for LED
char data = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
data = Serial.read();
Serial.print(data);
Serial.print("\n");
}
elseif(data == '1')
{
digitalWrite(13, HIGH); else if(data == '0')
digitalWrite(13, LOW);
}
}
5
Single servo control
#include <SoftwareSerial.h> // TX RX software void loop()
library for bluetooth {
//Read from bluetooth and write to usb serial
#include <Servo.h> // servo library if(bluetooth.available()> 0 ) // receive number from bluetooth
Servo myservo; // servo name {
int servopos = bluetooth.read(); // save the received number to servopos
int bluetoothTx = 10; // bluetooth tx to 10 pin Serial.println(servopos); // serial print servopos current number received from
int bluetoothRx = 11; // bluetooth rx to 11 pin bluetooth
myservo.write(servopos); // roate the servo the angle received from the android app
SoftwareSerial bluetooth(bluetoothTx, }
bluetoothRx);
void setup() }
{
myservo.attach(9); // attach servo signal wire
to pin 9
//Setup usb serial connection to computer
Serial.begin(9600);
6
Multi servo control
#include <SoftwareSerial.h> // TX RX software void loop()
library for bluetooth {
//Read from bluetooth and write to usb serial
#include <Servo.h> // servo library if(bluetooth.available()>= 2 )
Servo myservo1, myservo2, myservo3, myservo4; //
{
servo name
unsigned int servopos = bluetooth.read();
int bluetoothTx = 10; // bluetooth tx to 10 pin unsigned int servopos1 = bluetooth.read();
int bluetoothRx = 11; // bluetooth rx to 11 pin unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
if (realservo >= 1000 && realservo <1180) {
void setup() int servo1 = realservo;
{ servo1 = map(servo1, 1000, 1180, 0, 180);
myservo1.attach(3); // attach servo signal wire to myservo1.write(servo1);
pin 9
Serial.println("Servo 1 ON");
myservo2.attach(5);
myservo3.attach(6);
delay(10);
myservo4.attach(9); }
//Setup usb serial connection to computer
Serial.begin(9600);
7
Multi servo control
if (realservo >= 2000 && realservo <2180) {
int servo2 = realservo;
servo2 = map(servo2, 2000, 2180, 0, 180);
myservo2.write(servo2);
Serial.println("Servo 2 ON");
delay(10);
}
if (realservo >= 3000 && realservo <3180) {
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180, 0, 180);
myservo3.write(servo3);
Serial.println("Servo 3 ON");
delay(10);
}
if (realservo >= 4000 && realservo <4180) {
int servo4 = realservo;
servo4 = map(servo4, 4000, 4180, 0, 180);
myservo4.write(servo4);
Serial.println("Servo 4 ON");
delay(10);
}
}
}
8
4