element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Using Raspberry Pi to drive WS2812B LEDs
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 19 replies
  • Answers 2 answers
  • Subscribers 676 subscribers
  • Views 5377 views
  • Users 0 members are here
  • gpio
  • led
  • leds
Related

Using Raspberry Pi to drive WS2812B LEDs

Former Member
Former Member over 11 years ago

Hi there, I am pretty new to controlling hardware from the GPIO pins on the Raspberry Pi,

 

First I will give you a little background to what I am trying to achieve:

 

I have a 3D Printer which is controlled via my Raspberry Pi, running a distribution of wheezy containing OctoPrint. I have the Raspberry Pi Camera module connected up and a server accessible over the internet outside of my home network so I can see my prints progressing while away from home.

 

I am trying to use the above quoted LEDs which I purchased here:

 

20 RGB LED WS2812B Individually Addressable NeoPixel DC5V Arduino & Raspberry Pi | eBay

 

To illuminate my print bed. I chose these LED's due to them being RGB and (as the listing stated) them being compatible with the Raspberry Pi. I have thus far been unable to make them illuminate. The only light they emit is a quick purple flash when the ground and +5V supplies are first connected to the Raspberry Pi.

 

I have tried to use the Hyperion package on my Raspberry Pi but again no success. I ideally wanted to be able to be able to control them via the GPIO utilities already built in to the distribution to minimise CPU load (given the nature of 3D printer host software).

 

If anyone can advise me in this matter that would be fantastic.

 

Kindest regards,

 

Nathan

  • Sign in to reply
  • Cancel
  • rew
    0 rew over 11 years ago

    Those leds use a serial protocol to communicate their "color" with the host computer.

     

    The serial protocol uses a bit-time of 1.25 microseconds, or 800kbps. For each led you need to send 24 bits.

     

    If you would write a program that does the GPIO up-and-down things in the right cadence, then the Linux system will mess that up because there are interrupts and maybe other programs that want their slice of CPU-time.


    The only option I can think of is to write a kernel driver.

     

    HO STOP! HACKTIME!

     

    Connect the serial in of the WS2812 to the MOSI pin. Configure the SPI interface for 2.4MHz. Then send an SPI packet: a bunch of zeroes (*) (reset)  and then you encode:  0x1 0y1 0z1 .... the xyz are the bits you try to send, and the 0.1 (*) are the "protocol bits" that the WS2812 wants for synchronization.

     

    Anyway, then the raspberry pi will DMA the data to the SPI port and the signal will be nice and clean without interruptions.

     

     

    (*) I don't remember the polarity of the reset signal and the data bits. The way I've written it here, it can't work, so one or the other needs to be inverted.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    Also needs a PI Health Warning here

     

    The world is full of demolished PIs where the PI has tried and failed to drive WS28xx LEDs, in frustration the PI launched itself from the highest possible location into the abyss.

     

    Seriously though, If you Google this you will find there have been many attempts to drive these LEDs with a PI and all so far have failed with anything but a few LEDs or completely.

     

    A kernel driver may as stated by Roger may be your only hope but Linux being Linux, it still may suffer interference or other timing issues with longer strings

     

    what I have seen successful is using say an ATMEGA328 to drive the LEDs but a PI to perform the complicated Math of other routines to calculate the patterns, this way the critical timing is un affected by the multitasking operating system of the PI. Communications can easily be set up over the SPI or I2C bus

     

    If you are successful in writing and running a kernel mode driver, the world will be knocking at your door for a copy

     

    Good luck

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Robert Peter Oakes

    Thank you both for your responses.

     

    HAH.. me write a kernel? I would not know where to even begin beginning.. image

     

    So with you mentioning the ATMEGA328, could it therefore be a better solution to use the Arduino to actually drive the LEDs but use the Pi to send the commands? Is this what you are suggesting? I am guessing that if I was to go through the Arduino route then I could use the Adafruit NeoPixel libraries?

     

    Thanks again,

     

    Nathan

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    Yes correct

     

    The Arduino and the Adafruit library work very well, I use this combo myself with great success, including IR remote control or over the serial and Ethernet

     

    You can run several hundred LEDs if needed but if you use too many you may have to go for a MEGA with more memory and or speed

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Robert Peter Oakes

    Great,

     

    I already own an Uno R3 and a Mega however I was thinking about expanding my microcontroller collection with either an Arduino Mini or Nano V3.0 Would either of these be sufficient to drive these LEDs or would the Uno be required as a bare minimum?

     

    I am wanting to design a controller box to contain the Raspberry Pi, a twin relay board, an external power supply for the LEDs and obviously the Arduino attached to the Pi, so would ideally like to use the smallest Arduino possible.. I antisipate requiring no more than 20 of these LED's to illuminate the bed.

     

    Nathan

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    Driving the LEDs will work with almost any of the micro controllers as long as it is running at least 8Mhz clock, the issue you will run into is when you start adding other capabilities like Ethernet, IR, RF etc and you start eating up memory, even programming a bunch of patterns will eat away at the memory slowly the more you add.

     

    If you are using the PI to send out the patterns or selecting from a range of pre-programed then that should be ok. You can always add an SD card then you could have a very large number of patterns to select from

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • rew
    0 rew over 11 years ago in reply to rew

    Guys, consider me misguided: The raspberry pi SPI implementation skips a clock every byte. There is a gap between each byte. For a synchronous transfer that doesn't matter, but for a timed protocol as the WS2812 it ruins the whole thing.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to rew

    one of the reasons I am suggesting the use of an ATMega micro instead of the PI for the actual control of the LEDs, PI can be used as a Master sending commands to the ATMega to change light pattern etc

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Robert Peter Oakes

    Hi again,

     

    Thank you to all who have been replying to this thread, I feel like I am about to make a breakthrough.

     

    So, progress update:

     

    I have installed nanpy on my Raspberry Pi and I have it hooked up with my Arduino Uno (For now, I have ordered a Nano) and I have successfully been able to light a regular common anode RGB LED on pin 13 and gnd of the Uno.

     

    Now. My next question is how would I write a python script to control the WS2812B LEDs? I am guessing PWM signal would do it?

     

    Thanks again,

     

    Nathan

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    The WS2812B LEDs do not take PWM or even direct LED power, they have a chip built in that you talk to through a data line, serial in and a serial out to be precise

     

    each chip will take 24Bits of the serial data to represent RGB (8 bits each), once it has its 24bits, any more data is passed onto the next LED in the sequence. All this happens at a precise 800Khz

     

    so the code you write on the PI will simply have to decide what color and intensity you want for each LED and instruct the Uno to do that

     

    if you follow the Adafruit library examples your see what I am referring to, nice thing about these chips is you don't need power drivers for them, just give them 5V and then tell them what color you want. DONE.

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube