Adsense HTML/JavaScript

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:

No comments:

Post a Comment