Adsense HTML/JavaScript

Sunday, June 26, 2022

MicroPython bluetooth (BLE) exampls, run on ESP32-C3.

This video just show how MicroPython bluetooth module examples run on ESP32-C3, Ai-Thinker NodeMCU ESP-C3-32S-Kit and Espressif ESP32-C3-DevKitM-1, both running MicroPython v1.19.1 on 2022-06-18. Finally, have bi-direction BLE communication between ESP32-C3 dev. boards.




MicroPython bluetooth module provides an interface to a Bluetooth controller on a board. Currently this supports Bluetooth Low Energy (BLE) in Central, Peripheral, Broadcaster, and Observer roles, as well as GATT Server and Client and L2CAP connection-oriented-channels. A device may operate in multiple roles concurrently. Pairing (and bonding) is supported on some ports.

Note: This module is still under development and its classes, functions, methods and constants are subject to change.

NEXT:
~ It's modified to send/receive command via BLE UART to control LED remotely.
ESP32-C3/MicroPython BLE UART Communication, with user input and display on SSD1306 I2C OLED.


Monday, June 20, 2022

MicroPython/NodeMCU ESP-C3-32S-Kit to control onboard LEDs

With MicroPython v1.19 firmware installed on Ai-Thinker NodeMCU ESP-C3-32S-Kit, this exercise control the onboard LEDs.


Refer to ESP-C3-32S-Kit Specification, there are Cool, Warm and a three-in-one RGB lamp on board.
- IO3  : RGB red lamp beads
- IO4  : RGB green lamp beads
- IO5  : RGB blue lamp beads
- IO18 : Warm color lamp beads
- IO19 : Cool color lamp beads
  (high level is valid)




Exercise code:

mpy_NodeMCU_ESP-C3-32S-Kit_RGB.py, control onboard LEDs as Digital Output.
"""
MicroPython/NodeMCU ESP-C3-32S-Kit exercise
to control RGB LED.
"""
import uos
import usys
from machine import Pin
import time

# NodeMCU ESP-C3-32S-Kit onboard LEDs assignment
pinR = Pin(3, Pin.OUT)
pinG = Pin(4, Pin.OUT)
pinB = Pin(5, Pin.OUT)
pinWarm = Pin(18, Pin.OUT)
pinCool = Pin(19, Pin.OUT)


print()

print("====================================")
print(usys.implementation[0], uos.uname()[3],
      "\nrun on", uos.uname()[4])
print("====================================")

while True:
    
    #All OFF
    pinR.value(0)
    pinG.value(0)
    pinB.value(0)
    pinWarm.value(0)
    pinCool.value(0)
    time.sleep(1)
    
    #turn ON WARM
    pinWarm.value(1)
    time.sleep(1)
    
    #turn ON COOL
    pinWarm.value(0)
    pinCool.value(1)
    time.sleep(1)
    
    #turn ON RED
    pinCool.value(0)
    pinR.value(1)
    time.sleep(1)
    
    #turn ON GREEN
    pinR.value(0)
    pinG.value(1)
    time.sleep(1)
    
    #turn ON BLUE
    pinG.value(0)
    pinB.value(1)
    time.sleep(1)
    
    #turn ON RED/GREEN/BLUE
    pinR.value(1)
    pinG.value(1)
    pinB.value(1)
    time.sleep(1)


mpy_NodeMCU_ESP-C3-32S-Kit_RGB_PWM.py, control onboard LEDs as PWM.
"""
MicroPython/NodeMCU ESP-C3-32S-Kit exercise
to control RGB LED (PWM).

# ref:
# https://docs.micropython.org/en/latest/esp32/quickref.html#pwm-pulse-width-modulation
"""
import uos
import usys
import time
from machine import Pin, PWM

print()

print("====================================")
print(usys.implementation[0], uos.uname()[3],
      "\nrun on", uos.uname()[4])
print("====================================")
time.sleep(1)

# NodeMCU ESP-C3-32S-Kit onboard LEDs assignment
pwmR = PWM(Pin(3))
pwmG = PWM(Pin(4))
pwmB = PWM(Pin(5))
pwmWarm = PWM(Pin(18))
pwmCool = PWM(Pin(19))

pwmR.freq(1000)    # set PWM frequency from 1Hz to 40MHz
pwmG.freq(1000)
pwmB.freq(1000)
pwmWarm.freq(1000)
pwmCool.freq(1000)

def PWMLedTest(pwmpin):
    for d in range(0, 1024):
        pwmpin.duty(d)
        time.sleep(0.005)
    for d in range(1023, -1, -1):
        pwmpin.duty(d)
        time.sleep(0.005)

while True:
    
    #All OFF
    pwmR.duty(0)
    pwmG.duty(0)
    pwmB.duty(0)
    pwmWarm.duty(0)
    pwmCool.duty(0)
    time.sleep(1)
    
    PWMLedTest(pwmR)
    time.sleep(0.5)
    PWMLedTest(pwmG)
    time.sleep(0.5)
    PWMLedTest(pwmB)
    time.sleep(0.5)
    PWMLedTest(pwmCool) 
    time.sleep(0.5)
    PWMLedTest(pwmWarm)

    time.sleep(1)

Updated@2022-08-15
It's found that Cool and Warm LEDs are mutual affected. It because Cool and Warm LEDs share a common current limit resistors. And also, R, G and B share common current limit resistors.

Check update post in my new blogspot coXXect > MicroPython/NodeMCU ESP-C3-32S-Kit control onboard LEDs



Sunday, June 19, 2022

Flash MicroPython v1.19 firmware on ESP32-C3 (ESP32-C3-DevKitM-1/NodeMCU ESP-C3-32S-Kit)

To flash MicroPython v1.19 firmware on ESP32-C3, tested on Espressif ESP32-C3-DevKitM-1 and AI-Thinker NodeMCU ESP-C3-32S-Kit, both have a single USB connector. All steps run on Raspberry Pi.



To IDENTIFY connected USB port. 

- BEFORE Connect ESP32-C3 dev. board to USB
clear dmesg buffer:
$ sudo dmesg -c

- AFTER ESP32-C3 dev. board connected to USB
display dmesg:
$ dmesg

Download firmware.

Visit https://micropython.org/download/  to download for esp32c3.

Select "ESP32-C3 Espressif"


Flash Firmware.

To erase the entire flash using:

esptool.py --chip esp32c3 --port /dev/ttyUSB0 erase_flash

Flash firmware starting at address 0x0:

esptool.py --chip esp32c3 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x0 <.bin>












Finally, test with Thonny.









more exercise:
MicroPython/NodeMCU ESP-C3-32S-Kit to control onboard LEDs
MicroPython bluetooth (BLE) exampls
detect onboard BOOT button, and control onboard RGB LED (Neopixel)
send/receive command via BLE UART
multithreading exercise, get user input un-blocked using _thread
ESP32-C3/MicroPython + SSD1306 I2C OLED
ESP32-C3/MicroPython BLE UART Communication