# NeoPixels on Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/128/695/medium800/led_strips_DSC00518_scaled.jpg?1710430876)

Wouldn't it be fun&nbsp;to add bright, beautiful NeoPixels to your Raspberry Pi project? NeoPixels, and the WS2811/2812 LEDs that make them up, require a data signal with very&nbsp;specific timing to work correctly. Because the&nbsp;Raspberry Pi runs&nbsp;a multi-tasking Linux operating system it&nbsp;doesn't have real-time control over its GPIO pins and can't easily&nbsp;drive NeoPixels. &nbsp;Typically a small microcontroller like a Trinket or Teensy can be&nbsp;used to communicate with the Raspberry Pi and generate the NeoPixel data&nbsp;signal. But you're in luck! Thanks to the [Adafruit CircuitPython NeoPixel](https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel) library, you can now control NeoPixels or WS2811/WS2812 LEDs directly from your Raspberry Pi!

The Adafruit CircuitPython NeoPixel library solves the real-time control problem by using the PWM and DMA&nbsp;hardware on the Raspberry Pi's processor. &nbsp;The&nbsp;PWM (pulse-width modulation) module can generate&nbsp;a signal with a specific [duty cycle](http://en.wikipedia.org/wiki/Duty_cycle), for example to drive a servo or dim an LED. &nbsp;The DMA (direct memory access) module can transfer bytes of memory between parts of the processor without using the CPU. &nbsp;By using DMA to send a specific sequence of bytes&nbsp;to the PWM&nbsp;module, the [NeoPixel data&nbsp;signal](../../../../adafruit-neopixel-uberguide/advanced-coding)&nbsp;can be generated without&nbsp;being interrupted by the Raspberry Pi's operating system.

The great thing about this library is that it does all the hard work of setting up PWM and DMA to drive NeoPixels.&nbsp; You can use these LEDs with a single-board computer (like Raspberry Pi!) that has GPIO and Python [thanks to Adafruit\_Blinka, our CircuitPython-for-Python compatibility library](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).

