Life’s better in colour right? Then why are you still using boring old regular LEDs in your project, when you could be using addressable Neopixel RGB LEDs!
How to use Neopixels with Particle Boards
Hello guys how are you going? I’m Sam from Core Electronics and today we’re going to be taking a look at how we can use NeoPixels with our Particle device. Now what is a NeoPixel? It’s a good question and it comes up a lot and it’s not well understood exactly what the word NeoPixel is referring to. Well NeoPixel is Adafruits brand word for their RGB addressable LEDs based on the WS2812 chip. So with traditional LEDs there’s a single LED that you might be used to and it has 2 legs and you apply power across it, you give it a voltage and the correct amount of current and it will light up which is fantastic but for ages people have been using RGB LEDs,so red, blue and green, for their projects and RGB LEDs require 3 Pins to control the 3 colors which is fine. But if you’re using lots of them in a project you’re going to find that you’re using a ridiculous amount of Pins and it can get quite messy to control them because you’ve got all these different objects and definitions that you have to create for them. So we have these wonderful wonderful awesome devices called Digital Addressable LEDs. Now these guys are just a standard surface mount LED package in a 50/50 package and there’s actually a tiny little chip, you probably can’t see on there but down on the tutorial article there’s a, yeah, you can see a nice shot we got with the USB Microscope and there’s a small chip on there with some really thin stranded wires going to each die of the LED and it can light them up independently which is great. If you look at where those strands are you can actually understand why it’s important to not give too much current to your LEDs because your electronics, the circuit pathways inside these devices are really small and fragile and they can only handle what they’re rated for. So anyway that’s an RGB LED and what it allows you to do is send a simple serial command, digital information and the chip will take care of assigning the colors and what this means is you can have a large string of LEDs, you know meters or a couple in a circle like we’ve got here and you can control all of them from a single pin on your microcontroller which is super super cool. It isn’t without some downfalls though, the NeoPixels, each pixel consumes some RAM so because in your program you’re probably going to be using libraries for this and each pixel requires RAM to be constantly updating the colors on this so that’s a constraint. It also requires time to refresh all the Pixels, pretty much you’ll send information to the first Pixel in the chain and it will pass it on to the next one and on to the next one and so on and so forth. There’s a certain protocol that requires very strict timing so you won’t be able to reliably control NeoPixels very well, certainly not a huge amount of them from Microprocessor based programs so things like your Raspberry Pi, your Windows computer. You may be able to use them but they’re going to be no where near as reliable as a microcontroller and this is because the timing with them is really weird, it’s very specific, it’s doesn’t follow the bounds of say I2C, SPII or UART or something like that. But instead you need something that is capable of getting that exact timing something that doesn’t have to service an entire operating system like your Raspberry Pi.
Lets take a look at using them with our Particle Device, I’ve got a Photon here and you can use it with the Electron as well and I’ve got one of these shields and it’s for the Particle Photon and it is a NeoPixel Ring Kit. Now don’t confuse NeoPixel with the Digital Addressable LEDs, that’s just Adafruits brand word as we’ve said. If you search for WS2812 LED you’ll find the standard package. But Adafruit has created this NeoPixel, they’ve created libraries with heaps of platforms and created a brand around which is really cool.
So, I’ve got this Ring Kit which contains 24 LEDs, it has the power management built in so most of these LEDs are 5V devices so I’ve got a stand alone ring here and this is a 5V device so you’ll see that I’m using a Logic Convertor on the board if I want to use that with my Photon because the Photon is a 3.3V device. Check out our Logic Level Conversion tutorial for more info on that. This ones great because it allows us to just plug in directly, it has a clip for LiPo if you want to power your project that way. So let’s go ahead and plug this guy in, now if you’re using one of these rings you will need to solder the wires directly to it but we’re going to go through it using both ways. Plug that guy in nice and securely and what we’re going to do is create a really simple code to control this and it’s not very complicated at all. So go to particle.io and go to the IDE, to your build section. If you don’t have an account you’ll need to log in or sign up and create one. If you don’t have a Particle account, or at this point if you’re not really sure what Particle is check out our other Particle tutorials for really good understanding on that.
So, we are going to log in, (05:54) So, we’ll go down and I’ve created some bare bones code to drive your Neopixels. Now Adafruit has created libraries, ported them over to the Particle Platform which is really awesome, go them - you guys are fantastic! We’ve got this skeleton code here, which contains all the set up and the library inclusions so we can copy that over to here. Lets create a new app, I’m going to call this, what ever you want really! Neopixeltest1 right, sounds logical :-) Right, copy that code in and though even though we’ve added the include application include Neopixel Libraries we still actually need to go through and add them with the library tab into our code. It doesn’t actually know that to go and search for it, It needs to include them in our project and then that include function says, hey, I want to use this library in the app. So, Neopixel there, straight up, very popular library, include in app, and you’ll know if its properly included in your app if you’ve got the include line in your code (moment of chaos here…..)
07:43 Now, I’m in include in app and I want to select the app that I want to include it in (now where has the Neopixeltest1 gone?) Hmm ok, lets go ahead and create the app again, I think I have completely clustered this up, never fear, let’s go again! Create new app, Neopixel test, this is a good example, always concentrate on what you’re doing. Right, we have our app, fantastic!
Now, Libraries, Neopixels, Include in app, and we’re going to select the app we want to include in so Neopixeltest1 if thats what you have named your particular program (yep, Neopixeltest1 add to this app) Now we can see in our Neopixeltest1 that we have the included libraries Neopixel in there which is cool. Now go ahead and select all that code and just copy it back in, fantastic.
Now there’s three important bits of set up here, you can see some definitions. You’ve got pixel count, pixel pin and pixel type. Now there’s two different types of Neopixels or the chip controlling the Neopixels, you’ve got the standard WS2812 chip and you’ve got the WS2812B. Now they operate very similarly but the timings on them are slightly different which is why the libraries count both types so you do need to specify that. Now if you go on to the product page you can find the type that it is. Or on the back here I can see, it does say WS2812. Fantastic, so I want to change that to 2812 and now we’ve got to select the pin that you are driving your string of LEDs from. It says uses D-6, so Digital Pin 6, and it’s got 24 Pixels on it, there we go so we’re all set up. Then we’ve got to create the object which is Adafruit Neopixel and the object is called strip. We’ve got in our set up ‘strip begin” which initialises the Neopixels object which is called strip and show. Now the way Neopixels work is (I’ll go back to some of the other code to show you) there’s 2 commands which are going to be key to using Neopixels. You’ve got the object name which in this case is strip.setpixelcolor and from there you’d find the number, the LED number that you want to control. So you’d start from 0 and work your way around so if you wanted to control pixel 10 you wouldn’t put number ten you’d put 9 because it starts at zero and then you would put your color in RGB format. So then that sets the Pixel color but it doesn’t actually update that color and display it on the LED, you need to use this command, strip, or what ever your object is called, strip.show and that is going to update and push any new color information to the LED.
So this is our bare bones code, so if we go ahead and load this up. I’ll power my Photon up and actually there may already be a Neopixel code on here but I don’t think it’s set up for this, yeah so I’ve got it set up for this ring here, a smaller number of LEDs. Go ahead and target our device (I’ll find out which one is alive and breathing, right Photon Derek, give it a moment to compile fantastic its uploading and you can see the magenta LED, very good. Even though we have plugged it in and put our code on there nothing is happening because we haven’t set any colors to any of the pixels, we’ve just initialised it using begin and show commands so we need to use the strip.setpixelcolor - now remember to use the american spelling, there’s no ‘u’ in color and lets assign it to pixel number zero, so first pixel in the strip using RGB color, lets set it to a half brightness white, so say go 100, 100 and then that isn’t going to do anything. We need to use strip.show, now if we uploaded this it’s going to turn our first LED on just to a nice gentle white, which will be fantastic. Alright, so we are uploading our new code, give it a moment to connect to the Particle Cloud, now this is just going to turn on and do nothing, essentially it’s looping and turning on and on and on and on, which isn’t very interesting. So we’ll go through creating some animations, so there we go, it’s just a nice, well really it’s the one that’s under the USB cable so probably not a good example. But that is how you create color and that is a bare bones code that you can use for whatever you want, we’ve got another example there that just delays set pixel color and you can go through and add another line to turn it off if you like. But let’s do something a bit more interesting, we’ll go through and create an animation so you can copy this code into your file any and we’ve already included a library so you can just copy the code straight in. Now what we’re doing here is setting up everything like before, making sure that’s the right pixel type, and I’ve just gone through and defined some colors, so for peach, cyan, purple, blue, white and green I’ve created some RGB color settings that you can use and created a function called spin which is going to turn all the LEDs on and when it gets there turn them all off again. It’s a really simple animation effect, kind of cool and it’s going to go through spin and it is passing a color to it and it’s going to use that color to light it up. So it’ll go through sequentially and change all of those colors, pretty cool. Aright, so very simple, very simple, but let’s go ahead and flash this and you can see how i’m using strip.show after every set pixel color command because I want it to update continually in my four loop, so lets give this a moment There we go, peach, cyan, purple, blue, white and green, simple and that is all there is to controlling Neopixels, it’s incredibly easy and that is the Neopixel Ring. I’ve got this extra one set up here as you saw before and it works in the exact same way. In the documentation for this on the product page it will say that this is a WS2812 chip as well so you don’t need to use the ‘B’ and there are 16 Neopixels on it and we’ve got our power, our ground and our data pin so we connect out pin through our voltage ah, sorry our Logic Level Convertor and then connect it up to whatever pin you want. It can be driven from any digital pin which is really cool. And that is Using Neopixels with Particle, it’s incredibly easy guys and I encourage you to get some of these awesome LEDs for your projects and get making. I’m Sam, I’ll see you next time :-)