Blinking An Led
Blinking An Led
Objective
To blink Led with the help of ardunio board and arduino IDE software.
To Learn how to implement the digital output.
To Explain the arduino code for blinking an Led.
Equipment
Arduino Uno board and Breadboard Arduino IDE software
LED
resistor
Theory
The specific sketch you want to use here is called Blink. It’s about the most basic
sketch you can write, a sort of “Hello, world!” for Arduino. After selecting arduino
board you are using and serial port named COM 1,COM 2,COM 3 from the tools
option on the menu bar.Click in the Arduino window. From the menu bar, choose
File➪Examples➪0Basics➪Blink.A following window is open in front of you.
1
below) for a few seconds, followed by
the text Done compiling after the
process has finished.
2
Arduino code
/*Blink Turns on an LED on for one second, then off for one second, repeatedly.
// give it a name:
void setup() {
pinMode(led, OUTPUT);
void loop() {
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
3
The sketch is made up of lines of code. When looking at the code as a whole, you
can identify four distinct sections:
✓ Comments
✓ Declarations
✓ void loop
✓ void setup
Comments:
Notice that the code lines are enclosed within the symbols /* and */. These symbols
mark the beginning and end of a multi-line or block comment.Comments are
completely ignored by the software when the sketch is compiled and uploaded.In
this example, the comment simply tells you the name of the sketch, and what it
does, and provides a note explaining that this example code is in the public
domain.The symbols // signify a single-line comment as opposed to a multiline
comment. Any code written after these lines will be ignored for that line. In this
case, the comment is describing the piece of code that comes after it.
Declaration:
Declarations are values that are stored for later use by the program. In this case, a
single variable is being declared, but you could declare many other variables or even
include libraries of code in your sketch. For now, all that is important to remember is
that variables can be declared before the setup function.
Functions:
The next two sections are functions and begin with the word void: void setup and
void loop. A function is a bit of code that performs a specific task, and that task is
often repetitive. Rather than writing the same code out again and again, you can
use a function to tell the code to perform this task again.
Void setup ()
4
Setup is the first function an Arduino program reads, and it runs only once. Its
purpose, as hinted in the name, is to set up the Arduino device, assigning values
and properties to the board that do not change during its operation.
PinMode
The pinMode function configures a specified pin either for input or output: to either
receive or send data. The function includes two parameters:
✓ pin: The number of the pin whose mode you want to set
In the Blink sketch, after the two lines of comments, you see this line of code:
pinMode(led, OUTPUT);
Void Loop ()
The next section you see in the Blink sketch is the void loop function. This is also
highlighted in orange so the Arduino software recognizes it as a core function. loop
is a function, but instead of running one time, it runs continuously until you press
the reset button on the Arduino board or you remove the power.
Digitalwrite
Within the loop function, you again see curly brackets and two different orange
functions: digitalWrite and delay.First is digitalWrite:
The comment says set LED on, but what exactly does that mean? The function
digitalWrite sends a digital value to a pin.digital pins have only two states: on or off.
In electrical terms, these can be referred to as either a HIGH or LOW value, which is
relative to the voltage of the board. An Arduino Uno requires 5V to run, which is
provided by either a USB or a higher external power supply, which the Arduino
board reduces to 5V. This means that a HIGH value is equal to 5V and LOW is equal
to 0V.The function includes two parameters:
✓ pin: The number of the pin whose mode you want to set
5
✓ value: Either HIGH or LOW
This tells Arduino to send 0V (ground) to pin 13, which turns the LED off.
Delay
Circuit Diagram:
Procedure:
Led is connected to pin 13 on most arduino boards, so if you're using the pin
13 you don't require Led, resistor and breadboard.Otherwise, Follow the
circuit diagram and connect the components as
Connect one end of Led to output pin (except pin 13 ) through a resistor
and other end to ground pin on arduino board.
Plugged your Arduino into your computer
6
Open the Arduino software
Set the board and serial port
Open the Blink sketch from the Examples folder and uploadit to the board
Assignment
Create a program that will blink two LEDs; one red, one green, alternately. That is,
when the red LED comes on, the green should turn off and when the green comes
on, the red should turn off. The red should be on for two seconds and the green
should be on for one second. At no time should both LEDs be on simultaneously or
off simultaneously (at least not within ordinary human perception). This blinking
pattern should repeat over and over: red on for two seconds, green on for one
second, red on for two, green on for one, and so on.
Include your code and a proper schematic diagram with component values. It is
suggested that Multisim or another schematic capture program be used to generate
the schematic.