0% found this document useful (0 votes)
445 views

Blinking An Led

The document provides instructions for blinking an LED with an Arduino board. It explains that the Blink sketch uses digitalWrite() to turn the LED on and off by setting the pin to HIGH and LOW, and delay() to pause between changes for 1 second. The sketch code is broken down section by section, explaining the comments, declarations, setup() and loop() functions. Setup() configures the pin as an output, and loop() alternates calling digitalWrite() to change the pin state and delay() to pause between changes, blinking the LED on and off repeatedly.

Uploaded by

M. Ahmad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
445 views

Blinking An Led

The document provides instructions for blinking an LED with an Arduino board. It explains that the Blink sketch uses digitalWrite() to turn the LED on and off by setting the pin to HIGH and LOW, and delay() to pause between changes for 1 second. The sketch code is broken down section by section, explaining the comments, declarations, setup() and loop() functions. Setup() configures the pin as an output, and loop() alternates calling digitalWrite() to change the pin state and delay() to pause between changes, blinking the LED on and off repeatedly.

Uploaded by

M. Ahmad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Before uploading the sketch, First click the


Verify button. Verify checks the code to
make sure it makes sense. This doesn’t
necessarily mean your code will do what you
are anticipating, but it verifies that the syntax
is written in a way Arduino can
understand.You should see a progress bar and
the text Compiling Sketch (see Figure

1
below) for a few seconds, followed by
the text Done compiling after the
process has finished.

If the sketch compiled


successfully, you can now click the
Upload button next to the verify
button. A progress bar
appears, and you see a flurry of
activity on your board from the two
LEDs marked RX and TX. These show
that the Arduino is sending and receiving data. After a few seconds, the RX and TX
LEDs stop blinking, and a Done Uploading(see Figure below) message appears at
the bottom of the Arduino window.You should see the LED marked L blinking away
reassuringly: on for a second, off for a second.

2
Arduino code
/*Blink Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.*/

// Pin 13 has an LED connected on most Arduino boards.

// give it a name:

int led = 13;

// the setup routine runs once when you press reset:

void setup() {

// initialize the digital pin as an output.

pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

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.

int led = 13;

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

✓ mode: Either INPUT or OUTPUT

In the Blink sketch, after the two lines of comments, you see this line of code:

pinMode(led, OUTPUT);

The word pinMode is highlighted in orange.Orange indicates that Arduino


recognizes the word as a core function. OUTPUT is also coloured blue so that it can
be identified as a constant, which is a predefined variable in the Arduino language.
In this case, that constant sets the mode of this pin.

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:

digitalWrite(led, HIGH); // set the LED on

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

So digitalWrite(led, HIGH); in plain English would be “send 5V to pin

13 on the Arduino,” which is enough voltage to turn on an LED.

digitalWrite(led, LOW); // set the LED off

This tells Arduino to send 0V (ground) to pin 13, which turns the LED off.

Delay

This function does just what it says: It


stops the program for an amount of
time in milliseconds. In this case, the
value is 1000 milliseconds,
which is equal to one second. During
this time, nothing happens.

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.

You might also like