Raspberry Pi Control LED via Bluetooth

This tutorial instructs you how to program Raspberry Pi to control a LED through either Bluetooth or BLE.

This tutorial supplies guidance for both modules.

We will use the Bluetooth Serial Monitor App on a smartphone to send commands to Raspberry Pi.

These commands include:

Raspberry Pi LED Bluetooth

Hardware Preparation

1×Raspberry Pi 4 Model B
1×HC-05 Bluetooth Module
1×(Alternative) HM-10 BLE Module
1×LED
1×220 ohm resistor
1×Breadboard
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 LED and Bluetooth Module

If you are unfamiliar with LED and Bluetooth Module (pinout, how it works, how to program ...), the following tutorials can help you learn:

Wiring Diagram

  • If you desire to manage LED through Bluetooth, the HC-05 Bluetooth module should be utilized in conjunction with the wiring diagram below.
The wiring diagram between Raspberry Pi and LED Bluetooth

This image is created using Fritzing. Click to enlarge image

  • If you desire to manipulate LED through BLE, the HM-10 BLE module should be used in conjunction with the wiring diagram given below.
The wiring diagram between Raspberry Pi and LED BLE

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - controls LED via Bluetooth/BLE

The code functions for both the HC-10 Bluetooth module and the HM-10 BLE module. It is applicable to both.

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_led.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-control-led-via-bluetooth import serial import RPi.GPIO as GPIO from time import sleep # Define pins LED_PIN = 16 # GPIO 16 (LED pin) # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) # 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": GPIO.output(LED_PIN, GPIO.LOW) # Turn off LED bluetooth.write(b"LED is turned OFF\n") # Report action to smartphone app elif command == "LED ON": GPIO.output(LED_PIN, GPIO.HIGH) # Turn on LED bluetooth.write(b"LED is turned ON\n") # Report action to smartphone app # Add a delay to avoid excessive looping sleep(0.1) except KeyboardInterrupt: pass finally: GPIO.cleanup() bluetooth.close()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 bluetooth_led.py
  • Download and install the Bluetooth Serial Monitor App on your smartphone.
  • Once the code is uploaded, open the Bluetooth Serial Monitor App on your smartphone and select either Classic Bluetooth or BLE depending on the module you are using.
Bluetooth Serial Monitor App
  • Connect the Bluetooth App to the HC-05 Bluetooth module or HM-10 BLE module.
Bluetooth Serial Monitor pairing
  • Enter either “ON” or “OFF” and press the Send button.
Bluetooth Serial Monitor App
  • Check the LED's state on the Raspberry Pi board. It will be either ON or OFF.
  • We can also observe the LED's state through the Bluetooth App.
  • View the outcome on the Android App.
Bluetooth Serial Monitor App

You may ponder how Raspberry Pi can comprehend an entire command? For instance, when we send “OFF” command, how ca Raspberry Pi distinguish if the command is “O”, “OF” or “OFF”?

When sending a command, The bluetooth App adds a newline character ('\n') by choosing the “newline” option on the App. Raspberry Pi will read data until it encounters the newline character. The newline character serves as a command separator.

If you find the Bluetooth Serial Monitor app helpful, please consider giving it a 5-star rating on Play Store. We would really appreciate it! Thank you!

Video Tutorial