Arduino - BLE

In this tutorial, we are going to learn:

Arduino BLE

The scope of this tutorial:

It's worth noting that this tutorial specifically covers Bluetooth Low Energy (BLE, Bluetooth 4.0). If you are looking for information on Classic Bluetooth (Bluetooth 2.0), please refer to a similar tutorial Arduino - Bluetooth.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×HM-10 Bluetooth Module
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About HM-10 Bluetooth Module

The HM-10 is a Serial BLE module that functions as a Serial to Bluetooth Low Energy converter. It performs the following actions:

  • Receives data from the Serial RX pin and transmits it to a paired device such as a smartphone via BLE
  • Receives data from BLE (from the paired device) and sends it to the Serial TX pin.

Specifically, when using an Arduino to communicate with a smartphone app (Android/iOS), the following occurs:

  • The Arduino connects to the HM-10 Bluetooth module through the Serial/SoftwareSerial pins
  • The HM-10 Bluetooth module is paired with a smartphone app
  • The Arduino sends data to the smartphone app by sending it to the Serial/SoftwareSerial
  • The Arduino receives data from the smartphone app by reading it from the Serial/SoftwareSerial
  • No additional BLE code is required on the Arduino.

Pinout

BLE Pinout

The HM-10 BLE Module has 6 pins:

  • BKR pin: to control behavior of the module. Ignore this pin if you are a beginner.
  • RX pin: Serial data pin, connect this pin to the TX pin of Arduino. The data received from this pin will be sent to Bluetooth.
  • TX pin: Serial data pin, connect this pin to the RX pin of Arduino. The data received via BLE will be sent to this pin as serial data.
  • GND pin: power pin, connect this pin to the GND of the power source.
  • VCC pin: power pin, connect this pin to 3.3V of Supply voltage
  • STATE pin: Indicates the working states:
    • Blink in standby mode - repeat 500ms pulse;
    • On in connection state - high level.

    ※ NOTE THAT:

    • We only need to use 4 pins: VCC, GND, RX, TX
    • Some manufacturers produce the HM-10 with only four pins.

About Bluetooth Serial Monitor App

The Bluetooth Serial Monitor App is a mobile application that mimics the appearance of the Serial Monitor in Arduino IDE. It allows communication with an Arduino board through Bluetooth without the need for additional code in the Arduino sketch. To use it, follow these steps:

  • Connect the Arduino to an HM-10 Bluetooth module
  • Download and install the Bluetooth Serial Monitor App on your smartphone
  • Open the app and connect it to the HM-10 Bluetooth module

With these steps completed, you can now send and receive data from the Arduino as if you were using the Serial Monitor in Arduino IDE, without the need to modify your existing Arduino code or add any Bluetooth-specific code.

Wiring Diagram

Arduino BLE Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The Wiring Table

Arduino Pins HM-10 Bluetooth Pins
RX (Pin 0) TX
TX (Pin 1) RX
5V VCC
GND GND
BKR (NOT connected)
STATE (NOT connected)

※ NOTE THAT:

The Arduino code can use other pins by replacing the Serial object with an alternative such as Serial1, Serial2, or SoftwareSerial (if available).

How To Program For Bluetooth

No specialized Bluetooth code is necessary, only Serial code is needed.

Arduino sends data to Bluetooth App on Smartphone

To transmit data from an Arduino board to a Bluetooth app on a smartphone, the following Arduino code can be used. This example demonstrates sending the message “Arduino here, command me!” from the Arduino to the smartphone app every second

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ble */ void setup() { Serial.begin(9600); } void loop() { Serial.println("Arduino here, command me!"); delay(1000); }

Quick Steps

To use the code and connect an Arduino board to a smartphone via BLE, follow these steps:

  • Install the Bluetooth Serial Monitor App on your smartphone.
  • Connect the HM-10 Bluetooth module to the Arduino board according to the wiring diagram provided.
  • Open the Arduino IDE, copy the code provided above, and upload it to the Arduino board. If you are unable to upload the code, disconnect the TX and RX pins from the Bluetooth module, upload the code, and then reconnect the RX/TX pins.
  • Open the Serial Monitor on the Arduino IDE.
  • On your smartphone, open the Bluetooth Serial Monitor App and select the BLE mode.
