empowering creative people

Videos / How to use WS2812B RGB LEDs with Raspberry Pi

This tutorial is aimed at getting some instant gratification from your WS2812B LEDs (trade name: neopixels). I'll briefly cover a bare-bones setup for Raspberry Pi.

Search Videos

Related Content



G'day if you've got some addressable LED strip and you don't know what to do with it and a Raspberry Pi this tutorial is for you. We're just going to quickly show you how to get some example code running to power your strip of WS2812B LEDs these are also known as NeoPixels let's get started just before I get too ahead of myself this tutorial assumes that you're comfortable using the Raspberry Pi in its desktop environment or as a desktop computer so if you're not you can check out chapter 1 of our Raspberry Pi for beginners workshop which is a free online course. Ok now let's get started.

So the first thing you'll need to do is wire up your Raspberry Pi as shown in the diagram here, what we have is our Raspberry Pi up to the top with ground and power connections coming onto this bus and that's just feeding the low-voltage side of a logic level shifter. We have our signal coming from the PI and that's going into the logic level shifter we need this because the PI is a 3.3 volt device but our LED strip down the bottom is a 5 volt device so we need to translate between those voltage levels, and then on the other side we just have the signal coming out which is now at 5 volts going into the data for the LED strip and then we're just powering the strip from what's pictured here is a DC barrel Jack. I'm actually running it from a lab power supply today but there's no difference. So the first thing we need to do is install some packages, so what we're going to do is just scroll down and I have this this convenient little one-liner here that we can paste into our terminal so you can go up to the top and open the terminal from this button and then just right click to paste, so what's going on here is our Raspberry Pi's package indexes are being updated and then some Python wrappers are being downloaded so that we can drive LED strips that are also going to come with a few example scripts that we're going to be able to run so we'll just wait for that to complete.

So those packages have finished downloading and if we execute the LS command we'll be able to see the contents of the current directory and what we're interested in is this package that was just downloaded here, so what we can do is CD into that with our partners going to use tab completion and I'll just double tap tab again to remind me what's in that directory so I want to go into Python double tab again and I wanna navigate into examples double tab. So I've changed into a nested subdirectory and in this subdirectory, we have our example Python scripts. So the one that I want to run is this strandtest.py so what I can do is I have to run this with sudo by the way I see you do Streatham strand test up py and hit enter so this is worked for me straight away, if you are getting some erratic behavior here like the LEDs are kind of flickering in an unpredictable way and it's just kind of nonsense looking I'll show you how to rectify that now.

So we can just admire this demo for a little while, it's quite neat if you didn't get this result that I'll show you one way that seems to work for fixing it. So I'm going to press Ctrl C to quit the script and what we need to do is modify a certain file so I need to run sudo nano I need to edit the file /boot/config.txt this is a config file that affects how the raspberry pi starts and operates. So if I hit enter we're now editing the file from within the Nano text editor and the changes that I need to make are documented just down the page a little bit so I need to add these two lines HDMI force hot-plug equals 1 and HDMI force something to do with the audio so I'm going to copy that and over in Nano I'm just going to scroll down to about where it should go it doesn't matter where it goes I'll just find the right group so you can see we have here we go so this line says uncomment if HDMI display not detected yadda yadda, but this is this is one of the lines that we have pasted in but it's just commented out here so what I'll do is press the Delete key to just uncomment that, so it's now an active piece of code and I'll right-click and paste the rest. So now I've got two entries for that hotplug I can't use the mouse to navigate I have to use the key so I'll just go up and delete that line so now that we have those two lines that exist in our boot config text file you can press Ctrl X to save and then you're prompted do you want to save and you just say Y for yes and we absolutely don't want to change the name so I'm just going to press ENTER for this so now that we've edited our config text file to what it needs to be we can just execute sudo thumbnail even have to be sudo it's just us execute reboot.

Now if you execute reboot your Raspberry Pi will boot up again and then you can just have another crack at running that pseudo pythons strand test command and you should get something that looks pretty similar to this. So now that what everyone hopefully everyone has their LED strip working what I might try is to modify that file to see if we can make it do something a little more interesting so what I can execute is Nano and it was called strand test, and here's our Python script so straight away the very first parameter that's quite important is the LED count, this is how you set the length of strip that you have so if you were working with only one LED this would be a pretty uninteresting demo but I'm working with a whole roll of LEDs, so what I can do is backspace that and I'll just put in a hundred for now. We've already wired up to Pin 18 that's why it's working in the first place and the rest of these parameters I can forget about is there anything that's worth changing in here, oh I've got some functions where's the main piece of code, I might just speed things up a bit I think, here we go, this is the main piece of code so this is just executing and it seems to be executing about as fast as it can, so I might just save that now with the control X and a Y for yes to save strand test. And now if I run strand test again I should see that going all the way down the role and indeed you can see that spiraling all the way into the center of the strip until it's only filled 100 LEDs, so I could have made you push that to like 150 160 I'm not quite sure how many LEDs are on this roll that'd be pretty easy to calculate. So there you have it a quick start guide to getting your addressable LEDs working with your Raspberry Pi. I'll catch you next time. [Music]