Blink Led
Blink Led
21
Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino
This is simply a comment in your code and is ignored Although we can call our variables anything we want,
by the compiler (the part of the IDE that turns your every variable name in C must start with a letter, the
code into instructions the Arduino can understand rest of the name can consist of letters, numbers and
before uploading it). Any text entered behind a // underscore characters. C recognises upper and lower
command will be ignored by the compiler and is simply case characters as being different. Finally, you cannot
there for you, or anyone else that reads your code. use any of C's keywords like main, while, switch etc as
Comments are essential in your code to help you variable names. Keywords are constants, variables
understand what is going on and how your code and function names that are defined as part of the
works. Comments can also be put after commands as Arduino language. Donʼt use a variable name that is
in the next line of the program. the same as a keyword. All keywords within the sketch
will appear in red.
Later on as your projects get more complex and your
code expands into hundreds or maybe thousands of So, you have set up an area in memory to store a
lines, comments will be vital in making it easy for you number of type integer and have stored in that area
to see how it works. You may come up with an the number 10. Imagine a variable as a small box
amazing piece of code, but if you go back and look at where you can keep things. A variable is called a
that code days, weeks or months alter, you may forget variable because you can change it. Later on we will
how it all works. Comments will help you understand it carryout mathematical calculations on variables to
easily. Also, if your code is meant to be seen by other make our program do more advanced stuff.
people (and as the whole ethos of the Arduino, and
indeed the whole Open Source community is to share Next we have our setup() function
code and schematics. We hope when you start
making your own cool stuff with the Arduino you will be void setup() {
willing to share it with the world) then comments will ! pinMode(ledPin, OUTPUT);
enable that person to understand what is going on in }
your code.
An Arduino sketch must have a setup() and loop()
You can also put comments into a block statement by function otherwise it will not work. The setup() function
using the /* and */ commands. E.g. is run once and once only at the start of the program
and is where you will issue general instructions to
/* All of the text within prepare the program before the main loop runs, such
the slash and the asterisks as setting up pin modes, setting serial baud rates, etc.
is a comment and will be
ignored by the compiler */ Basically a function is a block of code assembled into
one convenient block. For example, if we created our
The IDE will automatically turn the colour of any own function to carry out a whole series of
commented text to grey. complicated mathematics that had many lines of code,
we could run that code as many times as we liked
simply by calling the function name instead of writing
22
Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino
void loop() {
! digitalWrite(ledPin, HIGH);
out the code again each time. Later on we will go into ! delay(1000);
functions in more detail when we start to create our ! digitalWrite(ledPin, LOW);
own. ! delay(1000);
In the case of our program the setup() function only }
has one statement to carry out. The function starts
with The loop() function is the main program function and
runs continuously as long as our Arduino is turned on.
void setup() Every statement within the loop() function (within the
curly braces) is carried out, one by one, step by step,
and here we are telling the compiler that our function until the bottom of the function is reached, then the
is called setup, that it returns no data (void) and that loop starts again at the top of the function, and so on
we pass no parameters to it (empty parenthesis). If forever or until you turn the Arduino off or press the
our function returned an integer value and we also had Reset switch.
integer values to pass to it (e.g. for the function to
process) then it would look something like this In this project we want the LED to turn on, stay on for
one second, turn off and remain off for one second,
int myFunc(int x, int y) and then repeat. Therefore, the commands to tell the
Arduino to do that are contained within the loop()
In this case we have created a function (or a block of function as we wish them to repeat over and over.
code) called myFunc. This function has been passed
two integers called X and Y. Once the function has The first statement is
finished it will then return an integer value to the point
after where our function was called in the program digitalWrite(ledPin, HIGH);
(hence int before the function name).
and this writes a HIGH or a LOW value to the digital
All of the code within the function is contained within pin within the statement (in this case ledPin, which is
the curly braces. A { symbol starts the block of code Digital Pin 10). When you set a digital pin to HIGH you
and a } symbol ends the block. Anything in between are sending out 5 volts to that pin. When you set it to
those two symbols is code that belongs to the LOW the pin becomes 0 volts, or Ground.
function.
This statement therefore sends out 5v to digital pin 10
We will go into greater detail about functions later on and turns the LED on.
so donʼt worry about them for now. All you need to
know is that in this program, we have two functions, After that is
the first function is called setup and itʼs purpose is to
setup anything necessary for our program to work delay(1000);
before the main program loop runs.
and this statement simply tells the Arduino to wait for
void setup() { 1000 milliseconds (to 1 second as there are 1000
! pinMode(ledPin, OUTPUT); milliseconds in a second) before carrying out the next
} statement which is
digitalWrite(ledPin, LOW);
23
Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino
which will turn off the power going to digital pin 10 and For example, if we wanted the LED to stay on for 2
therefore turn the LED off. There is then another delay seconds, then go off for half a second we could do
statement for another 1000 milliseconds and then the this:-
function ends. However, as this is our main loop()
function, the function will now start again at the void loop() {
beginning. By following the program structure step by ! digitalWrite(ledPin, HIGH);
step again we can see that it is very simple. ! delay(2000);
! digitalWrite(ledPin, LOW);
// Project 1 - LED Flasher ! delay(500);
}
int ledPin = 10;
or maybe you would like the LED to stay off for 5
void setup() { seconds and then flash briefly (250ms), like the LED
! pinMode(ledPin, OUTPUT); indicator on a car alarm then you could do this:-
}
void loop() {
void loop() { ! digitalWrite(ledPin, HIGH);
! digitalWrite(ledPin, HIGH); ! delay(250);
! delay(1000); ! digitalWrite(ledPin, LOW);
! digitalWrite(ledPin, LOW); ! delay(5000);
! delay(1000); }
}
or make the LED flash on and off very fast
We start off by assigning a variable called ledPin,
giving that variable a value of 10. void loop() {
! digitalWrite(ledPin, HIGH);
Then we move onto the setup() function where we ! delay(50);
simply set the mode for digital pin 10 as an output. ! digitalWrite(ledPin, LOW);
! delay(50);
}
In the main program loop we set Digital Pin 10 to high,
sending out 5v. Then we wait for a second and then
By varying the on and off times of the LED you create
turn off the 5v to Pin 10, before waiting another
any effect you want. Well, within the bounds of a
second. The loop then starts again at the beginning
single LED going on and off that is.
and the LED will therefore turn on and off continuously
for as long as the Arduino has power.
Before we move onto something a little more exciting
letʼs take a look at the hardware and see how it works.
Now that you know this you can modify the code to
turn the LED on for a different period of time and also
turn it off for a different time period.
24
Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino
25
Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino
Black 0 0 x100
Orange 3 3 x103
Yellow 4 4 x104
White 9 9 x109
None ±20%
We need a 150Ω resistor, so if we look at the colour Our final component is an LED (Iʼm sure you can
table we see that we need 1 in the first band, which is figure out what the jumper wires do for yourself),
Brown, followed by a 5 in the next band which is which stands for Light Emitting Diode. A Diode is a
Green and we then need to multiply this by 101 (in device that permits current to flow in only one
other words add 1 zero) which is Brown in the 3rd direction. So, it is just like a valve in a water system,
band. The final band is irrelevant for our purposes as but in this case it is letting electrical current to go in
this is the tolerance. Our resistor has a gold band and one direction, but if the current tried to reverse and go
therefore has a tolerance of ±5% which means the back in the opposite direction the diode would stop it
actual value of the resistor can vary between 142.5Ω from doing so. Diodes can be useful to prevent
and 157.5Ω. We therefore need a resistor with a someone from accidently connecting the Power and
Brown, Green, Brown, Gold colour band combination Ground to the wrong terminals in a circuit and
which looks like this:- damaging the components.
If we needed a 1K (or 1 kilo-ohm) An LED is the same thing, but it also emits light. LEDʼs
resistor we would need a Brown, Black, come in all kinds of different colours and brightnesses
Red combination (1, 0, +2 zeros). If we and can also emit light in the ultraviolet and infrared
needed a 570K resistor the colours part of the spectrum (like in the LEDʼs in your TV
would be Green, Violet and Yellow. remote control).
In the same way, if you found a resistor and wanted to If you look carefully at the LED you will notice two
know what value it is you would do the same in things. One is that the legs are of different lengths and
reverse. So if you found this resistor also that on one side of the LED, instead of it being
and wanted to find out what value it cylindrical, it is flattened. These are indicators to show
was so you could store it away in you which leg is the Anode (Positive) and which is the
your nicely labelled resistor storage Cathode (Negative). The longer leg gets connected to
box, we could look at the table to the Positive Supply (3.3v) and the leg with the
see it has a value of 220Ω. flattened side goes to Ground.
26
Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino
If you connect the LED the wrong way, it will not Supplied with your kit is an RGB LED, which is 3
damage it (unless you put very high currents through LEDʼs in a single package. An RGB LED has a Red,
it) and indeed you can make use of that ʻfeatureʼ as Green and a Blue (hence RGB) LED in one package.
we will see later on. The LED has 4 legs, one will be a common anode or
cathode, common to all 3 LEDʼs and the other 3 will
It is essential that you then go to the anode or cathode of the individual Red,
always put a resistor in Green and Blue LEDʼs. By adjusting the brightness
series with the LED to values of the R, G and B channels of the RGB LED
ensure that the correct you can get any colour you want. The same effect can
current gets to the LED. be obtained if you used 3 separate red, green and
You can permanently blue LEDʼs.
damage the LED if you
fail to do this. Now that you know how the components work and
how the code in this project works, letʼs try something
As well as single colour a bit more interesting.
resistors you can also
obtain bi-colour and tri-
colour LEDʼs. These will have several legs coming out
of them with one of them being common (i.e. Common
anode or common cathode).
27