Adsense HTML/JavaScript

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



No comments:

Post a Comment