Bluetooth Serial Monitor App
  • Pair the smartphone with the HM-10 Bluetooth module.
Bluetooth Serial Monitor pairing
  • Observe the result on the Android App.
Bluetooth Serial Monitor App
  • Observe the result on the Serial Monitor of Arduino IDE.
COM6
Send
Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

If you follow the steps and run the code, you will observe that the data displayed on the Serial Monitor of the Arduino IDE and on the Android app will match.

Bluetooth App Send data To Arduino

The code below performs the following actions:

  • Send data from the Bluetooth app to the Arduino board
  • The Arduino board will read the incoming data and then sends a response back to the Bluetooth device.
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ble */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins void setup() { Serial.begin(9600); } void loop() { Serial.println(F("Arduino here, command me!")); if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "LED OFF") { Serial.println("LED is turned OFF"); // reports action to smartphone app // TODO: control your LED here } else if (command == "LED ON") { Serial.println("LED is turned ON"); // reports action to smartphone app // TODO: control your LED here } } delay(500); }

Quick Steps

Here are the steps to use the code with Arduino and an Android app:

  • Open the Arduino IDE and paste the code provided above.
  • Click the Upload button to upload the code to the Arduino board.
  • Open the Serial Monitor on the Arduino IDE.
  • Open Android App and pair it with the HM-10 Bluetooth module using the instructions provided in a previous example.
  • Once connected, type "LED ON" or "LED OFF" in the Android app and press the "SEND" button.
Bluetooth Serial Monitor App
  • The Arduino board will receive the data, print a response to the Serial port. This data will be sent to the Bluetooth app.
  • Check the result on the Android app.
Bluetooth Serial Monitor App
  • Check the result on the Serial Monitor of the Arduino IDE
COM6
Send
Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! LED ON LED is turned ON Arduino here, command me! Arduino here, command me! Arduino here, command me! LED OFF LED is turned OFF Arduino here, command me! Arduino here, command me!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

When you complete the steps above, you will notice that the information displayed on the Serial Monitor of the Arduino IDE and the Android app will be the same

Arduino Code - Control LED with smartphone App via BLE

The following Arduino example code utilizes commands “ON” and “OFF” received through the Bluetooth Serial Monitor App to turn on/off a built-in LED.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ble */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #define LED_PIN 8 void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); // set the digital pin as output: } void loop() { if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "OFF") { digitalWrite(LED_PIN, LOW); // turn off LED Serial.println("LED is turned OFF"); // reports action to smartphone app } else if (command == "ON") { digitalWrite(LED_PIN, HIGH); // turn on LED Serial.println("LED is turned ON"); // reports action to smartphone app } } }

You can see the instructions in more detail in Arduino controls LED via Bluetooth/BLE tutorial

Arduino Code - Control Servo Motor with smartphone App via BLE

The following Arduino code receives an angle value from the Bluetooth Serial Monitor App to control the angle of a servo motor.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ble */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #include <Servo.h> Servo servo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { Serial.begin(9600); servo.attach(11); // attaches the servo on pin 11 to the servo object } void loop() { if (Serial.available()) { // if there is data comming int angle = Serial.parseInt(); if (angle >= 0 && angle <= 180) { servo.write(angle); // rotate servo Serial.print("Rotated servo to angle: ");// reports action to smartphone app Serial.println(angle); } else { Serial.print("Invalid angle: ");// reports invalid value to smartphone app Serial.println(angle); } } }

You can see the instructions in more detail in Arduino controls Servo Motor via Bluetooth/BLE tutorial

If the Bluetooth Serial Monitor app is useful for you, please give it a 5-star rating on Play Store. Thank you!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES