superturis
Published © GPL3+

Basic Bluetooth communication with Arduino & HC-05

Establish a BT connection, send command to turn LED on and off and check the status of the LED.

BeginnerFull instructions provided2,280
Basic Bluetooth communication with Arduino & HC-05

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
LED (generic)
LED (generic)
×1
Through Hole Resistor, 1 kohm
Through Hole Resistor, 1 kohm
×2
Through Hole Resistor, 2 kohm
Through Hole Resistor, 2 kohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Bluetooth terminal (Android)

Story

Read more

Schematics

Wiring

Prototype

Terminal

Code

Code

Arduino
This code will allow to set an output (pin 13) to either high or low using the commands "on" and "off". In addition to that, we can check the status of the pin with the command "status".
// We will receive from pin 10 and transmit to pin 11
const int BTRX = 10;
const int BTTX = 11;

// Output LED at pin 13
const int LED  = 13;

// We will use this variable to keep track of the status
int val = 0;

// We use software serial to avoid conflicts
// with the default RX/TX pins of the Arduino board
#include <SoftwareSerial.h>
SoftwareSerial SerialBT(BTRX, BTTX);

// Define the message
String msg; 

// Setup the connection
void setup() {
  SerialBT.begin(9600);
  SerialBT.println("Bluetooth connection is established");
  pinMode(LED, OUTPUT);
}

//    Loop waiting for a message in every iteration
//    Analyze message,
//    Trigger action (LED on / off / check status)

void loop() {
  if (SerialBT.available()){      // Data are pending
     msg = SerialBT.readString(); // Read the message
     if (msg == "on") {
         digitalWrite(LED, HIGH);
         SerialBT.print("LED at Pin ");
         SerialBT.print(LED);
         SerialBT.println(" is ON");
      } 
      else
      if (msg == "off") {
         digitalWrite(LED, LOW);
         SerialBT.print("LED at Pin ");
         SerialBT.print(LED);
         SerialBT.println(" is OFF");
      }
      else
      if (msg == "status") {
        val = digitalRead(LED);
        SerialBT.print("LED at Pin ");
        SerialBT.print(LED);
        SerialBT.println(" is ");
        if (val == 0) {
          SerialBT.println("OFF");
        } else {
          SerialBT.println("ON");
        }
      }
      else {
         SerialBT.print("Command <");
         SerialBT.print(msg);
         SerialBT.println("> is unknown");
      }
    }
}                                          

Credits

superturis
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.