Check out a free preview of the full Nodebots / Hardware course:
The "The Problem with LED Pulsing" Lesson is part of the full, Nodebots / Hardware course featured in this preview video. Here's what you'd learn in this lesson:

Steve explains what pulse width modulation (PWM) is and how digital devices use it to emulate analog signals.

Get Unlimited Access Now

Transcript from the "The Problem with LED Pulsing" Lesson

[00:00:00]
>> Steve Kinney: So what I would like you to do is that we've switched back over. I'm using Visual Studio Code, you might be using a different one, but we'll actually take a look at it on mine. And if I type led, you can see that there's a whole bunch of different methods that I called.

[00:00:15] There was blink, brightness, which will basically allow you to set it somewhere along the lines. Fun fact, most of these are not gonna work right now, we'll get into that in a second. But we can fade it, and these are all just utility methods given to us by Johnny-Five.

[00:00:36] So you have a whole bunch of other ones, a strobe seems like it's really cool. It is an alias for blink, so if you're really excited about that one, it's gonna be the same thing. But there's a bunch of really cool ones. What I'm really interested in is fading and pulsing.

[00:00:56] Right, fade is a one direction, it'll go from off and fade to on. And then for pulsing, it's effectively the same thing as blink. It's fade to on, fade to off, fade to on, fade to off. And you can give it amount of time. So we're actually going to do that instead.

[00:01:14] So instead of blink, we'll say pulse. I'm gonna save it, and I'm gonna go ahead and run this code. I'll kill the previous instance, if I didn't already. And I will run and I will deploy it, and we will all see what's about to happen. So if you look at the board, my code is deployed.

[00:01:37] There's two things to see. One, is there is no pulsing. Two, as I switched back to my computer, they crashed. And the error message is 0 is not a valid PWM pin. Right, so that gives us a sense, I kind of hinted before if we go ahead and look at that very helpful chart that I showed earlier.

[00:02:04] That error message is accurate, our PWM pins are five and six on either the a port or the b port. So, not zero, so when it said it's not a PWM pin, a helpful error message. Or at least an accurate error message, right? So the big question is what is PWM, and why for pulsing does it need to be a PWM pin?

[00:02:31] The problem with programming hardware digitally is, again, digital devices are ones and zeros, right, which is you're on or you're off. And that worked great for blinking, not so good for pulsing, where we're gradually going from zero to one. And if that's not a thing that we can do digitally, is this just a dead end that I have led you down?

[00:02:58] Or is there a solution in that error message? Which is where it said 0 is not a PWM pin, it seems like the solution might be plug it into a PWM pin. We know that they're five and six. So it's very gratifying, but can we make it fade in and out?

[00:03:12] Ones and zeroes, it's hard. So PWM, I've been purposely cagey about what it stood for until now because I feel like that would begin to give away the answer. It's pulse width modulation. And so it's basically the way that we can fake somewhere between zero and one, when we only have zeroes and ones.

[00:03:33] So how do we fake it? We just alternate real fast. If you alternate real fast between zero and one, the analog devices will kind of treat it as somewhere between zero and one. So not every pin on our board supports it, so we need to use a pin that does support it.

[00:03:52] Now that's fine and good, but it leads us to our next problem. Our next problem is pin five and six, is very far away from that ground pin. [LAUGH] And, I mean if you really tried you could probably reach, but seems like that's not a long term solution.

[00:04:16] So we're gonna need to bring in another piece of equipment called the breadboard. And I'm gonna take a quick moment to explain to how a breadboard works. And then we'll be using a breadboard pretty much for the rest of the day.