arduino bluetooth tutorial

SPP-C and HC-05 Bluetooth module with arduino tutorial

In this tutorial, I will share how to use the Bluetooth module with Arduino. We will learn how to send data, receive data from a phone, and also how to control Arduino from the phone.

This is a companion to a video on miliohm Youtube Channel. You can watch full tutorial below.

Let’s start hookup the Bluetooth module with Arduino.

Wiring Diagram

SPP-C and HC-05 Bluetooth module with arduino tutorial wiring diagram

The Code

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 4); //Rx,Tx

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

Upload the code above and now we can communicate arduino and Bluetooth module. We will communicate Arduino with Bluetooth module use serial monitor. And these are some list AT command we can use.

AT+NAME -> check the Bluetooth name

AT+VERSION -> Bluetooth version

AT+PIN -> Bluetooth pin

AT+BAUD -> Bluetooth baudrate

To Change Bluetooth name you can type

AT+NAMEyourdesiredname

Now we will try to display text sent from phone to LCD. So let’s hookup an LCD to arduino.

Bluetooth Module with LCD Wiring Diagram

SPP-C and HC-05 Bluetooth module with arduino and LCD wiring diagram
#include <SoftwareSerial.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
SoftwareSerial mySerial(3, 4); //Rx,Tx

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.clear();
}

void loop() {
  String data;
  if (mySerial.available()) {
    data += mySerial.readString();
    data = data.subString(0, data.length() - 2);
    Serial.print(data);
    lcd.setCursor(0,0);
    lcd.print(data);
    
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

Upload the program above, and you can use any serial app to Bluetooth. In this example I use android app called Serial Bluetooth Terminal. Any text you send from this app will shown on LCD.

Serial Bluetooth Terminal App

Add LEDs to Control From Phone

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 4); //Rx,Tx

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop() {
  if (mySerial.available()) {
    char data = mySerial.read();
    Serial.print(data);
    if (data == '1') {
      digitalWrite(5, !(digitalRead(5)));
    }
    else if (data == '2') {
      digitalWrite(6, !(digitalRead(6)));
    }
    else if (data == '3') {
      digitalWrite(7, !(digitalRead(7)));
    }
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

So when you send data 1 to Bluetooth, it will toggle first LED, 2 for second LED, and 3 is third LED.