# CircuitPython NeoPixel Library Using SPI

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/082/431/medium800thumb/led_pixels_20191004_152053.jpg?1571080582)

![](https://cdn-learn.adafruit.com/assets/assets/000/082/416/medium800/circuitpython_leds_timing.png?1571075547)

NeoPixels are really great in terms of pin use. They only require three pins, and two of those are power and ground. They only need one pin for data which is used for **all** the NeoPixels attached. Amazing.

However, this comes at the cost of requiring the data signal maintain a very specific timing requirement. See [here](https://learn.adafruit.com/adafruit-neopixel-uberguide/advanced-coding#writing-your-own-library-19-13) for some details from the excellent [NeoPixel Uberguide](https://learn.adafruit.com/adafruit-neopixel-uberguide/).

## The NeoPixel SPI Hack

This is a pretty cool hack. The key enabling feature is the much faster relative speed of the SPI bus compared to the NeoPixel data signals. The NeoPixel data signal runs at 800kHz = 0.8MHz with some older ones running even slower at 400kHz = 0.4MHz. A SPI bus can be clocked in the 10s of MHz - **orders of magnitude faster than NeoPixel**!

This hack takes advantage of that faster speed to "synthesize" the NeoPixel data signal on the SPI's MOSI pin. In its most simple form, the hack turns every **bit** of NeoPixel data into a specific **byte** in the SPI data. There only two bytes that matter - one that represents a NeoPixel 0 bit, and one that represents a NeoPixel 1 bit.

Once the desired NeoPixel data has been translated into SPI data, it is simply clocked out on the SPI bus. The SPI bus frequency is set such that the data comes out at the expected NeoPixel timing. Then, by wiring the SPI MOSI pin to the NeoPixel data in pin, you can drive NeoPixels using the SPI bus. From the NeoPixel's point of view, it just sees the specific data signal it expects and you get happy blinky NeoPixels.

## SPI Support in CircuitPython NeoPixel Library

All this work has been done for you via a new **NeoPixel\_SPI** class that can be found in the [CircuitPython NeoPixel SPI library](https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI). When driving NeoPixels via a SPI port, you simply use this class. After the initial setup, you can then use the NeoPixels as normal.

Let's see some examples.

# CircuitPython NeoPixel Library Using SPI

## FT232H Example

Using the SPI port option of an FT232H, we can drive NeoPixels from any PC with a USB port. See here for details about getting the FT232H installed and setup for your specific operating system:

[CircuitPython FT232H Guide](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h/)
And of course, also install the NeoPixel library:

```cpp
sudo pip3 install adafruit-circuitpython-neopixel-spi
```

## FT232H Wiring

The wiring is pretty simple.

- **FT232H 5V** to **NeoPixel VIN**
- **FT232H GND** to **NeoPixel GND**
- **FT232H D1** to **NeoPixel DIN**

![](https://cdn-learn.adafruit.com/assets/assets/000/082/409/medium800/circuitpython_ft232h_neopixel.jpg?1571073049)

Note how only the **MOSI** ( **D1** ) pin of the SPI port is used. The other SPI pins, including SCLK, are not used at all.

Once you are wired up, try running the program in the **Example Code** section.

```cpp
python3 neo_ring.py
```

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/082/423/medium800thumb/circuitpython_20191014_110432.jpg?1571077414)

# CircuitPython NeoPixel Library Using SPI

## Example Code

Once you've gone through the setup specific to your hardware, you should be able to run this example code on any of them.

This example was written for the [12 pixel RGB NeoPixel ring](https://www.adafruit.com/product/1643) shown in the wiring diagrams. To use with other NeoPixel products, change **NUM\_PIXELS** and **PIXEL\_ORDER** to match your setup. You can also change or add colors to the **COLORS** tuple and change the speed via **DELAY**.

Info: 

Save this as **neo\_ring.py** :

```python
import time
import board
import neopixel_spi as neopixel

NUM_PIXELS = 12
PIXEL_ORDER = neopixel.GRB
COLORS = (0xFF0000, 0x00FF00, 0x0000FF)
DELAY = 0.1

spi = board.SPI()

pixels = neopixel.NeoPixel_SPI(spi,
                               NUM_PIXELS,
                               pixel_order=PIXEL_ORDER,
                               auto_write=False)

while True:
    for color in COLORS:
        for i in range(NUM_PIXELS):
            pixels[i] = color
            pixels.show()
            time.sleep(DELAY)
            pixels.fill(0)
```


## Featured Products

### Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C

[Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C](https://www.adafruit.com/product/2264)
Wouldn't it be cool to drive a [tiny&nbsp;OLED display](https://www.adafruit.com/categories/98), read a [color...](https://www.adafruit.com/products/1334)

In Stock
[Buy Now](https://www.adafruit.com/product/2264)
[Related Guides to the Product](https://learn.adafruit.com/products/2264/guides)
### Raspberry Pi 3 - Model B+ - 1.4GHz Cortex-A53 with 1GB RAM

[Raspberry Pi 3 - Model B+ - 1.4GHz Cortex-A53 with 1GB RAM](https://www.adafruit.com/product/3775)
The Raspberry Pi 3 Model B is the most popular Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 3 better? How about a&nbsp;_faster_ processor, 5 GHz WiFi, and updated Ethernet chip with PoE capability?...

In Stock
[Buy Now](https://www.adafruit.com/product/3775)
[Related Guides to the Product](https://learn.adafruit.com/products/3775/guides)
### NeoPixel Ring - 12 x 5050 RGB LED with Integrated Drivers

[NeoPixel Ring - 12 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1643)
Round and round and round they go! 12 ultra bright smart LED NeoPixels are arranged in a circle with 1.5" (37mm) outer diameter. The rings are 'chainable' - connect the output pin of one to the input pin of another. Use only one microcontroller pin to control as many as you can...

In Stock
[Buy Now](https://www.adafruit.com/product/1643)
[Related Guides to the Product](https://learn.adafruit.com/products/1643/guides)

## Related Guides

- [NeoPixel LED Necklace Insert with USB Charging](https://learn.adafruit.com/neopixel-led-necklace-insert-with-usb-charging.md)
- [Programming SPI flash with an FT232H breakout](https://learn.adafruit.com/programming-spi-flash-prom-with-an-ft232h-breakout.md)
- [Adafruit FT232H Breakout](https://learn.adafruit.com/adafruit-ft232h-breakout.md)
- [CircuitPython Libraries on any Computer with FT232H](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h.md)
- [Desktop or Laptop TFT Sidekick With FT232H](https://learn.adafruit.com/tft-sidekick-with-ft232h.md)
- [LED Harness Bra](https://learn.adafruit.com/neopixel-led-harness-bra.md)
- [Sipping Power With NeoPixels](https://learn.adafruit.com/sipping-power-with-neopixels.md)
- [Ray Gun Blaster](https://learn.adafruit.com/ray-gun-blaster.md)
- [Burning Fire Wizard Staff](https://learn.adafruit.com/burning-fire-wizard-staff.md)
- [The PICsellator](https://learn.adafruit.com/the-picsellator.md)
- [Adafruit FT232H With SPI & I2C Devices](https://learn.adafruit.com/adafruit-ft232h-with-spi-and-i2c-libraries.md)
- [Multi-tasking the Arduino - Part 3](https://learn.adafruit.com/multi-tasking-the-arduino-part-3.md)
- [Gemma Color Touch Pendant Necklace](https://learn.adafruit.com/gemma-color-touch-pendant-necklace.md)
- [ANO Directional Navigation and Scroll Wheel Rotary Encoder and Breakout](https://learn.adafruit.com/ano-rotary-encoder.md)
- [Programming Microcontrollers using OpenOCD on a Raspberry Pi](https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi.md)
