Adsense HTML/JavaScript

Saturday, November 28, 2020

nanoESP32-S2/CircuitPython control on-board NeoPixel, using Adafruit CircuitPython Library Bundle

With CircuitPython installed to nanoESP32-S2, we are going to program nanoESP32-S2 with CircuitPython, using neopixel library come from Adafruit CircuitPython Library Bundle.


The CircuitPython Library Bundle contains all current libraries available for CircuitPython.

To use neopixel library, we have to install Adafruit CircuitPython Library Bundle to our CircuitPython board, nanoESP32-S2. 

- Visit CircuitPython Libraries, download the appropriate bundle for your version of CircuitPython, it is adafruit-circuitpython-bundle-6.x-mpy-20201126.zip in my case. Unzip the file, open the resulting folder and find the lib folder. Open the lib folder and find the library files you need to load. Copy the individual library files you need to the lib folder on your CIRCUITPY drive.

- And we have to check the IO assigned to nanoESP32-S2 on-board NeoPixel.
Visit GitHub wuxx/nanoESP32-S2/schematic/nanoESP32S2-v1.2.pdf to check the schematic. 

Found that the IO assigned to DIN of WS2812B is GPIO18, board.IO18 in CircuitPython.  Actually, board.NEOPIXEL is defined as board.IO18.

WS2812B is a intelligent control LED light source that the control circuit and RGB chip are integrated in a package of 5050 components, the NeoPixel.

- Edit code.py in CIRCUITPY, save and run it:
import time
import os
import microcontroller
import neopixel
import board

def cycleNeopixel(wait):
    for r in range(255):
        pixel[0] = (r, 0, 0)
        time.sleep(wait)
    for r in range(255, 0, -1):
        pixel[0] = (r, 0, 0)
        time.sleep(wait)
        
    for g in range(255):
        pixel[0] = (0, g, 0)
        time.sleep(wait)
    for g in range(255, 0, -1):
        pixel[0] = (0, g, 0)
        time.sleep(wait)
        
    for b in range(255):
        pixel[0] = (0, 0, b)
        time.sleep(wait)
    for b in range(255, 0, -1):
        pixel[0] = (0, 0, b)
        time.sleep(wait)
        
print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython NeoPixel example")
print("cpu.temperature: " + str(microcontroller.cpu.temperature))
print()
print("neopixel version: " + neopixel.__version__)
print()

# Create the NeoPixel object
pixel = neopixel.NeoPixel(board.IO18, 1, pixel_order=neopixel.RGB)
pixel[0] = (0, 0, 0)
time.sleep(2.0)

cycleNeopixel(0.01)

pixel[0] = (0, 0, 0)
time.sleep(2.0)

print("- bye -\n")


Check in the video:


It seem R and G is swapped in my unit, it should be GRB order for the build-in NeoPixel. To correct it, have to change "pixel_order=neopixel.RGB" to "pixel_order=neopixel.GRB" to match. It will be fixed in next exercise.

Next:

No comments:

Post a Comment