041 - MicroPython TechNotes: Bluetooth HC-06

Introduction

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:

  1. ESP32 development board.
  2. Gorillacell ESP32 shield.
  3. 4-pin female-female dupont wires.
  4. Gorillacell Bluetooth module.

Pinout

  1. GND – for the ground pin.
  2. VCC – for the supply voltage.
  3. TX – for the UART transmit pin.
  4. RX – for the UART receive pin.

Hardware Instruction

  1. Connect the Bluetooth module GND pin to ESP32 GND pin.
  2. Connect the Bluetooth module VCC pin to ESP32 3.3V pin.
  3. Connect the Bluetooth module TX pin to ESP32 GPIO 23 pin.
  4. Connect the Bluetooth module RX pin to ESP32 GPIO 25 pin.

Software Instruction

  1. Copy the sample source code below and paste it to your Thonny IDE.
  2. Run it then modify according to your needs.
  3. Enjoy and happy learning.

Video Demonstration

Call To Action

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:

 1# More details can be found in TechToTinker.blogspot.com
 2# George Bantique | tech.to.tinker@gmail.com
 3from machine import UART
 4bt = UART(2, baudrate=9600, tx=25, rx=23)
 5
 6# The following lines of code can be tested using the REPL:
 7# 1. To check if there is available serial data<br></br># bt.any()
 8# 2. To read all data available:<br></br>
 9# bt.read()<br></br>
10# 3. Or you can input the number of bytes as parameter
11#    you want to read from uart
12# bt.read(num_here)
13# 4. Or you can read 1 line
14# bt.readline()
15# 5. Now to write / transmit uart data
16# bt.write(your_data)<br></br>

2. Example # 2, controlling an output:

 1# More details can be found in TechToTinker.blogspot.com
 2# George Bantique | tech.to.tinker@gmail.com
 3from machine import Pin
 4from machine import UART
 5bt = UART(2, baudrate=9600, tx=25, rx=23)
 6led = Pin(2, Pin.OUT)
 7while True:
 8    if bt.any()!=0:
 9    msg = bt.read(bt.any()).decode().strip('rn')
10    print(msg)
11    if "ON" in msg:
12        led.value(1)
13    if "OFF" in msg:
14        led.value(0)

3. Example # 3, reading an input:

 1# More details can be found in TechToTinker.blogspot.com
 2# George Bantique | tech.to.tinker@gmail.com
 3from machine import Pin
 4from machine import UART
 5from machine import Timer
 6from time import ticks_ms
 7
 8bt = UART(2, baudrate=9600, tx=25, rx=23)
 9led = Pin(2, Pin.OUT)
10sw = Pin(0, Pin.IN)
11
12tim0 = Timer(0)
13t_start = ticks_ms()
14
15while True:
16    if bt.any()!=0:
17        #msg = bt.read(bt.any()).decode().strip('rn')
18        #print(msg)
19        msg = bt.read(bt.any()).decode()
20        if "ON" in msg:
21            led.value(1)
22            tim0.deinit()
23            print('LED is ON')
24        elif "OFF" in msg:
25            led.value(0)
26            tim0.deinit()
27            print('LED is OFF')
28        elif "BLINK" in msg:
29            tim0.init(period=250, mode=Timer.PERIODIC, callback=lambda t: led.value(not led.value()))
30            print('LED is blinking')
31        else:
32            print(msg.strip('rn'))
33            if ticks_ms()-t_start >= 300:
34                if sw.value()==0: # BOOT button is pressed
35                    bt.write('Boot button is pressed.rn')
36                    print('Sending "Boot button is pressed."')
37                    t_start=ticks_ms()

References And Credits

  1. Purchased your Gorillacell ESP32 development kit at: https://gorillacella.kr/


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.