Project 1. Led Flashing
Project 1. Led Flashing
Project 1. Led Flashing
LED Flashing
www.dfrobot.com.cn
01
www.dfrobot.com 01. LED Flashing 01
Required Components:
x1 x1
DFROBOT
DFRduino
UNO v3.0[R3]
x2 x1 x1
Jumper
M/M Resistor 220R 5MM LED
Normally red wire indicates power supply (Vcc), black wire indicates
ground (GND), green wire indicates digital pins, blue wire indicates analog
pins, and white is other.
Double check the orientation of LED leads on the circuit. LEDs are po-
larized (will only work if placed in the circuit the correct way around). he
long leg of the LED connects to Vcc. (In this e ample Pin 10) and the short
leg connects to GND.
When you finish the circuit, connect the Arduino controller and computer
with the provided
2
Are
f Gnd 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1
- +
5V
1
GND
3 2 3 1 3 2
RS
T 3V 5V GND VI
N 0 1 2 3 4 5
4 1 4 1
Open the Arduino IDE and enter the code as sample code 1-1 shows.
(We highly recommend you type code instead of copying and pasting so
that you can develop your coding skills and learn to code by heart.)
Any line of code that has “//” put Similar to single line comments, It is so called
Declaring variableisdeclaration.
variables a useful wayA
before it will not be compiled by any text between / * and * / will variable
of namingis for
anddata storage.In
storing values this
for
the complier. The Arduino IDE be ignored by the compiler. Once sample,
later use integers(int) are applied
by the program. Inte-
indicates this by turning the line again, the Arduino IDE will turn which
gers (intrepresent
) representnumbers
numbersrange
of code grey automatically. This this text grey to show that it is is from -32768
ranging fromto-32768
32767.The storage
to 32767.
allows you to write plain English commented out. This is a useful content
In decides
the above thewe variable
example, have
descriptions of the code you or way of annotating code. type.Here
input an we input 10.
integer: 10, an
“ integer.
” is
others have written and makes it the variable name we have chosen.
easier for other people who might variable
Of name
course, you ismay
the name of
it the
not be able to read code to variable, standing
anything for theofvalue.Of
you like instead
understand. We refer to this as course, but
ledPin, youit'smay name
better it at the
to name will
commenting out. instead of
variable ledPin), but
according it'sfunction.
to its better to
name the
Here the variable according
ledPin indi-to its
function.lHere
cates the isvariable
that the LED ledPin
connected to
indicates10that
Pin-out the LED is connected
of Arduino.
to Pin-out
Use 10 of Arduino.
a semicolon (;;) to conclude
Please
the use a ";" Ifto you
declaration. conclude the
don’t use
declaration.The
the semicolon, thesemicolon under
program will not
English input
recognise themethod is necessary.
declaration, so this is
important
A variable is a place to store a piece of data. It has a name, a value, and a type. In the above
e ample, “int” (integer) is the type, “ledPin” is the name and “10” is the value. In this e ample
we’re declaring a variable named ledPin of type “int” (integer), meaning the LED is connected
a variable? before you use them. Later on in the program, you can simply type the variable name rather
than typing out the data over and over again in each line of code, at which point its value will
be looked up and used by the IDE.
When you try to name a variable, start it with a letter followed with letter, number or
underscore. The language we are using (C) is case sensitive. There are certain names that you
cannot use such as “main”, “if”, “while” as these have their own pre-assigned function in the IDE.
Don’t forget the variable names are case-sensitive!
www.dfrobot.com 01. LED Flashing 06
The setup() function In this e ple there is only The function format is as follows:
one line in the setup() function:
void setup () {} pinMode pinMode
No return Empty
pinMode(ledPin, OUTPUT); Mode
Pin
value function b kets (OUTPUT/INPUT)
The setup() function is read by This function is used to define “pinMode” configures the spec-
the Arduino when a sketch digital pin working behavior. Dig- ified digital pin to behave either
starts. It is used it to initialize ital pins are defined as an input as an input or an output. It
variables, pin modes, initialize (INPUT) or an output (OUTPUT). has two parameters:
libraries, etc. The setup function In the e ample above you can “pin”: the number of the pin
will only run once after each see brackets containing two whose mode you wish to set
power-up or reset of the Ar- parameters: the variable (ledPin) “mode”: INPUT, OUTPUT,
duino board. and its behaviour (OUTPUT). or INPUT_PULLUP.
Difference of INPUT is signal that sent from outside events to Arduino such as button.
OUTPUT is signal that sent from Arduino to the environment such as LED
INPUT and and buzzer.
OUTPUT
www.dfrobot.com 01. LED Flashing 07
The Arduino program must In this project we want the LED digitalWrite writes a HIGH (on)
include the setup () and loop () to turn on for 1 second and then or a LOW (off) value to a digital
function, otherwise it won t work. turn off for 1 second, and re- pin. If the pin has been
After creating a setup() function, peat this action over and over. configured as an OUTPUT
which initializes and sets the initial How can we express this in code? with pinMode(), its voltage will
values, the loop() function does be set to the
precisely what its name suggests, Look at the loop () function within
corresponding value: 5V (or
and loops consecutively, allowing the first statement.
3.3V on 3.3V boards) for HIGH
your program to change and This involves another function:
and 0V (ground) for LOW. Please
respond. Use it to actively control digitalWrite ().
note that digitalWrite() is ap-
the Arduino board. Here we want plied only under the condition
to control the LED constantly on digitalWrite(ledPin,HIGH);
that pinMode() is set as
and off every other second. OUTPUT. Why? Read on
How can we make that happen?
If pinMode configures a digital pin to behave as an input, you should use the
The Relation digitalRead() function. If the pin is configured as an output, then you should use
digitalWrite().
of pinMode(), NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when
digitalWrite() calling digitalWrite(HIGH), the LED may appear dim. This is because without explic-
itly setting a pin-Mode(), digitalWrite() will enable the internal pull-up resistor, which
and acts like a large current-limiting resistor.
digitalRead() e.g. LED, Buzzer e.g. Press button control
pinMode(pin,OUTPUT) pinMode(pin,INPUT)
digitalWrite(pin,HIGH/LOW) digitalRead(pin)
www.dfrobot.com 01. LED Flashing 08
Next:
delay(1000);
Breadboard
vi
ty Direction
LEDs
A light-emitting diode (LED) is a
two-lead semiconductor
light
diode, which emits light
when activated.