Raspberry Pi - BLE

This tutorial instructs you how to use Raspberry Pi to control BLE HM-10 Module. In detail, we will learn:

Raspberry Pi BLE

The purpose of this tutorial:

It is important to be aware that this tutorial focuses on Bluetooth Low Energy (BLE, Bluetooth 4.0). If you need information on Classic Bluetooth (Bluetooth 2.0), please consult a similar tutorial Raspberry Pi - Bluetooth.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×HM-10 Bluetooth Module
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of HM-10 Bluetooth Module

The HM-10 is a Serial BLE module that works as a Serial to Bluetooth Low Energy converter. It carries out the following tasks:

  • Taking data from the Serial RX pin and sending it to a paired device like a smartphone via BLE
  • Receiving data from BLE (from the paired device) and transmitting it to the Serial TX pin.

When a Raspberry Pi is used to communicate with a smartphone app (Android/iOS), the following steps take place:

  • Establishing a connection between the Raspberry Pi and the HM-10 Bluetooth module through the Serial pins
  • Pairing the HM-10 Bluetooth module with the smartphone app
  • Sending data from the Raspberry Pi to the smartphone app by writing it to the Serial
  • Receiving data from the smartphone app by reading it from the Serial
  • No extra BLE code is needed on the Raspberry Pi.

The BLE HM-10 Module Pinout

BLE pinout

The HM-10 BLE Module has 6 pins:

  • BKR pin: This pin is used to control the behavior of the module. If you are a beginner, you can ignore this pin.
  • RX pin: This is the serial data pin and it should be connected to the TX pin of Raspberry Pi. The data received from this pin will be sent to Bluetooth.
  • TX pin: This is the serial data pin and it should be connected to the RX pin of Raspberry Pi. The data received via BLE will be sent to this pin as serial data.
  • GND pin: This is the power pin and it should be connected to the GND of the power source.
  • VCC pin: This is the power pin and it should be connected to 3.3V of Supply voltage.
  • STATE pin: This pin indicates the working states. It will blink in standby mode with a 500ms pulse, and will be on in connection state at a high level.

※ NOTE THAT:

  • -
  • Only four pins are required to be used with the HM-10, which some manufacturers produce.
  • VCC, GND, RX, and TX are the four pins in question.

Overview of Bluetooth Serial Monitor App

To use the Bluetooth Serial Monitor App, you must first connect the Raspberry Pi to an HM-10 Bluetooth module. Then, download and install the app on your smartphone. Finally, open the app and connect it to the HM-10 Bluetooth module.

Once you have finished these steps, you will be able to send and receive data from the Raspberry Pi.

Wiring Diagram

The wiring diagram between Raspberry Pi and BLE

This image is created using Fritzing. Click to enlarge image

Table of Wiring. This table shows the wiring connections. 1. The Wiring Table:. A listing of the wiring connections. 2. Table of Wiring:. A diagram displaying the wiring connections.

Raspberry Pi Pins HM-10 Bluetooth Pins
RX (GPIO15) TX
TX (GPIO14) RX
5V VCC
GND GND
BKR (NOT connected)
STATE (NOT connected)

Raspberry Pi sends data to Bluetooth App on Smartphone

In order to send data from a Raspberry Pi board to a Bluetooth application on a smartphone, the following code can be used. This example shows how to transmit the message “Raspberry Pi here, command me!” from the Raspberry Pi to the app every one second.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install pyserial
  • Create a Python script file bluetooth_send.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-ble import serial from time import sleep # Define serial port for communication bluetooth = serial.Serial('/dev/ttyS0', baudrate=9600, timeout=1) # Main program try: while True: bluetooth.write(b"Raspberry Pi here, command me!\n") sleep(1) except KeyboardInterrupt: pass finally: bluetooth.close()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 bluetooth_send.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

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

  • Download and install the Bluetooth Serial Monitor App on your smartphone.
  • Connect the HM-10 Bluetooth module to the Raspberry Pi board as per the wiring diagram.
  • On your smartphone, open the Bluetooth Serial Monitor App and select the BLE mode.
Bluetooth Serial Monitor App
  • Connect the smartphone to the HM-10 Bluetooth module.
Bluetooth Serial Monitor pairing
  • Take a look at the outcome on the Android App.
Bluetooth Serial Monitor App
  • Take a look at the outcome displayed on the Terminal.
PuTTY - Raspberry Pi
Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me!

If you take the necessary steps and execute the code, you will see that the data shown on the Android app are the same.

Bluetooth App Send data To Raspberry Pi

The code executes the following:

  • Transmit data from the Bluetooth application to the Raspberry Pi board
  • The Raspberry Pi board reads the incoming data and then sends a response to the Bluetooth device.

Detailed Instructions

  • Create a Python script file bluetooth_send_receive.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-ble import serial from time import sleep # Define serial port for Bluetooth communication bluetooth = serial.Serial('/dev/ttyS0', baudrate=9600, timeout=1) # Main program try: bluetooth.flushInput() # Clear any existing data in the input buffer while True: bluetooth.write(b"Raspberry Pi here, command me!\n") if bluetooth.inWaiting() > 0: command = bluetooth.readline().decode('utf-8').strip() # Read until newline character if command == "LED OFF": print("LED is turned OFF") # Report action to console # TODO: Control your LED here elif command == "LED ON": print("LED is turned ON") # Report action to console # TODO: Control your LED here # Add a delay to avoid excessive looping sleep(0.5) except KeyboardInterrupt: pass finally: bluetooth.close()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 bluetooth_send_receive.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

These are the instructions to use the code with Raspberry Pi and an Android app:

  • Start the Android App and pair it with the HM-10 Bluetooth module using the steps from a prior example.
  • After connecting, type "LED ON" or "LED OFF" in the Android app and press the "SEND" button.
Bluetooth Serial Monitor App
  • The Raspberry Pi board will receive the data and print a response to the Serial port. Afterwards, this data will be sent to the Bluetooth app. Finally, the result can be checked on the Android app.
Bluetooth Serial Monitor App
  • Check out the output on the Terminal.
PuTTY - Raspberry Pi
Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! LED ON LED is turned ON Raspberry Pi here, command me! Raspberry Pi here, command me! Raspberry Pi here, command me! LED OFF LED is turned OFF Raspberry Pi here, command me! Raspberry Pi here, command me!

Once you have finished the steps mentioned above,. you will observe that the data displayed on the Android app are identical.

Raspberry Pi Code - Control LED with smartphone App via BLE

Please refer to the Raspberry Pi controls LED via BLE tutorial.

Raspberry Pi Code - Control Servo Motor with smartphone App via BLE

Please refer to the Raspberry Pi controls Servo Motor via BLE tutorial.

Video Tutorial