Before you get started you will want to be familiar with&nbsp;how to [connect to a Raspberry Pi's terminal using SSH](../../../../adafruits-raspberry-pi-lesson-6-using-ssh/overview). &nbsp;It will also be helpful to check out the [NeoPixel Uberguide](../../../../adafruit-neopixel-uberguide/overview)&nbsp;for more information on using NeoPixels.

# NeoPixels on Raspberry Pi

## Raspberry Pi Wiring

Wiring NeoPixels to work with a Raspberry Pi is quite simple. The only issue to deal with is converting the Pi's GPIO from 3.3V up to about 5V for the NeoPixel to read. There are two ways you can do this level conversion, either with a simple 1N4001 power diode or with a level converter chip like the 74AHCT125.

Note that you _might_ be able to get your NeoPixels to work without any level conversion, but it's not really guaranteed because the data line needs to be at least 0.7 \* VDD (5 volts), or about 3.5 volts. Try one of the level conversion options below if you can't directly drive the pixels from your Raspberry Pi.

The diode method is a quick way to reduce the power supply voltage slightly so the NeoPixels can read the Pi's 3.3V output. However you need to be careful to use a diode that can handle all the current drawn by the NeoPixels. The diodes Adafruit sells only handle 1 Amp of continuous current so they're good for driving up to about 16 NeoPixels at full 100% bright white - and about 50 NeoPixels if they're all lit with various colors. Also because the NeoPixels aren't running at 5 volts they might be a little dimmer than normal.

A level converter chip like the 74AHCT125 is a better method because it will convert the Pi's 3.3V output up to 5V without limiting the power drawn by the NeoPixels. You'll get full NeoPixel brightness that's only limited by the current capability of the power supply.

Warning: 

Info: 

 **NeoPixels must be connected to GPIO10, GPIO12, GPIO18 or GPIO21** to work! GPIO18 is the standard pin.

**Be aware, you can only create one strip at a time!** If you have more than one, connect them together and then wire them to your Raspberry Pi using a single connection.

You can use the following wiring diagrams to connect your NeoPixels to your Raspberry Pi.

Info: 

# Raspberry Pi Wiring with Level Shifting Chip

If you're using the 74AHCT125 level converter chip, wire up your Raspberry Pi as follows:

- **Pi GPIO18** to **74AHCT125 pin 1A**
- **74AHCT125 pin 1Y** to **NeoPixel DIN**
- **Power supply ground** to **74AHCT125 ground** 
- **Power supply ground** to **74AHCT125 pin**  **1OE**  
- **Power supply ground** to **Pi GND**
- **Power supply ground** to **NeoPixel GND**
- **Power supply 5V** to **74AHCT125 VCC**
- **Power supply 5V** to **NeoPixel 5V**.

![led_strips_raspi_NeoPixel_Level_Shifted_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/064/121/medium640/led_strips_raspi_NeoPixel_Level_Shifted_bb.jpg?1540314807)

# Raspberry Pi Wiring with Diode

If you're using a 1N4001 diode wire up your hardware as follows:

- **Pi pin 18** to **NeoPixel DIN**.
- **1N4001 diode cathode** (side **_with_** the stripe) to **NeoPixel 5V**.
- **Power supply ground** to **Pi ground**.
- **Power supply ground** to **NeoPixel GND**.
- **Power supply 5V** to **1N4001 diode anode** (side **_without_** the stripe).

Make sure to get the orientation of the diode correct, with the cathode (side with the stripe) going to the NeoPixel!

![led_strips_raspi_NeoPixel_Diode_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/064/122/medium640/led_strips_raspi_NeoPixel_Diode_bb.jpg?1540315941)

# Powering NeoPixels from Raspberry Pi Without Level Shifting

Remember, your NeoPixels may not work connected directly to the Raspberry Pi without a level shifter. If you run into issues, try adding a level shifter to your project.

Danger: 

If you're only powering a few pixels, you can power them from the Raspberry Pi 5V pin.

- **Pi 5V** to **NeoPixel 5V**
- **Pi GND** to **NeoPixel GND**
- **Pi GPIO18** to **NeoPixel Din**

![led_strips_raspi_NeoPixel_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/063/929/medium640/led_strips_raspi_NeoPixel_bb.jpg?1539981142)

# Using External Power Source Without Level Shifting

If you're going to be using more than a few pixels, it's a good idea to connect an external power source. Remember each pixel can draw up to 60mA so don't skimp on the power supply!

Remember, your NeoPixels may not work connected directly to the Raspberry Pi without a level shifter. If you run into issues, try adding a level shifter to your project.

- **Pi GND** to **NeoPixel GND**
- **Pi GPIO18** to **NeoPixel Din**
- **Power supply ground** to **NeoPixel GND**
- **Power supply 5V** to **NeoPixel 5V**

![led_strips_raspi_NeoPixel_powered_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/063/928/medium640/led_strips_raspi_NeoPixel_powered_bb.jpg?1539980907)

# NeoPixels on Raspberry Pi

## Python Usage

Warning: 

# Setup Virtual Environment

If you are installing on the Bookworm (released in 2023) or later version of Raspberry Pi OS, you will need to install your python modules in a virtual environment. You can find more information in the&nbsp;[Python Virtual Environment Usage on Raspberry Pi](https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi)&nbsp;guide. To Install and activate the virtual environment, use the following commands:

```terminal
cd ~
sudo apt install python3-venv
python3 -m venv env --system-site-packages
```

You will need to activate the virtual environment every time the Pi is rebooted. To activate it:

```terminal
source env/bin/activate
```

To deactivate, you can use&nbsp;`deactivate`, but leave it active for now.

# Python Installation of NeoPixel Library

You'll need to install the Adafruit\_Blinka library that provides the CircuitPython support in Python. This may also require verifying you are running Python 3. [Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux)!

For more recent versions of Python, you will need to set up a Virtual Environment as described in the above linked guide.

Once that's done, from your command line run the following command:

```terminal
pip3 install rpi_ws281x adafruit-circuitpython-neopixel
python3 -m pip install --force-reinstall adafruit-blinka
```

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

Warning: If you have the Pi 5, Neopixels support takes a bit more work to get going due to a change in the GPIO controls.

If you have a Raspberry Pi 5 and need NeoPixels working, you have a couple of options.

If you don't want to bother with software updates and don't need it for other peripheral, you can use the SPI port. Check out the&nbsp;[CircuitPython NeoPixel Library Using SPI](https://learn.adafruit.com/circuitpython-neopixels-using-spi) guide for more information.

If you need the SPI port for other hardware and don't mind potentially doing software updates, check out the [Using NeoPixels on the Pi 5](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/using-neopixels-on-the-pi-5) guide for more information.

# Python Usage

To demonstrate the usage of this library with NeoPixel LEDs, we'll use the Python REPL.

Danger: 

Running code for the NeoPixel library requires root access. However, running a virtual environment prevents this. To get around it, you can pass the user environment variables into the virtual environment. To enter the REPL as root inside a virtual environment, use the following:

```terminal
sudo -E env PATH=$PATH python3
```

If you wanted to run a script file such **neopixel\_rpi\_simpletest.py** , you would use the following command:

```terminal
sudo -E env PATH=$PATH python3 neopixel_rpi_simpletest.py
```

Run the following code to import the necessary modules and initialise a NeoPixel strip with 30 LEDs. Don't forget to change the pin if your NeoPixels are connected to a different pin, and change the number of pixels if you have a different number.

```python
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30)
```

Depending on the specific NeoPixels you have connected, you may need to add some additional parameters to the initializer. Here are a couple common ones:

- **bpp** - Bytes Per Pixel. Defaults to 3. Set this to 4 if you have RGBW NeoPixels. If you try to fill the NeoPixels with a solid color and they light up as different colors, you will probably want to change this.
- **pixel\_order** - If you are seeing the NeoPixels light up as the same colors, but they are a different color than you expect, you may need to change this value.

Now you're ready to light up your NeoPixel LEDs using the following properties:

- **brightness** - The overall brightness of the LED
- **fill** - Color all pixels a given color.
- **show** - Update the LED colors if `auto_write` is set to `False`.

For example, to light up the first NeoPixel red:

```python
pixels[0] = (255, 0, 0)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/063/932/medium800/led_strips_NeoPixel_Single_LED_Red.jpg?1539981675)

To light up all the NeoPixels green:

```python
pixels.fill((0, 255, 0))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/063/933/medium800/led_strips_NeoPixel_All_LEDs_Green.jpg?1539981763)

That's all there is to getting started with NeoPixel LEDs on Raspberry Pi!

Below is an example program that repeatedly turns all the LEDs red, then green, then blue, and then goes through a single rainbow cycle. If you chose a pin other than `D18` for your NeoPixels, change the pin to match the pin you chose to use to connect the NeoPixels to your Raspberry Pi. If you're using a different number of NeoPixels, change `num_pixels` to match.

# Full Example Code
https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/blob/main/examples/neopixel_rpi_simpletest.py

# NeoPixels on Raspberry Pi

## Python Docs


## Featured Products

### 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)
### Raspberry Pi Model B+ 512MB RAM

[Raspberry Pi Model B+ 512MB RAM](https://www.adafruit.com/product/1914)
OMG OMG OMG, did you hear? There's a Raspberry Pi&nbsp;called the Model B+ and check it out...more USB ports, more GPIO, better power supply, four mounting holes, less sticky-out SD card! Yep, that's right, the fantastic engineers at Raspberry Pi HQ have blessed us with a new design....

In Stock
[Buy Now](https://www.adafruit.com/product/1914)
[Related Guides to the Product](https://learn.adafruit.com/products/1914/guides)
### Raspberry Pi Model B 512MB RAM

[Raspberry Pi Model B 512MB RAM](https://www.adafruit.com/product/998)
Adafruit ships the **Raspberry Pi Model B 512MB RAM** as of 10/18/2012.  
  
The Raspberry Pi® is a single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools. The Raspberry...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/998)
[Related Guides to the Product](https://learn.adafruit.com/products/998/guides)
### 74AHCT125 - Quad Level-Shifter (3V to 5V)

[74AHCT125 - Quad Level-Shifter (3V to 5V)](https://www.adafruit.com/product/1787)
Level shifting chips let you connect 3V and 5V devices together safely. This chip is similar to others in the shop (such as the 74LVC125) except this one is particularly good at converting **3V logic up to 5V**. This is in demand, especially when connecting some 3V devices such as...

In Stock
[Buy Now](https://www.adafruit.com/product/1787)
[Related Guides to the Product](https://learn.adafruit.com/products/1787/guides)
### Female DC Power adapter - 2.1mm jack to screw terminal block

[Female DC Power adapter - 2.1mm jack to screw terminal block](https://www.adafruit.com/product/368)
If you need to connect a DC power wall wart to a board that doesn't have a DC jack - this adapter will come in very handy! There is a 2.1mm DC jack on one end, and a screw terminal block on the other. The terminals are labeled with positive/negative assuming a positive-tip configuration...

In Stock
[Buy Now](https://www.adafruit.com/product/368)
[Related Guides to the Product](https://learn.adafruit.com/products/368/guides)
### 5V 2A (2000mA) switching power supply - UL Listed

[5V 2A (2000mA) switching power supply - UL Listed](https://www.adafruit.com/product/276)
This is an FCC/CE certified and UL listed power supply. Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to 2000mA. 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one...

In Stock
[Buy Now](https://www.adafruit.com/product/276)
[Related Guides to the Product](https://learn.adafruit.com/products/276/guides)
### 5V 10A switching power supply

[5V 10A switching power supply](https://www.adafruit.com/product/658)
This is a beefy switching supply, for when you need a lot of power! It can supply 5V DC up to 10 Amps, running from 110V or 220V power (the plug it comes with is for US/Canada/Japan but you can use any plug adapter for your country, or just replace the cable with a standard computer/appliance...

In Stock
[Buy Now](https://www.adafruit.com/product/658)
[Related Guides to the Product](https://learn.adafruit.com/products/658/guides)
### Adafruit NeoPixel Digital RGB LED Strip - White 60 LED

[Adafruit NeoPixel Digital RGB LED Strip - White 60 LED](https://www.adafruit.com/product/1138)
You thought it couldn't get better than [our world-famous 32-LED-per-meter Digital LED strip](http://adafruit.com/products/306) but we will prove you wrong! You wanted **twice the LEDs**? We got it (well, its 1.875 times as many but that's within a margin of...

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

## Related Guides

- [STEAM-Punk Goggles](https://learn.adafruit.com/steam-punk-goggles.md)
- [NeoPixel Tiara](https://learn.adafruit.com/neopixel-tiara.md)
- [Adafruit NeoPixel Überguide](https://learn.adafruit.com/adafruit-neopixel-uberguide.md)
- [LED Trampoline](https://learn.adafruit.com/led-trampoline.md)
- [Easy NeoPixel Graphics with the CircuitPython Pixel Framebuf Library](https://learn.adafruit.com/easy-neopixel-graphics-with-the-circuitpython-pixel-framebuf-library.md)
- [NeoPixie Dust Bag](https://learn.adafruit.com/neopixel-pixie-dust-bag.md)
- [Sewable NeoPixels](https://learn.adafruit.com/flora-rgb-smart-pixels.md)
- [Trinket Sound-Reactive LED Color Organ](https://learn.adafruit.com/trinket-sound-reactive-led-color-organ.md)
- [Guitar Synth with CircuitPython SynthIO](https://learn.adafruit.com/guitar-synth-with-circuitpython-synthio.md)
- [Experimenting with NeverWet + Electronics](https://learn.adafruit.com/neverwet-electronics.md)
- [Knobby Sequencer](https://learn.adafruit.com/knobby-sequencer.md)
- [Make a Guggenhat: Bluetooth-connected wearable NeoPixel marquee hat](https://learn.adafruit.com/guggenhat-bluetooth-le-wearable-neopixel-marquee-scrolling-led-hat-display.md)
- [Overwatch Prop Gun: Lucio's Blaster Pt. 3](https://learn.adafruit.com/overwatch-prop-gun-lucios-blaster-pt-3.md)
- [Animated Edge Lit Acrylic NeoPixel Lamp](https://learn.adafruit.com/acrylic-neopixel-lamp.md)
- [Raspberry Pi as an Ad Blocking Access Point](https://learn.adafruit.com/raspberry-pi-as-an-ad-blocking-access-point.md)
