In this tutorial, we will learn about HC-05 Bluetooth to serial port module and how to interface it with Raspberry Pi Pico. This Bluetooth device operates on UART communication. In other words, it uses serial communication to transmit and receive data serially over standard Bluetooth radiofrequency. Therefore, we will use TX, RX pins of Raspberry Pi Pico to connect with the module.
In this tutorial, we will use an android application to communicate with the Raspberry Pi Pico through the HC-05 Bluetooth module. We will see an example to control an LED from the Android application using Bluetooth wireless communication. The LED will be connected with a GPIO pin of the Raspberry Pi Pico and in effect, we will be controlling that pin.
Prerequisites
Before we start this lesson, make sure you are familiar with and have the latest version of Python3 installed in your system and set up MicroPython in your Raspberry Pi Pico. Additionally, you should have a running Integrated Development Environment(IDE) to do the programming. We will be using the same Thonny IDE as we have done previously when we learned how to blink and chase LEDs in MicroPython here:
If you are using uPyCraft IDE, you can check this getting started guide:
Bluetooth Terminal Application
Throughout this guide, we will use an android smartphone that will connect to our Raspberry Pi Pico. So, make sure you have an android phone at hand. We will also use a Bluetooth terminal application to pair the two devices together.
Go to the Play Store and download the application by the name: Serial Bluetooth terminal. This can be seen in the figure below.
HC-05 Introduction
HC-05 is one of the commonly used Bluetooth device that uses a UART communication protocol. The HC-05 Bluetooth is much different in features from all other Bluetooth devices because of its multiple pins and their functions. It has multiple pins for the different method which makes it unique as compared to others. The module normally operates at UART serial communication with TX and RX pins at 9600 baud rates. However, it can be programmed with AT commands and it supports 9600,19200,38400,57600,115200,230400,460800 baud rates.
Its CSR Bluecore 04-External single chip is already configured to communicate with other Bluetooth devices through serial communication. It offers a two-way communication method and the HC-05 can act as either slave and master. The Bluetooth module offers only short distance communications due to its limitation but still, most of the devices come with it due to its speed and security. The limitation of this device is that it doesn’t allow to transfer any kind of media.
In short, this module can be used to perform low-cost wireless communication between microcontrollers such as Arduino, TM4C123, Pic Microcontroller, Raspberry Pi, BeagleBone, STM32, and Arduino Mega, etc. Also, we can use it for communication between a microcontroller and PC or Android application. There are many Android applications available in the Google Play store which we can use to send and receive data to/from HC05. For this tutorial, we will use ‘Serial Bluetooth Terminal’ application that we installed previously.
Recommended Reading: HC-05 Bluetooth Module
Pinout
The HC-05 comes with multiple pins and indicators, which helps to control different operations and view their states through indicators. This pinout diagram provides indications of all pins.
The table below also briefly describes the functionality of each pin.
Pin | Description |
VCC | The operating voltage range is 3.3 volts. But I/O pins can withstand voltage of up to 5 volts. Therefore, we can connect 5 volts power source to this pin, and also other pins can also operate on 5 volts signals such as Tx and Rx signals. |
GND | Ground reference of both the microcontroller and HC-05 should be at the same level. Therefore, we should connect a power supply, HC05, and Raspberry Pi Pico ground pins to each other. |
Tx | As discussed earlier, the HC-05 Bluetooth module uses UART communication to transmit data. This is a transmitter pin. The TX pin will be the data transfer pin of the module in UART. |
Rx | This pin is a data receiving the pin in UART communication. It is used to receive data from the microcontroller and transmits it through Bluetooth. |
State | The state shows the current state of the Bluetooth. It gives feedback to the controller about the connectivity of Bluetooth with another device. This pin has an internal connection with the onboard LED which shows the working of HC05. |
Enable/Key | Using an external signal, Enable/Key pin is used to change the HC-05 mode between data mode and command mode. The HIGH logic input will transfer the device in command mode and the LOW logic input will change the mode to data mode. By default, it works in data mode. |
Button | The command and data mode states are changeable through a button present on the module. |
LED | This pin shows the working status of module along with the State pin |
Interfacing HC-05 with Raspberry Pi Pico
In this section, we will show you how to connect the HC-05 module with Raspberry Pi Pico. The connection of the HC-05 Bluetooth module with Raspberry Pi Pico is very easy as we will be using the serial communication interface that consists of two pins TX and RX. As you know, in order to communicate with the HC-05 Bluetooth module, we need to use a UART communication port of the Raspberry Pi Pico board. The figure below shows the different RX and TX pins found on the Raspberry Pi Pico board that consists of the UART interface. It contains two UART channels: UART0 and UART1.
LED Control Example with HC-05, Raspberry Pi Pico and Android App
For demonstration purposes, we will control the Raspberry Pi Pico GPIO pin with an LED connected to it. This will be done through the android application via the Bluetooth module. We will control the LED by pressing buttons configured inside the android application to toggle the LED.
We will require the following components for this project.
Required Components:
- Raspberry Pi Pico
- HC-05 Bluetooth Module
- 5mm LED
- 220-ohm resistor
- Connecting Wires
- Breadboard
Schematic Raspberry Pi Pico and HC-05
Follow the schematic diagram below to connect all the devices together.
The HC-05 module has two serial input pins, both of which will be used for data communication. TX of HC-05 will be connected with RX of the Raspberry Pi Pico and RX of HC-05 will be connected with TX of the Raspberry Pi Pico. We will use UART0 TX GP0 and RX GP1 pins. The VCC pin of the HC-05 module will be connected with 5V power supply and the grounds will be common.
We will use GP2 to connect with the anode pin of LED, and the cathode pin with the common ground through the 220-ohm resistor. Moreover, all the grounds will be in common.
The data will be transferred at the default baud rate of 9600 but it can also be changed.
Setting up Android App
In your smartphone settings, enable Bluetooth and scan for available Bluetooth devices. You will see the HC-05 device in your scanned list. The default name for this Bluetooth device is “HC-05” and the default pin code is either “0000” or “1234”.
We will use an android smartphone to connect with our Raspberry Pi Pico. To do that you will have to perform a series of steps.
After you have installed the ‘Serial Bluetooth Terminal app, open it. On the top left corner of the screen, you will find three horizontal bars. Tap it.
Now, tap on the Devices tab.
In the ‘available devices’ section, select ‘HC-05’
After you have selected the HC-05 module then tap the ‘link’ icon at the top of the screen. At first, you will get the message: ‘Connecting to HC-05.’ After a successful connection, you will get the message: ‘Connected.’
Now we will configure the buttons that will be used to toggle the LED. First click the M1 button as shown below:
Now add the details. Here we are configuring this button as “LED ON” button with a value associated with it. Press the tick icon at the top right to confirm the changes.
Likewise, create another button e.g. M2 as “LED OFF” button and associate a value with it as well.
Now we will have two buttons through which we will be able to control the state of the LED.
MicroPython: Raspberry Pi Pico LED Control with HC-05 and Android App
from machine import Pin,UART
uart = UART(0,9600)
Led_pin = 2
led = Pin(Led_pin, Pin.OUT)
while True:
if uart.any():
data = uart.readline()
print(data)
if data== b'1':
led.high()
print("LED is now ON!")
elif data== b'0':
led.low()
print("LED is now OFF!")
How the Code Works?
We will start by importing the Pin and UART classes from the machine module.
from machine import Pin,UART
Then we will create an uart object by using UART() and specify the UART channel as the first parameter and the baud rate as the second parameter. We are using UART0 in this case with baud rate 9600 for the uart communication.
uart = UART(0,9600)
Next, we will define the Raspberry Pi Pico GPIO pin that we will connect with the LED. It is GP2 in our case. You can use any appropriate output pin.
Led_pin = 2
We will configure the led pin as an output through Pin() function and pass the GPIO pin as the first parameter and Pin.OUT as the second parameter inside it.
led = Pin(Led_pin, Pin.OUT)
Inside the infinite loop we will first check if there is any data being received through the UART channel. If there is any data it will get saved in the variable ‘data.’ This will be achieved by using uart.readline(). We will press each button and see what we obtain in the shell terminal of Thonny the first time.
while True:
if uart.any():
data = uart.readline()
print(data)
In our case when we pressed the ‘LED ON‘ button, we obtained b’1′ in the shell terminal. Likewise when we pressed ‘LED OFF‘ button, we obtained b’0′ in the terminal. We will use these characters inside the if-elif statements to check which button was pressed.
You have to check for yourself what characters do you get for your buttons and use them in the script appropriately.
If ‘LED ON’ button was pressed, then the LED will turn ON and print “LED is now ON!” in the shell terminal of Thonny.
Similarly, if ‘LED OFF’ button was pressed, then the LED will turn OFF and print “LED is now OFF!” in the shell terminal of Thonny.
if data== b'1':
led.high()
print("LED is now ON!")
elif data== b'0':
led.low()
print("LED is now OFF!")
Demonstration
After you have selected the HC-05 module then tap the ‘link’ icon at the top of the screen. At first, you will get the message: ‘Connecting to HC-05.’ After a successful connection, you will get the message: ‘Connected.’
Now click the LED ON button, you will see the LED will turn ON. In response, the Raspberry Pi Pico sends an acknowledgment message and you will get a “LED is now ON!” message on the Thonny shell terminal. Moreover, the value ‘1’ will be seen on the android application terminal for led on.
Similarly, if you press the LED OFF button, the LED will turn OFF. You will get a “LED is now OFF!” response message from the board. Moreover, the value ‘0’ will be seen on the android application terminal for led off.
Watch the video of the demo below:
Related Bluetooth tutorials and projects:
- HC06 Bluetooth Module Guide with Arduino Interfacing
- HC-05 Bluetooth Interfacing with TM4C123G Tiva C Launchpad – Keil uvision
- HM-10 Bluetooth Module – Interfacing Example with Arduino
- Introduction to Bluetooth Low Energy
- Control 2 DC Motors via Bluetooth and Arduino
- Bluetooth Based Home Automation
- Bluetooth based dc motor speed and direction control using Arduino
- HC-05 Bluetooth module interfacing with Arduino
- Bluetooth module HC 05 interfacing with pic microcontroller
- Bluetooth Controlled Robot using pic microcontroller