Transcription

NeoPixels on Raspberry PiCreated by Tony berry-piLast updated on 2021-11-15 06:18:09 PM EST Adafruit IndustriesPage 1 of 10

Table of ContentsOverview3Raspberry Pi Wiring3 4556Raspberry Pi Wiring with Level Shifting ChipRaspberry Pi Wiring with DiodePowering NeoPixels from Raspberry Pi Without Level ShiftingUsing External Power Source Without Level ShiftingPython Usage7 Python Installation of NeoPixel Library Python Usage Full Example Code778Python Docs Adafruit Industries10Page 2 of 10

OverviewWouldn't it be fun to add bright, beautiful NeoPixels to your Raspberry Pi project?NeoPixels, and the WS2811/2812 LEDs that make them up, require a data signal withvery specific timing to work correctly. Because the Raspberry Pi runs a multi-taskingLinux operating system it doesn't have real-time control over its GPIO pins and can'teasily drive NeoPixels. Typically a small microcontroller like a Trinket or Teensy canbe used to communicate with the Raspberry Pi and generate the NeoPixel data signal.But you're in luck! Thanks to the Adafruit CircuitPython NeoPixel (https://adafru.it/yew)library, you can now control NeoPixels or WS2811/WS2812 LEDs directly from yourRaspberry Pi!The Adafruit CircuitPython NeoPixel library solves the real-time control problem byusing the PWM and DMA hardware on the Raspberry Pi's processor. The PWM (pulsewidth modulation) module can generate a signal with a specific duty cycle (https://adafru.it/dYj), for example to drive a servo or dim an LED. The DMA (direct memoryaccess) module can transfer bytes of memory between parts of the processor withoutusing the CPU. By using DMA to send a specific sequence of bytes to thePWM module, the NeoPixel data signal (https://adafru.it/jFu) can be generatedwithout 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 PWMand DMA to drive NeoPixels. You can use these LEDs with a single-board computer(like Raspberry Pi!) that has GPIO and Python thanks to Adafruit Blinka, ourCircuitPython-for-Python compatibility library (https://adafru.it/BSN).Before you get started you will want to be familiar with how to connect to a RaspberryPi's terminal using SSH (https://adafru.it/jsE). It will also be helpful to check out the NeoPixel Uberguide (https://adafru.it/ja1) for more information on using NeoPixels.Raspberry Pi WiringWiring NeoPixels to work with a Raspberry Pi is quite simple. The only issue to dealwith 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 simple1N4001 power diode or with a level converter chip like the 74AHCT125. Adafruit IndustriesPage 3 of 10

Note that you might be able to get your NeoPixels to work without any levelconversion, but it's not really guaranteed because the data line needs to be at least0.7 * VDD (5 volts), or about 3.5 volts. Try one of the level conversion options below ifyou 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 theNeoPixels can read the Pi's 3.3V output. However you need to be careful to use adiode that can handle all the current drawn by the NeoPixels. The diodes Adafruitsells only handle 1 Amp of continuous current so they're good for driving up to about16 NeoPixels at full 100% bright white - and about 50 NeoPixels if they're all lit withvarious colors. Also because the NeoPixels aren't running at 5 volts they might be alittle dimmer than normal.A level converter chip like the 74AHCT125 is a better method because it will convertthe 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 thepower supply.NeoPixels must be connected to GPIO10, GPIO12, GPIO18 or GPIO21 to work! GPIO18is the standard pin.Be aware, you can only create one strip at a time! If you have more than one, connectthem 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 yourRaspberry Pi.You can use ANY Raspberry Pi computer (Zero, A , Pi 4, etc!) but this guide is notfor 'Raspberry Pi shaped' boards that are not Raspberry Pi (e.g. Banana Pi, etc)Raspberry Pi Wiring with Level Shifting ChipIf you're using the 74AHCT125 level converter chip, wire up your Raspberry Pi asfollows: Adafruit IndustriesPage 4 of 10

Pi GPIO18 to 74AHCT125 pin 1A 74AHCT125 pin 1Y to NeoPixel DIN Power supply ground to74AHCT125 ground Power supply ground to74AHCT125 pin 1OE Power supply ground to Pi GND Power supply ground to NeoPixelGND Power supply 5V to 74AHCT125VCC Power supply 5V to NeoPixel 5V.Raspberry Pi Wiring with DiodeIf you're using a 1N4001 diode wire up your hardware as follows: Pi pin 18 to NeoPixel DIN. 1N4001 diode cathode (side withthe stripe) to NeoPixel 5V. Power supply ground to Pi ground. Power supply ground to NeoPixelGND. Power supply 5V to 1N4001 diodeanode (side without the stripe).Make sure to get the orientation of thediode correct, with the cathode (side withthe stripe) going to the NeoPixel!Powering NeoPixels from Raspberry PiWithout Level ShiftingRemember, your NeoPixels may not work connected directly to the Raspberry Piwithout a level shifter. If you run into issues, try adding a level shifter to your project. Adafruit IndustriesPage 5 of 10

Do not power more than a few NeoPixels from the Raspberry Pi's 5V output! ThePi cannot source enough current to light many pixels and will be damaged. Usea good quality external 5V power supply that can handle the current demands ofall the pixels.If you're only powering a few pixels, you can power them from the Raspberry Pi 5Vpin. Pi 5V to NeoPixel 5V Pi GND to NeoPixel GND Pi GPIO18 to NeoPixel DinUsing External Power Source Without LevelShiftingIf you're going to be using more than a few pixels, it's a good idea to connect anexternal power source. Remember each pixel can draw up to 60mA so don't skimp onthe power supply!Remember, your NeoPixels may not work connected directly to the Raspberry Piwithout 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 NeoPixelGND Power supply 5V to NeoPixel 5V Adafruit IndustriesPage 6 of 10

Python UsagePython Installation of NeoPixel LibraryYou'll need to install the Adafruit Blinka library that provides the CircuitPythonsupport in Python. This may also require verifying you are running Python 3. Sinceeach platform is a little different, and Linux changes often, please visit theCircuitPython on Linux guide to get your computer ready (https://adafru.it/BSN)!Once that's done, from your command line run the following command: sudo pip3 install rpi ws281x adafruit-circuitpython-neopixel sudo python3 -m pip install --force-reinstall adafruit-blinkaIf your default Python is version 3 you may need to run 'pip' instead. Just make sureyou aren't trying to use CircuitPython on Python 2.x, it isn't supported!Python UsageTo demonstrate the usage of this library with NeoPixel LEDs, we'll use the PythonREPL.For NeoPixels to work on Raspberry Pi, you must run the code as root! Rootaccess is required to access the RPi peripherals.Run the following code to import the necessary modules and initialise a NeoPixel stripwith 30 LEDs. Don't forget to change the pin if your NeoPixels are connected to adifferent pin, and change the number of pixels if you have a different number.import boardimport neopixelpixels neopixel.NeoPixel(board.D18, 30)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 . Adafruit IndustriesPage 7 of 10

For example, to light up the first NeoPixel red:pixels[0] (255, 0, 0)To light up all the NeoPixels green:pixels.fill((0, 255, 0))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, thenblue, and then goes through a single rainbow cycle. If you chose a pin other thanD18 for your NeoPixels, change the pin to match the pin you chose to use to connectthe NeoPixels to your Raspberry Pi. If you're using a different number of NeoPixels,change num pixels to match.Full Example Code# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries# SPDX-License-Identifier: MIT# Simple test for NeoPixels on Raspberry Piimport timeimport boardimport neopixel# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18 Adafruit IndustriesPage 8 of 10

# NeoPixels must be connected to D10, D12, D18 or D21 to work.pixel pin board.D18# The number of NeoPixelsnum pixels 30# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and greenreversed!# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.ORDER neopixel.GRBpixels neopixel.NeoPixel(pixel pin, num pixels, brightness 0.2, auto write False, pixel order ORDER)def wheel(pos):# Input a value 0 to 255 to get a color value.# The colours are a transition r - g - b - back to r.if pos 0 or pos 255:r g b 0elif pos 85:r int(pos * 3)g int(255 - pos * 3)b 0elif pos 170:pos - 85r int(255 - pos * 3)g 0b int(pos * 3)else:pos - 170r 0g int(pos * 3)b int(255 - pos * 3)return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)def rainbow cycle(wait):for j in range(255):for i in range(num pixels):pixel index (i * 256 // num pixels) jpixels[i] wheel(pixel index & 255)pixels.show()time.sleep(wait)while True:# Comment this line out if you have RGBW/GRBW NeoPixelspixels.fill((255, 0, 0))# Uncomment this line if you have RGBW/GRBW NeoPixels# pixels.fill((255, 0, 0, 0))pixels.show()time.sleep(1)# Comment this line out if you have RGBW/GRBW NeoPixelspixels.fill((0, 255, 0))# Uncomment this line if you have RGBW/GRBW NeoPixels# pixels.fill((0, 255, 0, 0))pixels.show()time.sleep(1)# Comment this line out if you have RGBW/GRBW NeoPixelspixels.fill((0, 0, 255))# Uncomment this line if you have RGBW/GRBW NeoPixels# pixels.fill((0, 0, 255, 0))pixels.show()time.sleep(1) Adafruit IndustriesPage 9 of 10

rainbow cycle(0.001)# rainbow cycle with 1ms delay per stepPython DocsPython Docs (https://adafru.it/C5m) Adafruit IndustriesPage 10 of 10

Nov 15, 2021 · for 'Raspberry Pi shaped' boards that are not Raspberry Pi (e.g. Banana Pi, etc) Adafruit Industries Page 4 of 10. Pi GPIO18 to 74AHCT125 pin 1A . def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if pos 0 or pos 255: r g b 0File Size: 300KB