Project 1. Led Flashing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Project 1

LED Flashing

www.dfrobot.com.cn
01
www.dfrobot.com 01. LED Flashing 01

Let's get started!


Let’s kickstart our Arduino adventure! In the first lesson, you will learn
the basics of components such as LEDs, buttons and resistors - includ-
ing pull-up and pull-down resistors. Additionally, you will start to write
Arduino sketches to control a LED with your Arduino.
www.dfrobot.com 01. LED Flashing 02
LED Flashing
In use the Blink on- .
T can have a clear idea of how a LED works and how they can be used in a circuit.

Required Components:

x1 x1
DFROBOT
DFRduino
UNO v3.0[R3]

DFRduino UNO R3 Prototype Shield


with Breadboard

x2 x1 x1
Jumper
M/M Resistor 220R 5MM LED

*You may need to choose a different value resistor depending on the


LED you will use. We will mention how to calculate resistance value in
the latter part of this lesson.

*DFRduino is DF Robot’s signature Arduino board and functions the


same as any other Arduino board.
www.dfrobot.com 01. LED Flashing 03
Hardware

build the circuit Prototype


DF Wiring Definitions:
Shield on top of it.
bottom of the Prototype Shield should line up Green: Digital Connections
and slide in to the f . Be gentle and be
Blue: nalog Connections
careful not to bend them.
Peel the adhesive strip off the back of the Breadboard and then stick it on : Power Supply
to the Prototype Shield. ou can now set up the circuit according to the Black:
picture below.
White: Other
is standard practice to use wires of different colored insulation for
your own reference, but using different combinations of colors wont
stop the circuit working.

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

Fig 1-1 LED Flashing Circuit


www.dfrobot.com 01. LED Flashing 04
Arduino Sketch

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.)

The sample code 1-1:


//Project -- Blinking a LED
/*
Description: turn LED on and off every other second.
*/
int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}

When you ve finished entering the code, click on Verify to check


if the code can be compiled. If the code has no errors, click
Upload to upload code to the micro-controller. Now your onboard
LED should be blinking on and off.
www.dfrobot.com 01. LED Flashing 05
CODE

Comments: Multi-Line Comments: Declaring Variables:

/ * the text between these two


symbols will be commented out;
int ledPin = 10;
the compiler will ignore the text type name
and the text will appear gray * / of variables of variables

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

What is to digital pin 10. Variables display as orange te t in the sketch.


Integers can store numbers from -32768 to 32767. You must introduce, or declare variables

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.

If you want to set the digital pin


2 to input mode, what code
would you type?

Answer: pinMode (2, INPUT);

Function Segmenting code into functions allows a programmer to create modular


pieces of code that perform a defined task and then return to the area of
code from which the function was "called". The typical case for creating a
function is when one needs to perform the same action multiple times in a
program.
There are two required functions in an Arduino sketch, setup() and loop().
Other functions must be created outside the brackets of those two
functions.

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

If we scroll further down, we can The function format is as follows:


see the main part of the code.
This is the loop.
void loop() { digitalWrite (pin , value );
digitalWrite(ledPin,HIGH);
delay(1000); Pin Value HIGH/LOW
digitalWrite(ledPin,LOW);
delay(1000);
}

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);

delay() pauses the program for the


amount of time specified (in
miliseconds). (There are 1000 mil-
liseconds in 1 second.)
www.dfrobot.com 01. LED Flashing 09
Hardware

Breadboard

constructing and testing circuits


without having to permanently
solder them in place. Components
are pushed into the sockets on
the breadboard and then extra
to make
connections.

vi
ty Direction

The breadboard has two columns,


each with 17 strips of connections.
In this diagram the black lines
show how the top row is connect-
ed. This is the same for every other
row. The two columns are isolated
from one-another.

Fig 1-3 DIP Chip Inserted


www.dfrobot.com 01. LED Flashing 10

Resistors Read Resistor Color Rings


As the name suggests, resistors If you want to read the resistance The resistor value will be marked
resist the flow of electricity. The value from the resistor color code, on the on the outer package of
higher the value of the resistor, the visit this website for cal- your resistors, but what should we
more resistance it has and culation tables: http://www. do in case the label gets lost and
the less electrical current will flow 21ic.com/tools/compo nent/ there are no measuring tools at
through it. The unit of resistance 201003/54192.htm hand? The answer is to read the
is called the Ohm, which is resistor value. This is a group of
usually ( colored rings around the resistor.
letter Omega). Details are available online for
Unlike LEDs, resistors are those who are interested in having
not polarized (do not have a a try.
positive and negative lead) - they
can be connected either way Here is an online calculator for
around. Normally a LED needs five-color-ring resistor value
2V of voltage and 35 mA cur- calculating: http://www.21ic.com/
rent to be lit, so with a resistor of tools/compo nent/
would be able to 201003/54192.htm
control the flow
you might risk
burning it out. Be careful of this
because resistors can get hot!
www.dfrobot.com 01. LED Flashing 11

LEDs
A light-emitting diode (LED) is a
two-lead semiconductor
light
diode, which emits light
when activated.

Typically, LEDs have two leads,


one positive and one negative.
There are two ways to tell which
is the positive lead of the LED
and which the negative: Firstly,
the positive lead is longer. Sec-
ondly, where the negative lead
enters the body of the LED, there
is a flat edge to the case of the
LED.

LEDs are polarized, so they have


to be connected the right way
around. If you put the negative lead
of an LED in to the power supply
and the positive lead to ground,
the component will not wor , as
can be seen in the diagram to the
right.

n your it, you can also find LEDs


with 4 leads. This is an
with 3 primary color LEDs
embedded in to it. This will be
explored later.

You might also like