Introduction to Arduino 7 – Pulsing an LED

Now we're going to make our thirdcircuit.

To make an LED pulse we need to give it a voltage that ranges between 0 and 5 volts, not a voltage that is 0 or 5, and to do that we use PWM, or pulse widthmodulation PWM allows for a pin to be an outputbetween 0 and 5 volts, and to do this, it swaps really quickly between high andlow values, and this is the same principle as most dimmer switches work in ceiling lights.

This animation here is exactly what isgoing to be happening with the LED.

So as that period of the PWM signal being high increases, the brightness of the LED increases.

The wiring of this circuit is simplygoing to be an extension of the last circuit we built.

Where we're just going toadd another piece to this overall puzzle.

To use PWM, we can't use the onboard LED anymore and this is because if we look at Pin 13, it is not able to use PWM.

So instead, we're going to use Pin 3 and wire it to an LED.

If we move over to the camera I've still got the circuit from before wired up.

So now we're going to introduce some more complexity.

First thing you should do is unplug it before we start playing with electricity and, like the diagram, we'regoing to take our LED and we're going to place it so that the shorter leg is connected to ground.

Keep in mind that an LED will only work when it is plugged in the correct way.

It will not power on if it's in the opposite way.

So the shorterleg needs to be connected to ground.

I'm then going to take a resistor and I'mgoing to plug it into the same route and then I'm going to take my cable from the end of the resistor and plug it into Pin 3 and that should be all the wiring weneed for this circuit.

Before we start programming, I need to introduce one more function that we'll need to use.

The function we are going to need is 'analog write' and this is to give an analog voltage to one of these pins.

A value of 0 is for 0 volts and a value of 255 is for 5 volts.

So knowing this, we can go to Arduino and start coding.

And with this new project, I'm going to start like we did before, and initialize the variables we're going to use.

This time we're going to dothings a little bit differently, where we use variables to allocate pins, and this is simply to make code easier to read, as the comments are kind of in the codewhen we use appropriate variable names like this.

So, to start, we're going toinit the pin numbers, or initialize.

We're going to have an integer, which is our LED pin, is going to equals 3 because that is what we hooked it up to.

We're also going to have our button pin, 10.

From there we're going to initializesome other variables.

So we're going to have our button, whichwill be the same as before.

it would be a boolean, either on or off, and we're goingto have the brightness of the LED and we're going to set it so it starts atzero.

Now this brightness is what we are goingto vary as we press down the button, So the plan for this little project is tomake it so that the brightness of the LED will increase when we press the button.

So in the setup we want to initialize the pins and we are going to have the LED pin in 'output' and we are going to have the buttonpin in 'input' and that is all we're going to need touse in the setup.

So now we're going to move to the loop and this is where we're going to start using a control structure from before.

We're going to use an 'if'statement.

So at the start of a loop, we're going to read the button.

So like the last piece of code, but it's going to digitally read the button and the last time, in 'digital read' we just put the number 10 but instead now thatwe've allocated a variable for it, we're going to put in 'button pin'.

and as you can see now, we don't need to keep writing 10 in all these functions and ask ourselves, “What does ten mean?” because it can get a little bit confusing.

So here we're just going to read the button using our variable we created.

I'm just going to write a comment here to tell myself what I want to do, say, if the button is pressed, we want to increase brightness.

So we're going to write 'if' And we're going to say that if our button is high, So the high voltage.



So we have 'true', 'false', 'high', 'low'.

They are the same things to Arduino.

And we're going to start our function byusing a curly brace.

And when I click 'enter', it will also put the closing brace for us, so we don't need to do that.

We've press the button, our brightness iszero, the first thing we're going to do is increase it.

We're going to do thatusing 'plus plus' ( ++ ).

this is going to increment the brightness.

That's one of those compound operators from before.

The next thing I want to do is, when I'mincrementing this variable, if we hold the button for long enough, it's going to get past that 255, the maximum value for 'analog right'.

So we're going to use a 'modulo' compound operator to make sure it stays within that range.

We have brightness 'per cent equals' ( %= ) 255.

That's always going to keep brightness in between 0 and 255.

The next thing I want to do, is I want to 'analog write'.

So to out to the LED pin, we're going to give the current brightness, and we're going to add a short delay.

We need to put this delay in because this loop will actually be running very fast and so if we don't put it in, we actually won't be able to see the visible change in brightness.

So, now, we can just upload that to the board.

I'm just gonna plug inthe board again.

Click 'Upload' Cool.

Now, if we go over to the board, As I press the button, the LED will get brighter.

Now it might be a little bit hard to see the change of brightness at the end, but that's not only because ofthe camera, that's actually because of the LED as well and so one thing I'mgoing to implement is a way to make sure that this brightness is changing thesame amount as we press the button.

Now, it's not going to be changing exactlythe same amount, but for us, it might as well be.

Knowing that circuit works, I'm going to quickly go back to the slides and introduce the Serial Monitor.

The Serial Monitor is going to allow us to pick a better top value than 255 for the max brightness.

The Serial Monitor allows the Arduino to communicate with the computer using serial communication.

We're going to needtwo main pieces of code to do that.

'Serial dot begin', which will allow us toset the rate at which communication will happen.

The Arduino unit is going to use9600 as a communication rate.

If you set this to a different value, you'll get absolute incomprehensible garbage, so make sure it is 9600.

If you are using a different board, you'll have to find out the value for that board.

We're alsogoing to use 'Serial print ln', which is going to allow us to print a value on to the next line of the serial monitor.

Now we can jump back into Arduino, and introduce those things.

Alright, so, in the setup, you're going to need tostart the serial communication, and to do that, we're going to do what was in the slide, 'Serial dot begin' and 9600.

We're also going to output what the brightness of the LED is.

So I'm going to put this here just so it is run every loop.

Obviously where you put it will determine when it is printed, so if it's printed in the 'if' statement, it will only print when the button is pressed but I'm just going to put it there, just so it's always printing.

There's 'Serial print ln' and we're going to print 'brightness' I'm going to upload the code again.

So, as before, as we press the button on the LED, the brightness will increase, but we get this weird plateau at the top that we want to get rid of.

So, on the computer, I'm going to go to Tools, you click on Serial Monitor and as you can see, we've got a string ofzeros right now and this is the current brightness of the LED.

So if I press this button, you'll see that number increase.

So what I'm going to do, is I'm going topress the button and see what value is on the serial monitor when it looks likeit's at max brightness.

So I'm probably going to stop that there, which seems to be about 95.

So if I go back to the code, and instead of 255 I'm going to put in 95.

And so effectively, using that 'modulo' operator, now the maximum limit ofbrightness is 95.

If it's 100, brightness is going to get chopped down to 5.

And so, we are making our 'analog write' now in between zero and 95.

So if I now reload the code, we've now uploaded it to the board, then if I go to the camera agian, I press this LED, it's gonna get brighter, and then it's going to drop tothe next cycle a little quicker.

Now it still might not be quite as visible inthe camera, but to me it looks like it's always increasing at the same rate.

So now we've learnt about control structures and how we can PWM out to a pin, We're going to build up into an example that is going to allow us to automatically play the Dino 'No Internet' game on Google Chrome.

So, that'll be in the next few videos.


Leave a Reply