pboard controlling WS2801 ledstrip over SPI

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
klankschap
Posts: 16
Joined: Tue Feb 02, 2016 11:20 am

pboard controlling WS2801 ledstrip over SPI

Post by klankschap » Tue Feb 02, 2016 11:22 am

Has anyone experience controlling an Adafruit WS2801 led strip over SPI using the micro python on the pyboard?

Thanks
Floris

razanur
Posts: 2
Joined: Sun Nov 03, 2019 4:55 pm

Re: pboard controlling WS2801 ledstrip over SPI

Post by razanur » Sun Nov 03, 2019 5:15 pm

Works pretty much out of the box. I wrote this code for mine:

Code: Select all

N_LED = const(100)
DEFAULT_COLOR = {'r': 0xff, 'g': 0xA0, 'b': 0xA0}

class WS2801:

  def __init__(self, num_led, spi):
    self.NUM_LED = num_led
    self._databuffer = bytearray(3*num_led)
    self._spi = spi
    # Check if baud frequency adequate.

  def set_led(self, num, r=0x00, g=0x00, b=0x00):
    if num < self.NUM_LED:
      self._databuffer[num*3] = r
      self._databuffer[num*3+1] = b
      self._databuffer[num*3+2] = g
    else:
      print("Command ignored. LED out of index!")

  def write(self, clear=False):
    self._spi.write(self._databuffer)
    if clear:
      self.clear()

  def clear(self, default=0x00):
    for i in range(len(self._databuffer)):
      self._databuffer[i] = default

  def set_all(self, r=0xff, g=0xff, b=0xff):
    for i in range(self.NUM_LED):
      self.set_led(num=i, r=r, g=g, b=b)
      
hspi = SPI(1, baudrate=int(250e3), sck=Pin(14), mosi=Pin(13), miso=Pin(12))
print("Initialize WS2801 handler")
ws_handler = WS2801.WS2801(spi=hspi, num_led=N_LED)

print("Test LEDs")
ws_handler.clear()
ws_handler.write()
for i in range(N_LED):
  ws_handler.clear()
  ws_handler.set_led(num=i, **DEFAULT_COLOR)
  ws_handler.write()
  time.sleep_ms(100)
ws_handler.clear()
ws_handler.write()
      
Good luck.

razanur
Posts: 2
Joined: Sun Nov 03, 2019 4:55 pm

Re: pboard controlling WS2801 ledstrip over SPI

Post by razanur » Sun Nov 03, 2019 5:17 pm

Also, you'll probably have to cheat to get to the 5V logic level: https://hackaday.com/2017/01/20/cheatin ... -data-line

Ricardo
Posts: 11
Joined: Mon Dec 09, 2019 9:49 pm

Re: pboard controlling WS2801 ledstrip over SPI

Post by Ricardo » Sat Mar 06, 2021 10:43 pm

Thanks for the code, razanur! I hope you don't mind me using and sharing your code (with credits) on a small project. If anyone is interested, you can see the PC and ESP32 code on Github:

Image

Monitor Ring Light - A custom ring light around a monitor, providing uniform lighting and custom color toning for video recordings and video meetings. Made with RGB LED Pixels (WS2801), ESP32, MicroPython and Dear PyGUI.

Post Reply