041 – MicroPython TechNotes: Bluetooth HC-06

In this article, I will talk about the (external) BLUETOOTH module with ESP32 using MicroPython.
BILL OF MATERIALS:
To follow this lesson you will need the following:
- ESP32 development board.
- Gorillacell ESP32 shield.
- 4-pin female-female dupont wires.
- Gorillacell Bluetooth module.

PINOUT:
- GND – for the ground pin.
- VCC – for the supply voltage.
- TX – for the UART transmit pin.
- RX – for the UART receive pin.
HARDWARE INSTRUCTION
- Connect the Bluetooth module GND pin to ESP32 GND pin.
- Connect the Bluetooth module VCC pin to ESP32 3.3V pin.
- Connect the Bluetooth module TX pin to ESP32 GPIO 23 pin.
- Connect the Bluetooth module RX pin to ESP32 GPIO 25 pin.
SOFTWARE INSTRUCTION:
- Copy the sample source code below and paste it to your Thonny IDE.
- Run it then modify according to your needs.
- Enjoy and happy learning.
VIDEO DEMONSTRATION:
For any concern, write your message in the comment section.
You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.
Thank you and have a good days ahead.
See you,
– George Bantique | tech.to.tinker@gmail.com
SOURCE CODE:
1. Example # 1, exploring the basics:
# More details can be found in TechToTinker.blogspot.com
# George Bantique | tech.to.tinker@gmail.com
from machine import UART
bt = UART(2, baudrate=9600, tx=25, rx=23)
# The following lines of code can be tested using the REPL:
# 1. To check if there is available serial data
# bt.any()
# 2. To read all data available:
# bt.read()
# 3. Or you can input the number of bytes as parameter
# you want to read from uart
# bt.read(num_here)
# 4. Or you can read 1 line
# bt.readline()
# 5. Now to write / transmit uart data
# bt.write(your_data)
2. Example # 2, controlling an output:
# More details can be found in TechToTinker.blogspot.com
# George Bantique | tech.to.tinker@gmail.com
from machine import Pin
from machine import UART
bt = UART(2, baudrate=9600, tx=25, rx=23)
led = Pin(2, Pin.OUT)
while True:
if bt.any()!=0:
msg = bt.read(bt.any()).decode().strip('rn')
print(msg)
if "ON" in msg:
led.value(1)
if "OFF" in msg:
led.value(0)
3. Example # 3, reading an input:
# More details can be found in TechToTinker.blogspot.com
# George Bantique | tech.to.tinker@gmail.com
from machine import Pin
from machine import UART
from machine import Timer
from time import ticks_ms
bt = UART(2, baudrate=9600, tx=25, rx=23)
led = Pin(2, Pin.OUT)
sw = Pin(0, Pin.IN)
tim0 = Timer(0)
t_start = ticks_ms()
while True:
if bt.any()!=0:
#msg = bt.read(bt.any()).decode().strip('rn')
#print(msg)
msg = bt.read(bt.any()).decode()
if "ON" in msg:
led.value(1)
tim0.deinit()
print('LED is ON')
elif "OFF" in msg:
led.value(0)
tim0.deinit()
print('LED is OFF')
elif "BLINK" in msg:
tim0.init(period=250, mode=Timer.PERIODIC, callback=lambda t: led.value(not led.value()))
print('LED is blinking')
else:
print(msg.strip('rn'))
if ticks_ms()-t_start >= 300:
if sw.value()==0: # BOOT button is pressed
bt.write('Boot button is pressed.rn')
print('Sending "Boot button is pressed."')
t_start=ticks_ms()
REFERENCES AND CREDITS:
1. Purchased your Gorillacell ESP32 development kit at:
WOW your article is amazing.Many interesting information. recently I got interested in the topic UART https://www.micros.com.pl/uklady-scalone/uklady-interfejsowe/uart/ Maybe you will bring up such a topic
Do you mean like this? https://techtotinker.blogspot.com/2020/10/013-esp32-micropython-uart-serial-in.html