0:00
In most programming languages, you start with a program that simply prints Hello World to the screen
0:08
The equivalent in the microcontroller world, such as Arduino, is getting light to blink on and off
0:16
The LED blinking sketch is the first program that you should run to test whether your Arduino board is working and is configured correctly
0:30
An LED which stands for light emitting diode is a small electronic component that's a bit like a light bulb, but it's more efficient and requires a lower voltage to operate
0:48
Before we get to the programming part, let's talk first about how to connect an LED to the Arduino microcontroller
0:58
To help you make the proper connection, you can download the Learn Arduino Intro app from the Google Play Store
1:07
The Arduino intro app contains a lot of Arduino projects to help you get started with Arduino
1:15
It will show you the materials you need, the breadboard diagram, and of course the Arduino code complete with comments
1:24
I'll put a link in the description below. LED stands for light-emitting diode
1:30
A diode only allows electricity to flow through it one way. So if you hook it up backwards, it won't work
1:40
An LED has two leads, or sometimes called legs. The longer leg, which is the positive side, is called the anode
1:50
The shorter leg, which is the negative side, is called the cathode
1:55
LEDs can come in many shapes, colors, and sizes. For this video, you'll be using a 5mm LED
2:05
The positive side is also called the anode while the negative side is called the cathode
2:11
The cathode can also be determined by a small flat portion along the side of the bulb
2:19
Now each digital pin of the Arduino is outputting 5 volts DC, and the air, and so, you can be a small
2:25
at 40 mll amps and most LEDs require a voltage of 2 volts and a current of 35
2:32
mliamps. So anything higher than 2 volts may damage your LED. Therefore you need a resistor
2:39
that will reduce the 5 volts to 2 volts and the current from 40mmmm to 35
2:45
mliamps if you want to display the LED at its maximum brightness. A resistor is a device
2:52
designed to cause a resistance to an electric current in order to to cause a drop in voltage across its terminals
2:59
You use resistors to decrease voltage or current to other devices. If you want the LED to be dimer, you could use a higher value of resistance
3:10
To get the correct resistance, you need to know alms law. The value of resistance is known as alms
3:17
According to alms law, voltage is equal to current times resistance. This is an entirely separate topic
3:25
so we'll not talk about this one in this video. For now, you can use a 220-oom resistor
3:32
Resistors have color bands on them that let you know what value they have
3:37
A 220-oom resistor has the colors red-red brown. Resistors don't have polarity, meaning they don't have a positive and a negative side
3:48
Therefore, you don't have to worry if it's inverted or not. Lastly, you'll be needing some jumper wires
3:55
The jumper wires you use can either be commercially available jumper wires, usually with
4:01
molded ends to make insertion into the breadboard easier, or you can make your own by cutting
4:06
short strips of the stiff single core wire and stripping away about one inch from the tip For a one LED circuit jumper wires are not necessarily required but it good to practice using jumper wires as early as now Again for our blinking
4:23
LED project, you'll be needing an LED, a 220-on resistor, jumper, jumper wires, and of course
4:30
a breadboard and your Arduino board. In this video, I'll be using an Arduino
4:36
Uno board, but you can use any Arduino board for this one. To start, let's open a
4:41
Open up the Arduino intro app and tap on Project Number 1, Blink from the Projects tab
4:48
Now we can see the materials we need. By default, you should have an Arduino board, breadboard, and jumper wires at hand
4:56
Now let's tap on the breadboard tab to view the breadboard diagram or how the components
5:02
are connected. In this diagram, the straight leg of the LED, which is called the cathode or the negative side
5:09
is inserted in J5, while the bent leg, which is the anode or the positive side, is inserted in J6
5:17
You don't have to follow this exactly. You can insert the legs of the LED anywhere on the
5:23
terminal strips as long as the negative side and the positive side are not directly connected
5:29
Just be sure that the LED is inserted firmly into the holes. Next, the negative side of the LED
5:36
should be connected to the ground or GND pin of your Arduino board. You can achieve this by
5:42
inserting one end of a jumper wire in H5 and the other end in the GND pin. Note that F5 to J5 are
5:51
connected via the metal strip found underneath the holes of the breadboard. You can use any color of a jumper
5:58
wire. Next, you connect the positive side of the LED to any of the digital pins. Well, except pin zero and one
6:06
These pins are reserved for serial communications. In this case, it is connected to digital pin 9
6:14
But before you make the connection, you have to put a 220-on resistor between the positive side of the LED and pin 9
6:24
This will bring down the 5 volts of the pin to 2 volts
6:28
If the full 5 volts will be fed directly to the LED, your LED will get damage
6:34
Note again that the resistor has no problem. polarity so you can place it in any way. In this diagram, one end of the resistor is placed on I6 and the other end on I10. Then a jumper wire is inserted in H6 going to digital pin 9 of the Arduino. Again, F10 to J10 are all connected in the breadboard. However, they are not connected to A10 to E10 because of the brake in the middle called the groove
7:03
Now your circuit is complete. You'll now have to connect the Arduino
7:06
circuit to your computer via the USB cable. Let's go now to the Arduino code or sketch
7:14
You can see the sketch for the blink project in the code tab of the Arduino intro app
7:19
If you have the Arduino IDEE installed already, you can open it up and type in the code you see in the app
7:25
You don't have to copy the green color text. These are called comments and they will not be read by the compiler
7:33
Press the verify or compile button at the top of the ID. to make sure there are no errors in your code
7:40
If this is successful, click the upload button to upload the code to your Arduino
7:45
If you have done everything right, you should now see the red lead on the breadboard flashing
7:50
on and off every second. Let's take a look at the code and the hardware to find out how they both work
7:58
This is the code to control the LED. An Arduino code or sketch must contain at least two functions
8:04
A function is a series of programming statements that can be called by name
8:09
The first function is the setup function. This function runs only once when the program starts
8:16
The second function is the loop function. This function runs repetitively over and over again as long as the Arduino board has power
8:25
This is the full breakdown of the Arduino LED blink code The first six lines are what you call a comment A comment is just text meant to be read by humans As a code it is just ignored
8:39
You're not really required to write comments. However, they are helpful if you have plenty of lines of code so you can keep track of what's
8:46
happening in your program. There are two types of comments. The first one is a multi-line comment
8:53
It starts with the forward slash asterisk symbol and ends with the asterisk
8:57
forward slash symbol. Anything in between is considered a comment and it will not produce an error
9:05
It can span many lines. The second one is the single line comment. It starts with a double
9:11
forward slash. Anything after that will be considered a comment. You don't have to end a single
9:18
line comment with another symbol as long as the text is written on the same line. Line 8 declares
9:25
a variable named Pin Lead. A variable is a place to store data. In this case, you are setting up
9:32
a variable of type I&T or int or integer, and an integer is a whole number with the range of
9:39
negative 32,768 to 32,767. Next, you have assigned that integer, the name of pin lead, and have
9:50
given it a value of 9. You don't have to call it pin lead. You could call it a
9:55
anything you wanted to. But you want your variable name to be descriptive. So you call it pin lead
10:02
to show that this variable is for the pin on the Arduino where the lead is connected. In this case
10:08
you are using digital pin 9. At the end of this statement is a semicolon. This symbol tells the
10:15
compiler that this statement is now complete. Although you can call your variables anything
10:21
every variable name in a C-based program like Arduino must start with a letter
10:28
The rest of the name can consist of letters, numbers, and underscore characters
10:33
Note that Arduino recognizes upper and lowercase characters as being different. Finally, you cannot use any of Arduino's keywords like Main, Wild, Switch, and Manymore as your variable names
10:46
Keywords are constants, variables, and function names that are defined. as part of the Arduino language
10:54
To help you avoid naming a variable after a keyword, all keywords within the sketch will appear in red
11:00
It also helps if the first letter in your variable name is capitalized
11:05
An Arduino sketch must have a setup and loop function, otherwise it will not work
11:11
The set up function runs once only at the start of the program
11:16
It is where you will issue general instructions to prepare the program before the main business
11:20
before the main loop runs, such as setting up pin modes, setting serial bod rates, and many others
11:27
Basically, a function is a bunch of code assembled into one convenient block
11:32
All of the code within the function is contained within the curly braces
11:36
An open curly brace symbol starts the block of code, and a close curly brace symbol ends the block
11:43
Anything in between those two symbols is code that belongs to the function
11:47
Your setup function only has one statement and that is pin mode, which is telling the Arduino that you want to set the mode of one of your pins to be in output mode rather than input mode
12:02
This also means that the Arduino is writing to a pin instead of reading from it
12:09
Within the parentheses, you put the pin number and the mode, output or input
12:14
Your pin number is pin led, which is the variable. that has been previously set to the value of 9
12:21
Therefore, this statement is simply telling the Arduino that Digital P9 is to be set to output mode
12:29
As the setup function runs only once, you now move on to the main function, which is the loop function
12:36
The loop function runs continuously as long as Arduino is turned on
12:41
Every statement within the loop function that is within the Curly Braces is carried out one by one step by step until the bottom of the function is reached Then the loop starts again at the top of the function
12:55
And so on forever until you turn the Arduino off or press the reset switch on your Arduino board
13:04
In this project you want the LED to turn on, stay on for one second, turn off and remain off for one second and then repeat
13:13
The commands to tell the Arduino to do this are contained within the loop function because you want them to repeat over and over and over again
13:22
The first statement is digital right pin lead comma high semicolon. The digital right command sets a high or a low value to the pin within the statement
13:34
In this case, pin lead, which is digital pin 9. When you set a pin to high, you are sending out five volts to that
13:43
pin. When you set it to low, the pin will have zero volts. This statement, therefore, sends out
13:50
5 volts to pin 9 and turns the LED on. After that is delay 1,000. This statement simply tells
13:59
the Arduino to wait for 1,000 milliseconds or 1 second before carrying out the next statement
14:07
The next line is digital right pin lead, comma, low semicolon. This will turn
14:13
off the power going to digital pin 9 and therefore turn the LED off
14:19
Then there's another delay statement for another 1,000 milliseconds and then the function ends
14:25
However, as this is your main loop function, the function will start again at the beginning
14:32
Now that your sketch is complete, the next thing you need to do is upload this sketch into your Arduino board
14:39
First, make sure that you connect your board to your computer, via USB cable
14:45
Next, go to Tools port and select the correct port number. In Windows, it starts with Com, COM, which is followed by a number
14:55
Normally, it's a number higher than one. You can check this out in the device manager settings in Windows
15:03
Then select your board by going to Tools menu and then board
15:07
Since I'm using an Arduino Uno board, that's what I'm going to select
15:11
Finally, click on the upload button. It's the button with the right-facing arrow
15:17
If everything is okay, you'll receive a done-uploading message on the lower portion of the IDE
15:24
If not, the IDE will give you an error message with some descriptions of what went wrong with your sketch
15:31
If your program won't compile or it doesn't do what you expect, here are a few things to check
15:37
First, the Arduino programming language is case-sensitive. In other words, pin lead with a small p is different from pin lead with a capital P
15:47
Second, blocks of code are encapsulated with curly braces. Make sure an open brace is paired with a close brace
15:56
Third, every open parentheses must have a matching close parentheses. Lastly, there should be no comments in numbers
16:05
In general, each program statement needs to end with a semicolon. This means that each line
16:11
of your program will have a semicolon. By following the program structure step by step
16:16
you can see that it is really very simple. To summarize everything, you start off by assigning
16:23
a variable called pin lead, giving that variable a value of 9, which is where the positive side
16:29
of the LED is connected. Then you move on to the setup function where you set the mode for
16:36
digital pin 9 as an output. In the main program loop, you set Digital Pin 9 to
16:41
high, sending out 5 volts, then you wait for a second and then turn off the 5 volts to
16:46
digital pin 9 before waiting another second. The loop then starts again at the beginning
16:52
The LED will turn on and off continuously for as long as the Arduino has power
16:58
Now that you know this, you can modify the code to turn the LED on or off for varying amounts of time