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
- ESP32 development board.
- Gorillacell ESP32 shield.
- 4-pin female-female dupont wires.
- Gorillacell Bluetooth 4.0 BLE module.
Pinout
- GND – for the grounds.
- VCC – for the supply voltage.
- TX – Serial UART transmit pin.
- RX – Serial UART receive pin.
Hardware Instruction
- First, attach the ESP32 development board on top of Gorillacell ESP32 shield and make sure that both USB port are on the same side.
- 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.
- 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.
- 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.
- Lastly, connect the ESP32 to the computer by attaching a micro USB cable.
Software Instruction
- Copy the sample source code from the SOURCE CODE section and paste it to your Thonny IDE.
- 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
-
Purchase your Gorillacell ESP32 development kits: https://gorillacell.kr/
-
JDY-32 Datasheet http://myosuploads3.banggood.com/products/20190517/20190517042519JDY32.pdf
Posts in this series
- 049 - MicroPython TechNotes: MP3 Player
- 048 - MicroPython TechNotes: Analog Touch Sensor
- 047 - MicroPython TechNotes: E108 GPS
- 046 - MicroPython TechNotes: RF433 Transceivers
- 045 - MicroPython TechNotes: Infrared Transmitter
- 044 - MicroPython TechNotes: Infrared Receiver
- 043 - MicroPython TechNotes: ESP12E WiFi | External WiFi module
- 041 - MicroPython TechNotes: Bluetooth HC-06
- 040 - MicroPython TechNotes: Relay
- 039 - MicroPython TechNotes: Electromagnet
- 038 - MicroPython TechNotes: Buzzer
- 037 - MicroPython TechNotes: Servo Motor
- 036 - MicroPython TechNotes: Stepper Motor
- 035 - MicroPython TechNotes: Dual Motor Driver
- 034 - MicroPython TechNotes: DC Motors | Gear Motor and Fan Motor
- 033 - MicroPython TechNotes: TCS34725 RGB Color Sensor
- 032 - MicroPython TechNotes: BMP280 Sensor
- 031 - MicroPython TechNotes: TOF Distance Sensor
- 030 - MicroPython TechNotes: DS3231 RTC
- 029 - MicroPython TechNotes: HC-SR04 Ultrasonic Sensor
- 028 - MicroPython TechNotes: DHT11 Temperature and Humidity Sensor
- 027 - MicroPython TechNotes: Rotary Encoder
- 026 - MicroPython TechNotes: Light Dependent Resistor (LDR)
- 025 - MicroPython TechNotes: Joystick
- 024 - MicroPython TechNotes: Slider Switch
- 023 - MicroPython TechNotes: Continuous Rotation Potentiometer
- 022 - MicroPython TechNotes: Potentiometer | Reading an Analog Input
- 021 - MicroPython TechNotes: Color Touch Sensor
- 020 - MicroPython TechNotes: Touch Sensor
- 019 - MicroPython TechNotes: Switch Module
- 018 - MicroPython TechNotes: Button | Reading an Input
- 017 - MicroPython TechNotes: LASER Module
- 016 - MicroPython TechNotes: RGB LED Matrix
- 015 - MicroPython TechNotes: Neopixel 16
- 014 - MicroPython TechNotes: 8x8 Dot Matrix Display (I2C)
- 013 - MicroPython TechNotes: 8x16 Dot Matrix Display (SPI)
- 012 - MicroPython TechNotes: 8x8 Dot Matrix Display (SPI)
- 011 - MicroPython TechNotes: 1.3 OLED Display
- 010 - MicroPython TechNotes: 0.96 OLED Display
- 009 - MicroPython TechNotes: 7 Segment Display
- 008 - MicroPython TechNotes: 16x2 LCD
- 007 - MicroPython TechNotes: RGB LED
- 006 - MicroPython TechNotes: Traffic Light LED Module
- 005 - MicroPython TechNotes: Gorilla Cell LED | MicroPython Hello World
- 004 - MicroPython TechNotes: Gorilla Cell I/O Devices
- 003 - MicroPython TechNotes: Gorillacell ESP32 Shield
- 002 - MicroPython TechNotes: Introduction for Gorillacell ESP32 Dev Kit
- 001 - MicroPython TechNotes: Get Started with MicroPython
- 000 - MicroPython TechNotes: Unboxing Gorillacell ESP32 Development Kit
No comments yet!