{
  "id": "mcauser/micropython-p9813",
  "name": "micropython-p9813",
  "prettyName": "micropython-p9813",
  "description": "MicroPython driver for P9813 chainable RGB LEDs",
  "version": "",
  "author": "mcauser",
  "license": "MIT",
  "tags": [
    "grove",
    "grove-chainable-rgb-led",
    "micropython",
    "p9813",
    "rgb-led"
  ],
  "status": "VALID",
  "official": false,
  "source": "https://github.com/mcauser/micropython-p9813",
  "updatedAt": "2025-06-28T07:01:01.615Z",
  "page": "https://checkmim.com/packages/mcauser+micropython-p9813",
  "createdAt": "2025-05-27T04:29:19.218Z",
  "submittedBy": "oliverr",
  "install": {
    "mpremote": "mpremote mip install github:mcauser/micropython-p9813",
    "mip": "import mip; mip.install(\"github:mcauser/micropython-p9813\")"
  },
  "readme": "# MicroPython P9813\n\nA MicroPython library for P9813 RGB LED drivers.\n\nFor example, the Seeed Studio [Grove - Chainable RGB LED](https://wiki.seeedstudio.com/Grove-Chainable_RGB_LED/).\n\n![demo](docs/demo.jpg)\n\n\n### Installation\n\nUsing mip via mpremote:\n\n```bash\n$ mpremote mip install github:mcauser/micropython-p9813\n$ mpremote mip install github:mcauser/micropython-p9813/examples\n```\n\nUsing mip directly on a WiFi capable board:\n\n```python\n>>> import mip\n>>> mip.install(\"github:mcauser/micropython-p9813\")\n>>> mip.install(\"github:mcauser/micropython-p9813/examples\")\n```\n\nManual installation:\n\nCopy `src/p9813.py` to the root directory of your device.\n\n\n## SPI Version\n\nYou can use either HSPI or SPI. This version is significantly faster than the\nbit-bang version. MISO is not used. The LED driver is write only.\n\n### SPI Example\n\n**Basic usage**\n\n```python\nimport p9813\n\nnum_leds = 10\nauto_write = False\n\n# Using Hardware SPI\nfrom machine import SPI, Pin\n\nspi = SPI(1, 10000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))  # TinyPICO\nchain = p9813.P9813_SPI(spi, num_leds, auto_write)\n\n# Or using Software SPI\n# from machine import SoftSPI, Pin\n# spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(4), mosi=Pin(5), miso=Pin(6))  # TinyS2\n# chain = p9813.P9813_SPI(spi, num_leds, auto_write)\n\n# Set the first LED to red. (red, green, blue)\n# In a chain, the first LED is closest to the MCU\nchain[0] = (255, 0, 0)\n\n# Set the second LED to green\nchain[1] = (0, 255, 0)\n\n# Set third LED to blue and forth LED magenta\nchain[2:4] = [(0, 0, 255), (255, 0, 255)]\n\n# Write data to all LEDs\nchain.write()\n\n# What colour is the forth LED?\nprint(chain[3])\n# (255, 0, 255)\n\n# How many LEDs in the chain again?\nprint(len(chain))\n# 10\n\n# What are the first two colours?\nprint(chain[0:2])\n# [(255, 0, 0), (0, 255, 0)]\n\n# What are all the colours?\nprint(chain)\n# [(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)]\n\n# Turn on auto-write, so you don't have to call write() after each change\nchain.auto_write = True\n\n# Make all LEDs red\nchain.fill((255,0,0))\n\n# Make all LEDs green\nchain[0:10] = [\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n    (0, 255, 0),\n]\n\n# Draw a rainbow by modifying the buffer directly\n# Each 4 bytes = 1 LED (checksum, blue, green, red)\nchain._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\")\nchain.write()\n\n# Turn off all LEDs\nchain.reset()\n```\n\n\n## Bit Bang Version\n\nThis version works exactly like as the SPI version, only the serial data\nis written by sending pulses on two output pins (clock and data).\n\n### Bit Bang Example\n\n**Basic Usage**\n\n```python\nimport p9813\n\nnum_leds = 10\nauto_write = False\n\nfrom machine import Pin\n\npin_clk = Pin(4, Pin.OUT)\npin_data = Pin(5, Pin.OUT)\nchain = p9813.P9813_BITBANG(pin_clk, pin_data, num_leds, auto_write)\n\n# Make all LEDs red\nchain.fill((255, 0, 0))\n\n# Set the second LED to green\nchain[1] = (0, 255, 0)\n\n# Set the last 8 LEDs to blue\nchain[2:] = [(0, 0, 255)] * 8\n\n# Turn off all LEDs\nchain.reset()\n```\n\nFor more detailed examples, see [examples](/examples).\n\nIf you mip installed them above, you can run them like so:\n\n```python\nimport p9813.examples.basic\n```\n\n\n## Chaining\n\nYou can connect multiple LEDs together to form a chain.\nFor each additional LED, an additional 4 bytes need to be sent per refresh.\nThe more LEDs you add, the longer it takes to write.\n\n\n## Parts\n\n* [Grove - Chainable RGB LED](https://www.seeedstudio.com/Grove-Chainable-RGB-LED.html)\n* [Grove 4P Universal Buckled Cable](https://www.seeedstudio.com/Grove-Universal-4-Pin-Buckled-20cm-Cable-5-PCs-pack.html)\n* [Grove 4P Male Dupont Conversion Cable](https://www.seeedstudio.com/Grove-4-pin-Male-Jumper-to-Grove-4-pin-Conversion-Cable-5-PCs-per-Pack.html)\n* [TinyPICO](https://www.tinypico.com/)\n* [TinyS2](https://unexpectedmaker.com/tinys2)\n* [WeMos D1 Mini](https://www.aliexpress.com/item/32529101036.html)\n\n\n## Connections\n\nGrove Chainable RGB LED | TinyPICO (ESP32)\n----------------------- | ----------------\nCI (clock) (yellow)     | 4\nDI (data) (white)       | 14\nVCC (red)               | 3V3\nGND (black)             | GND\n\nGrove Chainable RGB LED | TinyS2 (ESP32s2)\n----------------------- | ----------------\nCI (clock) (yellow)     | 4\nDI (data) (white)       | 5\nVCC (red)               | 3V3\nGND (black)             | GND\n\nGrove Chainable RGB LED | WeMos D1 Mini (ESP8266)\n----------------------- | -----------------------\nCI (clock) (yellow)     | D2 (GPIO4)\nDI (data) (white)       | D1 (GPIO5)\nVCC (red)               | 3V3 (or 5V)\nGND (black)             | GND\n\nIf you are using the bit-banged version, you can use any output GPIO pins for clock + data.\n\nIf you are using the SPI version, you need to use the SPI pins available to your MCU.\n\nIf you are chaining multiple LEDs, clock out -> clock in, data out -> data in, eg.\n\nLED1 | LED2\n---- | ----\nCO   | CI (yellow)\nDO   | DI (white)\nVCC  | VCC (red)\nGND  | GND (black)\n\n\n## Links\n\n* [micropython.org](http://micropython.org)\n* [P9813 datasheet](docs/P9813_datasheet.pdf)\n* [TinyPICO Getting Started](https://www.tinypico.com/gettingstarted)\n* [WeMos D1 Mini](https://wiki.wemos.cc/products:d1:d1_mini)\n\n\n## License\n\nLicensed under the [MIT License](http://opensource.org/licenses/MIT).\n\nCopyright (c) 2017 Mike Causer\n"
}