Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

MicroPython P9813

A MicroPython library for P9813 RGB LED drivers.

For example, the Seeed Studio Grove - Chainable RGB LED.

demo

Installation

Using mip via mpremote:

$ mpremote mip install github:mcauser/micropython-p9813
$ mpremote mip install github:mcauser/micropython-p9813/examples

Using mip directly on a WiFi capable board:

>>>importmip>>>mip.install("github:mcauser/micropython-p9813")
>>>mip.install("github:mcauser/micropython-p9813/examples")

Manual installation:

Copy src/p9813.py to the root directory of your device.

SPI Version

You can use either HSPI or SPI. This version is significantly faster than the bit-bang version. MISO is not used. The LED driver is write only.

SPI Example

Basic usage

importp9813num_leds=10auto_write=False# Using Hardware SPIfrommachineimportSPI, Pinspi=SPI(1, 10000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19)) # TinyPICOchain=p9813.P9813_SPI(spi, num_leds, auto_write)
# Or using Software SPI# from machine import SoftSPI, Pin# spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(4), mosi=Pin(5), miso=Pin(6)) # TinyS2# chain = p9813.P9813_SPI(spi, num_leds, auto_write)# Set the first LED to red. (red, green, blue)# In a chain, the first LED is closest to the MCUchain[0] = (255, 0, 0)
# Set the second LED to greenchain[1] = (0, 255, 0)
# Set third LED to blue and forth LED magentachain[2:4] = [(0, 0, 255), (255, 0, 255)]
# Write data to all LEDschain.write()
# What colour is the forth LED?print(chain[3])
# (255, 0, 255)# How many LEDs in the chain again?print(len(chain))
# 10# What are the first two colours?print(chain[0:2])
# [(255, 0, 0), (0, 255, 0)]# What are all the colours?print(chain)
# [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 0, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]# Turn on auto-write, so you don't have to call write() after each changechain.auto_write=True# Make all LEDs redchain.fill((255,0,0))
# Make all LEDs greenchain[0:10] = [
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
(0, 255, 0),
]
# Draw a rainbow by modifying the buffer directly# Each 4 bytes = 1 LED (checksum, blue, green, red)chain._buf=bytearray(b"\xc3\x00\x00\xff\xc6\x00K\xb4\xc9\x00\x99f\xcc\x00\xe4\x1b\xcc3\xcc\x00\xe4\x81~\x00\xf0\xcc3\x00\xf0\xe4\x00\x1b\xe1\x99\x00f\xd2K\x00\xb4")
chain.write()
# Turn off all LEDschain.reset()

Bit Bang Version

This version works exactly like as the SPI version, only the serial data is written by sending pulses on two output pins (clock and data).

Bit Bang Example

Basic Usage

importp9813num_leds=10auto_write=FalsefrommachineimportPinpin_clk=Pin(4, Pin.OUT)
pin_data=Pin(5, Pin.OUT)
chain=p9813.P9813_BITBANG(pin_clk, pin_data, num_leds, auto_write)
# Make all LEDs redchain.fill((255, 0, 0))
# Set the second LED to greenchain[1] = (0, 255, 0)
# Set the last 8 LEDs to bluechain[2:] = [(0, 0, 255)] *8# Turn off all LEDschain.reset()

For more detailed examples, see examples.

If you mip installed them above, you can run them like so:

importp9813.examples.basic

Chaining

You can connect multiple LEDs together to form a chain. For each additional LED, an additional 4 bytes need to be sent per refresh. The more LEDs you add, the longer it takes to write.

Parts

Connections

Grove Chainable RGB LEDTinyPICO (ESP32)
CI (clock) (yellow)4
DI (data) (white)14
VCC (red)3V3
GND (black)GND
Grove Chainable RGB LEDTinyS2 (ESP32s2)
CI (clock) (yellow)4
DI (data) (white)5
VCC (red)3V3
GND (black)GND
Grove Chainable RGB LEDWeMos D1 Mini (ESP8266)
CI (clock) (yellow)D2 (GPIO4)
DI (data) (white)D1 (GPIO5)
VCC (red)3V3 (or 5V)
GND (black)GND

If you are using the bit-banged version, you can use any output GPIO pins for clock + data.

If you are using the SPI version, you need to use the SPI pins available to your MCU.

If you are chaining multiple LEDs, clock out -> clock in, data out -> data in, eg.

LED1LED2
COCI (yellow)
DODI (white)
VCCVCC (red)
GNDGND (black)

Links

License

Licensed under the MIT License.

Copyright (c) 2017 Mike Causer

About

MicroPython driver for P9813 chainable RGB LEDs

Topics

Resources

Stars

8 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages