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

In NodeMCU ESP-C3-32S-Kit, there are Cool, Warm and a three-in-one RGB lamp on board.
- IO3  : RGB red
- IO4  : RGB green
- IO5  : RGB blue
- IO18 : Warm LED
- IO19 : Cool LED

Here is exercises to control ESP-C3-32S-Kit onboard LEDs in MicroPython:


mpy_NodeMCU_ESPC3_RGB.py
"""
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_ESPC3_PWM.py
"""
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 os
import sys
import time
from machine import Pin, PWM

print()

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.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)
Actually, it's copy from my another old blogspot Embedded things > MicroPython/NodeMCU ESP-C3-32S-Kit to control onboard LEDs. I repost it, because I have some following exercise.


In NodeMCU ESP-C3-32S-Kit, Warm and Cool, R/G/B LEDs share common current limit resistors.

In this video, it can be noted that the Cool LED is affected by Warm LED. 


Check Section 5 Schematic diagram (page 15) in ESP-C3-32S-Kit Specification. It because Cool and Warm LEDs share a common current limit resistors. And also, R, G and B share common current limit resistors.

mpy_NodeMCU_ESPC3_PWM_prepare.py
"""
MicroPython/NodeMCU ESP-C3-32S-Kit exercise
prepare LEDs PWM.

After this code finished and return to REPL,
you can manual set LEDs in REPL consol with:
pwmR.duty(d)
pwmG.duty(d)
pwmB.duty(d)
pwmWarm.duty(d)
pwmCool.duty(d)
where d is the pwm duty to be set, 0~1023.

ref:

for PWM (pulse width modulation) ~
https://docs.micropython.org/en/latest/esp32/quickref.html#pwm-pulse-width-modulation

for LEDs connection, refer Section 5 Schematic diagram (page 15) in
ESP-C3-32S-Kit Specification: ~
https://docs.ai-thinker.com/_media/esp32/docs/esp-c3-32s-kit-v1.0_specification.pdf
"""
import os
import sys
from machine import Pin, PWM

print()

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

# 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)

#All OFF
pwmR.duty(0)
pwmG.duty(0)
pwmB.duty(0)
pwmWarm.duty(0)
pwmCool.duty(0)

Next:
Run tasks (adjusting LEDs pwm) concurrently, base on time ticks, uasyncio and _thread.


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.