Adsense HTML/JavaScript

Showing posts with label NeoPixel. Show all posts
Showing posts with label NeoPixel. Show all posts

Monday, December 7, 2020

nanoESP32-S2/CircuitPython exercise: to control external NeoPixel

Previous exercise show how to install Adafruit CircuitPython Library Bundle to nanoESP32-S2 dev. board, and drive the onboard NeoPixel. This post show how to drive external NeoPixel (Ring and Square).

IO45 is used to drive the 8X NeoPixel Ring. connect to its DI.
IO42 is used to drive the 4X4 NeoPixel Square, connect to its DI.
IO18 is used to drive the onboard NeoPixel.
In the exercise of ADCNeoPixel, IO1 is used as analog input, convert to digital and display on 8X NeoPixel Ring.

All the NeoPixels have color order of  GRB, so have to set pixel_order=neopixel.GRB.

Just copy the code to code.py on your nanoESP32-S2 board, save to run.

ADCNeoPixel.py
import time
import os
import microcontroller
import board
import analogio
import neopixel

adc01=analogio.AnalogIn(board.IO1)

NumOfPixel_ring = 8

def offAllPixel():
    pixel_ring.fill((0, 0, 0))
    onboard_pixel[0] = (0, 0, 0)
    pixel_ring.show()
    onboard_pixel.show()

print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython NeoPixel/ADC example")
print("cpu.temperature: " + str(microcontroller.cpu.temperature))
print("neopixel version: " + neopixel.__version__)
print()

ref = adc01.reference_voltage
print("reference voltage: " + str(ref))

# Create the NeoPixel objects
# onboard NeoPixel = IO18
onboard_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, 
                    auto_write=False,
                    brightness=0.02,
                    pixel_order=neopixel.GRB)
                    
pixel_ring = neopixel.NeoPixel(board.IO45, NumOfPixel_ring, 
                    auto_write=False,
                    brightness=0.3,
                    pixel_order=neopixel.GRB)

offAllPixel()
#blink neopixels to start
time.sleep(0.5)
onboard_pixel[0] = (255, 0, 0)
onboard_pixel.show()
time.sleep(0.5)
onboard_pixel[0] = (0, 255, 0)
onboard_pixel.show()
time.sleep(0.5)
onboard_pixel[0] = (20, 0, 255)
onboard_pixel.show()
time.sleep(0.5)
onboard_pixel[0] = (0, 0, 0)
onboard_pixel.show()

pixel_ring.fill((0, 0, 0))
pixel_ring.show()
time.sleep(1.0)

while True:
    value01=adc01.value
    step = int(value01/8192)
    offAllPixel()
    for i in range(step):
        pixel_ring[i] = (255, 255, 255)
    pixel_ring.show()
    
    onboard_pixel.brightness = step/10
    onboard_pixel[0] = (0, 0, 255)
    onboard_pixel.show()

    time.sleep(0.1)

print("- bye -\n")


testNeopixel.py
import time
import os
import microcontroller
import neopixel
import board
import random

NumOfPixel_ring = 8
NumOfPixel_sq = 16

def offAllPixel():
    fillAllPixel(0, 0, 0)
    
def fillAllPixel(r, g, b):
    pixel_ring.fill((r, g, b))
    pixel_sq.fill((r, g, b))
    onboard_pixel[0] = (r, g, b)


print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython NeoPixel example")
print("Test on:")
print("Onboard NEOPIXEL")
print("8X NeoPixel Ring")
print("4X4 NeoPixel Square")
print("cpu.temperature: " + str(microcontroller.cpu.temperature))
print()
print("neopixel version: " + neopixel.__version__)
print()

# Create the NeoPixel objects
# onboard NeoPixel = IO18
onboard_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, 
                    brightness=0.1,
                    pixel_order=neopixel.GRB)
                    
pixel_ring = neopixel.NeoPixel(board.IO45, NumOfPixel_ring, 
                    brightness=0.1,
                    pixel_order=neopixel.GRB)
                    
pixel_sq = neopixel.NeoPixel(board.IO42, NumOfPixel_sq, 
                    brightness=0.1, pixel_order=neopixel.GRB)

offAllPixel()

#blink neopixels to start
time.sleep(0.5)
onboard_pixel[0] = (255, 0, 0)
time.sleep(0.5)
onboard_pixel[0] = (0, 255, 0)
time.sleep(0.5)
onboard_pixel[0] = (20, 0, 255)
time.sleep(0.5)
onboard_pixel[0] = (0, 0, 0)

for i in range(NumOfPixel_ring):
    pixel_ring.fill((0, 0, 0))
    pixel_ring[i] = (255, 255, 255)
    time.sleep(0.5)
pixel_ring.fill((0, 0, 0))

for i in range(NumOfPixel_sq):
    pixel_sq.fill((0, 0, 0))
    pixel_sq[i] = (255, 255, 255)
    time.sleep(0.5)
pixel_sq.fill((0, 0, 0))
time.sleep(0.5)

fillAllPixel(255, 0, 0)
time.sleep(1.0)
fillAllPixel(0, 255, 0)
time.sleep(1.0)
fillAllPixel(0, 0, 255)
time.sleep(1.0)

offAllPixel()
time.sleep(2.0)

print("- bye -\n")

randomNeopixel.py
import time
import os
import microcontroller
import neopixel
import board
import random

NumOfPixel_ring = 8
NumOfPixel_sq = 16

def offAllPixel():
    fillAllPixel(0, 0, 0)
    
def fillAllPixel(r, g, b):
    pixel_ring.fill((r, g, b))
    pixel_sq.fill((r, g, b))
    onboard_pixel[0] = (r, g, b)
    
def randomNeopixel():

    for r in range(2000):
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        
        onboard_pixel[0] = (r, g, b)
        i = random.randint(0, NumOfPixel_ring-1)
        pixel_ring[i] = (r, g, b)
        i = random.randint(0, NumOfPixel_sq-1)
        pixel_sq[i] = (r, g, b)
        
        time.sleep(0.2)


print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython NeoPixel example")
print("Generate random color on:")
print("Onboard NEOPIXEL")
print("8X NeoPixel Ring")
print("4X4 NeoPixel Square")
print("cpu.temperature: " + str(microcontroller.cpu.temperature))
print()
print("neopixel version: " + neopixel.__version__)
print()

# Create the NeoPixel objects
# onboard NeoPixel = IO18
onboard_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, 
                    brightness=0.05,
                    pixel_order=neopixel.GRB)
                    
pixel_ring = neopixel.NeoPixel(board.IO45, NumOfPixel_ring, 
                    brightness=0.05,
                    pixel_order=neopixel.GRB)
                    
pixel_sq = neopixel.NeoPixel(board.IO42, NumOfPixel_sq, 
                    brightness=0.05, pixel_order=neopixel.GRB)

offAllPixel()

#blink neopixels to start
time.sleep(0.5)
onboard_pixel[0] = (255, 0, 0)
time.sleep(0.5)
onboard_pixel[0] = (0, 255, 0)
time.sleep(0.5)
onboard_pixel[0] = (20, 0, 255)
time.sleep(0.5)
onboard_pixel[0] = (0, 0, 0)

randomNeopixel()

offAllPixel()
time.sleep(2.0)

print("- bye -\n")


Related:

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: