PL Report
PL Report
AND TECHNOLOGY
OEL REPORT
ASSIGNED BY:
DR AMIR ZEB
Blinking LED
Turn an LED on and off every second.
In this introductory Arduino example, we explore the most basic output function
– blinking the on-board LED. Before diving into the code, let's understand some
fundamentals.
COMPONENTS:
Arduino Board
LED
Circuit
This example uses the built-in LED that most Arduino boards have. This LED is connected to a
digital pin and its number may vary from board type to board type. To make your life easier, we have
a constant that is specified in every board descriptor file. This constant is pinled and allows you to
control the built-in LED easily.
If you want to light an external LED with this sketch, you need to build this circuit, where you connect
one end of the resistor to the digital pin correspondent to the pinled constant. Connect the long leg
of the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short leg
of the LED (the negative leg, called the cathode) to the GND. In the diagram below we show an UNO
board that has D13 as the pinled value.
The value of the resistor in series with the LED may be of a different value than 220 ohms; the LED
will light up also with values up to 1K ohm.
CODE FOR BLINKING LIGHT
/*
blink
3. Repeat
*/
int pinled = 9;
void setup() {
pinMode(pinled, OUTPUT);
void loop() {
After you build the circuit plug your Arduino board into your computer, start the Arduino
Software (IDE) and enter the code below. You may also load it from the menu
File/Examples/01.Basics/Blink . The first thing you do is to initialize pinled pin as an output
pin with the line
pinMode(pinled,OUTPUT);
Your setup function only has one statement and that is pinMode, which telling the Arduino
that you want to set the mode of one of your pins to be in OUTPUT mode, rather than INPUT
mode. This also means that the Arduino is writing to a pin instead of reading from it.
Within the parenthesis, you put the pin number and the mode (OUTPUT or INPUT). Your pin
number is pinled, which is the variable that has been previously set to the value 9. Therefore, this
statement is simply telling the Arduino that Digital Pin 9 is to be set to OUTPUT mode. As
the setup( ) function runs only once, you now move onto the main function which is the loop
function.
In the main loop, you turn the LED on with the line:
digitalWrite(pinled,HIGH);
This supplies 5 volts to the LED anode. That creates a voltage difference across the pins of the
LED, and lights it up. Then you turn it off with the line:
digitalWrite(pinled, LOW);
That takes the pinled pin back to 0 volts, and turns the LED off. In between the on and the off, you want
enough time for a person to see the change, so the
delay()
commands tell the board to do nothing for 1000 milliseconds, or one second. When you use the
delay()
command, nothing else happens for that amount of time. Once you've understood the basic examples,
check out the BlinkWithoutDelay example to learn how to create a delay while doing other things.
The loop( ) function runs continuously as long as the Arduino is turned on. Every statement within
the loop( ) function (that is within it’s 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, and so
on forever or until you turn the Arduino off or press the Reset switch on your Arduino board.
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. The commands to tell the Arduino to do this are contained within
the loop ( ) function because you want them to repeat over and over. The first statement is
digitalWrite(pinled, HIGH);
Conclusion:
This simple project serves as a starting point for Arduino beginners. As you progress, explore
advanced examples like "BlinkWithoutDelay" to incorporate delays while performing
additional tasks. The loop() function runs continuously until the Arduino is powered off or
reset, providing a foundation for more complex projects.