Controlling WS2812 LEDs

I thought this might come in use for people wanting to simply control a strip of WS2811/2812/2812B LEDS. This is not all-singing – you can control individual LEDS but the fancy stuff you’ll find elsewhere.  In my case I wanted to smoothly update a whole array of these things in a home control board using the minimum of room and having to do the least setup or re-programming.

I’ve been using the ADAFRUIT library for some time – that is all singing – but.. it also has bugs. I found when adjusting colour and brilliance of strips of LEDS at the same time there were inconsistencies – also the library was using up a lot of space I could not afford. So I went off looking.

I stumbled across THIS library.

You’ll find if your needs are simple this does a lot of what you want – basically you define an array of LEDS – set the colour for each one – or the lot in a loop – and call the update routine – no setup for different types – it just WORKS and works quickly and takes up very little room.

But it was a TAD too basic. So I added some stuff. I wanted to be able to dynamically adjust the number of LEDS without reprogramming the chip. On inspection the room for each led (3 bytes per LED in RAM)  is MALLOCed… so a simple addition to the library…

Add this to the public definitions in the header


and some code into the main SW2812.cpp file..


And that’s it – so now you can change clear an array of LEDS to off – OR specify an RGB colour for the lot.

All of that was fine – all I needed now was a routine that could be called in the background…..

I have a byte variable which contains the brilliance of the strip – and another which contains a colour from a FLASH-based lookup table…

For historical reasons these bytes are called neocol and neobright.

Here’s the lookup table – you don’t need it of course but I was short of room.



And here is a routine you might call – every few milliseconds – to arrange smooth change of colour and brilliance to your array as you set those two values…


I hope that proves useful to you – compared to similar code using Adafruit I gained over 1k of code space back and it does a smoother job.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s