![Hardware with Arduino & JavaScript](https://static.frontendmasters.com/assets/courses/2023-09-26-arduino-javascript/thumb.webp)
Check out a free preview of the full Hardware with Arduino & JavaScript course
The "Pulsing the LED" Lesson is part of the full, Hardware with Arduino & JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Steve uses the pulse method to demonstrate Pulse Width Modulation. Digital pins simulate pulsing or fading by rapidly sending on/off signals.
Transcript from the "Pulsing the LED" Lesson
[00:00:00]
>> I'm gonna give you a side quest for a moment, which is if we go and look at the Johnny-Five docs, There are a bunch of different methods on the LED. So let's go ahead and let's take a look there. We can see that we've got, in this case, there's another diagram.
[00:00:26]
They're not using a resistor. I'm gonna use a resistor. We've got blank. We've also got the ones, I showed you before in the repo. There was fade in and fade out. There's pulse, which we can't use just yet, but actually, why don't this point, why don't you set it to actually a given, let's see, what could be fun?
[00:00:44]
We can't fade just yet. And I'll tell you, actually you can fade because we're on pin 13, actually let's do polls and then I'll explain how it works. Awesome, so we saw that blinking works, we saw we can manually turn it on and off. Interesting things happen if I try to use like pulse or fade in or fade out, which is I get yelled at.
[00:01:08]
Once it loads, like I said, I have my font very big and it's a very long message. But the important part here, it says, pin 13 is not a valid PWM pin. What does that mean? Let's talk about it. So if you get a regular light bulb like the old-school ones, you can kinda fade them on and off, right?
[00:01:31]
And the way that that works is you send it more electricity or less electricity. If you send it less electricity, it is less bright. If you send it more, it is more bright. And that's, again, that voltage current kind of stuff. Here's the thing about digital. Digital is 1s and 0s, which is on or off.
[00:01:57]
There's no in-between in computers at the time of this recording, there's no in-between 1 and 0, it's a 1 one or it's a 0. So like 0.5, how do you fade between 0 and 1? The answer is you cheat, obviously, right? And the way that we cheat is something called pulse width modulation, which is we just say 1 and 0 real fast.
[00:02:25]
[LAUGH] Right, because, yeah, we've got the fourth dimension of time, right? Things can only be 1 or 0 at a given moment in time, but most of this is computers, you turn it off and you turn it on really fast. And the speed at which you were turning it off or turning it on is how we simulate a certain amount in between 0 and 1, right?
[00:02:48]
So, just consistently 5 volts would be on, consistently 0 volts would be off, but the rate at which that square wave kind of fills in is all the values between 0 and 1 through cheating, right? And I think I said this before. Some of those pins have a little squiggly line.
[00:03:11]
Some of them do not. That squiggly line, as you can see by this very scientific graph, suggests that there's a pin that supports pulse width modulation, which, jives with the error message that we got from Johnny-Five, which is, this is not a valid PWM pin, which makes total sense because there's no squiggly line, right?
[00:03:35]
And so our next mission now is to both do two things. Take that yellow wire or I use the yellow wire, you use whatever color wire spoke to you. And what we wanna do is both update the code and switch the wire to use a pin that supports PWM or scientifically has a squiggly line next to the number, right?
[00:04:01]
So I'm gonna go switch over and do that in a second, and you can join me as well and we'll take a look at it. So I am going, I'm a responsible person, I'm gonna unplug my board because I'm in front of people. If you don't at home, I forgive you.
[00:04:20]
So, I go here and if I look closely I can see, okay, 13, the next one that has a squiggly line, it doesn't matter which one you use, it only matters that you've got the squiggles. So I'm gonna move it down into 11, and then I can go plug it back in.
[00:04:44]
And then I go back over to my code and some really complicated code change. You're ready? I'm gonna take this 3. I'm gonna turn it into a 1. That code changes it from 13 to 11. I should make sure it works before I actually really, [LAUGH]. Go let it start up.
[00:05:14]
And now if we look back over my board, it is now pulsing, cuz now we can simulate, even though it is a digital pin, we simulate that we are changing the amount of voltage going to it. And we are through a lot of on and off real fast.
[00:05:29]
The LED doesn't understand the difference between digital and analog. It is simply two conductors and an LED. But as far as we're concerned, the LED doesn't concern itself with anything. As far as we're concerned, it is emulating the idea of pulsing through just rapid, giving it electricity and not, right?
[00:05:52]
And so, generally speaking, if you do need to do something that is analog-esque but from computers, right, you do have some analog ports as well that you can use. But in this case we're using the digital ones. You can simulate that by any pin that happens to support pulse width modulation, which again, very scientifically uses squiggly lines.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops