042 - MicroPython TechNotes: JDY-32 | Bluetooth Low Energy BLE

Introduction

In this article, I will show you on how we can use a Bluetooth Low Energy module with ESP32 using MicroPython. Bluetooth modules are useful for controlling applications such as in robotics or for displaying sensor values in IOT projects. BLE stands for Bluetooth Low Energy. It provides low power consumption because it stays in sleep mode most of the time.

Bill Of Materials

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

Pinout

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

Hardware Instruction

  1. First, attach the ESP32 development board on top of Gorillacell ESP32 shield and make sure that both USB port are on the same side.
  2. Next, attach the dupont wires to Bluetooth 4.0 module by following the color coding that is black for the ground, red for the VCC, yellow for the TX pin, and white for the RX pin.
  3. Next, attach the other end of the dupont wires to the ESP32 shield by matching the colors of the wires to the colors of the pin headers such that black is to black, red is to red, and yellow and following colors to yellow pin headers.
  4. Next, power the Gorillacell ESP32 shield with an external power supply by connecting a type-C USB cable. Make sure that the power switch is set to ON state.
  5. Lastly, connect the ESP32 to the computer by attaching a micro USB cable.

Software Instruction

  1. Copy the sample source code from the SOURCE CODE section and paste it to your Thonny IDE.
  2. Please feel free to modify it according to your needs.

Video Demonstration

Call To Action

If you have any concern regarding this video, please write your question in the comment box. You might also liked to support my journey on Youtube by subscribing on my channel, TechToTinker. Click this to Subscribe. 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 of BLE:

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

2. Example # 2, demonstrating sending and transmitting BLE messages:

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

References And Credits

  1. Purchase your Gorillacell ESP32 development kits: https://gorillacell.kr/

  2. JDY-32 Datasheet http://myosuploads3.banggood.com/products/20190517/20190517042519JDY32.pdf



Posts in this series



No comments yet!

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