On a rainy afternoon, I discovered a box of Neopixels in my workspace. These individually addressable RGB LEDs open up a world of possibilities for creative lighting projects. Neopixels can be used for various applications, such as creating intricate light displays, enhancing home decor, building interactive installations, and even developing wearable tech. Their versatility and vibrant colors make them a popular choice among hobbyists and professionals alike.
I decided to control these Neopixels using Micropython due to its simplicity and efficiency in handling microcontrollers. Combining Neopixels with Micropython seemed like the perfect challenge to enhance my programming skills and create dynamic lighting effects. This project aims to explore the potential of Neopixels and demonstrate the capabilities of Micropython in creating versatile and engaging light displays.
Hardware SetupWhat we need to do in our case is to connect the 5V power supply pin on the Neopixels to the VDD Pin on the board (black wire), the GND of the Neopixels with the GND of the board (red wire) and finally the DIN pin of the Neopixels with any input pin from the board, in our case PIN 9.1 (green wire).
Frist thing we need to have micropython ready on our board! To do that, all you need to do is click on this link and follow the instructions!
https://www.hackster.io/Infineon_Team/micropython-on-psoc-fcf1d0
Now let´s start with our project!
The first step to do is to import the necessary modules. Here, the code imports the modules for handling the GPIO pins and timing.
from machine import Pin, bitstream
import time
Next, we need to defining timing for the Bitstream. The timing list holds the timing values for the bitstream based on the LED's datasheet. These values are crucial for sending the correct signals to the LEDs.
timing = [500, 1125, 800, 750]
In this line, we set up the data pin (DIN1) for the LEDs, specifying it as an output pin and initializing its value to 0. In my case my neopixels were at pin 9.1.
DIN1 = Pin('P9_1', Pin.OUT, value=0)
Next step is creating a function for manually controlling all LEDs. This function manualLightAll is used to manually control all the LEDs using the bitstream function. The bitstream function in MicroPython is used to create the exact timing signals required for controlling Neopixel LEDs, allowing the user to send color and brightness information to the LEDs accurately.
def manualLightAll(buf):
bitstream(DIN1, 0, timing, buf)
Then we create a Color Pattern by combining the values for red, green and blue into a bytearray.
def create_color(red, green, blue):
return bytearray([red, green, blue])
Animation Cycle Function: The animation_cycle
function is responsible for cycling through a color pattern on the LEDs. It iterates through the specified amount of LEDs, creating a color pattern for each and displaying it using manualLightAll
. The time.sleep
function introduces a delay between each frame of the animation.
def animation_cycle(amount, cycles, delay):
for cycle in range(cycles): # Number of cycles of the animation
for i in range(amount): # Amount of LEDs
buf = bytearray()
# Create a color pattern that will cycle through each LED
for j in range(amount):
if j == i:
# Red color
buf.extend(create_color(255, 0, 0))
elif j == (i + 1) % amount:
# Green color
buf.extend(create_color(0, 255, 0))
elif j == (i + 2) % amount:
# Blue color
buf.extend(create_color(0, 0, 255))
else:
# Turn off the LED if not in use
buf.extend(create_color(0, 0, 0))
manualLightAll(buf)
time.sleep(delay) # Delay between each frame
Last is running the animation, here, the code sets the number of LEDs, the number of animation cycles, and the delay between color changes before initiating the animation using the animation cycle function.
amount_of_leds = 8
animation_cycles = 20
frame_delay = 0.1
animation_cycle(amount_of_leds, animation_cycles, frame_delay)
FarewellAs you dive into the realm of Neopixel control with your MicroPython board, remember to embrace the tools at your disposal. Whether it's leveraging the bitstream function for precise timing or exploring other methods, the key is to have fun and get creative with your light displays. Wishing you endless success in illuminating your projects with Neopixels! Catch you later!
Comments