Adsense HTML/JavaScript

Saturday, July 2, 2022

MicroPython/ESP32-C3-DevKitM-1 exercise: onboard BOOT button, and RGB LED (Neopixel).


Run on Espressif ESP32-C3-DevKitM-1 with MicroPython v1.19.1 on 2022-06-18 installed, the following exercise detect onboard BOOT button, and control onboard RGB LED (Neopixel).


mpyESP32-C3-DevKitM-1_neopixel.py
Simple testing on onboard RGB LED (Neopixel).
import machine
import time
import neopixel

"""
MicroPython v1.19.1/ESP32-C3-DevKitM-1 exercise:
Simple testing on onboard RGB LED (Neopixel).
"""

# On Espreffif ESP32-C3-DevKitM-1:
# The onboard RGB LED (WS2812) is connected to GPIO8

np = neopixel.NeoPixel(machine.Pin(8), 1)

while True:
    np[0] = (0, 0, 0)
    np.write()
    time.sleep(1)
    np[0] = (255, 0, 0)
    np.write()
    time.sleep(1)
    np[0] = (0, 255, 0)
    np.write()
    time.sleep(1)
    np[0] = (0, 0, 255)
    np.write()
    time.sleep(1)
    np[0] = (255, 255, 255)
    np.write()
    time.sleep(1)
    

mpyESP32-C3-DevKitM-1_neopixel_2.py
Control onboard RGB LED (Neopixel), with level control.
import machine
import time
import neopixel

"""
MicroPython v1.19.1/ESP32-C3-DevKitM-1 exercise:
Control onboard RGB LED (Neopixel), with level control.
"""

# On Espreffif ESP32-C3-DevKitM-1:
# The onboard RGB LED (WS2812) is connected to GPIO8

np = neopixel.NeoPixel(machine.Pin(8), 1)

def setNeoPixel(level, enable):
    np[0] = (level * enable[0],
             level * enable[1],
             level * enable[2])
    np.write()
    
def testNeoPixel(enable):
    for l in range(0, 256):
        setNeoPixel(l, enable)
        time.sleep(0.02)

while True:
    np[0] = (0, 0, 0)
    np.write()
    time.sleep(1)

    testNeoPixel([True, False, False])
    testNeoPixel([False, True, False])
    testNeoPixel([False, False, True])
    
    testNeoPixel([True, True, False])
    testNeoPixel([False, True, True])
    testNeoPixel([True, False, True])
    
    testNeoPixel([True, True, True])


mpyESP32-C3-DevKitM-1_button.py
Simple test onboard BOOT button, and verify the logic.
import machine
import time

"""
MicroPython v1.19.1/ESP32-C3-DevKitM-1 exercise:
Simple test onboard BOOT button, and verify the logic.
"""

# On Espreffif ESP32-C3-DevKitM-1:
# The onboard BOOT Button is connected to GPIO9

button_BOOT = machine.Pin(9,
                          machine.Pin.IN,
                          machine.Pin.PULL_UP)

while True:
    time.sleep(0.5)
    print(button_BOOT.value())

mpyESP32-C3-DevKitM-1_button_neopixel.py
Read BOOT button and turn on/off onboard RGB accordingly.
import machine
import time
import neopixel

"""
MicroPython v1.19.1/ESP32-C3-DevKitM-1 exercise:
Read BOOT button and turn on/off onboard RGB accordingly.
"""

# On Espreffif ESP32-C3-DevKitM-1:
# The onboard RGB LED (WS2812) is connected to GPIO8
# The onboard BOOT Button is connected to GPIO9

button_BOOT = machine.Pin(9,
                          machine.Pin.IN,
                          machine.Pin.PULL_UP)
np = neopixel.NeoPixel(machine.Pin(8), 1)

while True:
    time.sleep(0.2)
    if (button_BOOT.value()):  # button released
        np[0] = (0, 0, 0)
    else:                      # button pressed
        np[0] = (0, 3, 0)
    np.write()

mpyESP32-C3-DevKitM-1_button_irq.py
Implement IRQ handler to detect BOOT button pressing, and toggle onboard RGB.
import machine
import time
import neopixel

"""
MicroPython v1.19.1/ESP32-C3-DevKitM-1 exercise:
Implement IRQ handler to detect BOOT button pressing,
and toggle onboard RGB.

* No debouncing for button detection here.
"""

# On Espreffif ESP32-C3-DevKitM-1:
# The onboard RGB LED (WS2812) is connected to GPIO8
# The onboard BOOT Button is connected to GPIO9

button_BOOT = machine.Pin(9,
                          machine.Pin.IN,
                          machine.Pin.PULL_UP)
np = neopixel.NeoPixel(machine.Pin(8), 1)

np[0] = (0, 0, 0)
last_np_state = False
def toggle_LED():
    global last_np_state
    last_np_state =  not last_np_state

    if last_np_state:
        np[0] = (0, 0, 5)
    else:
        np[0] = (0, 0, 0)
    np.write()
    
def boot_pressed_handler(pin):
    print("BOOT button pressed:\t", pin)
    toggle_LED()
    
button_BOOT.irq(trigger=machine.Pin.IRQ_FALLING,
                handler=boot_pressed_handler)

while True:
    pass


No comments:

Post a Comment