LED Blinking with Arduino Uno
Code-
// the setup function runs once when you press reset or
power the board
void setup()
{
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop()
{
digitalWrite(13, HIGH); // turn the LED on (HIGH is the
voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the
voltage LOW
delay(1000); // wait for a second
}
Code
To blink the LED takes only a few lines of code. The first thing we do is define a variable that will hold the number
of the pin that the LED is connected to. We don't have to do this (we could just use the pin number throughout the
code) but it makes it easier to change to a different pin. We use an integer variable (called an int).
int ledPin = 13;
The second thing we need to do is configure as an output the pin connected to the LED. We do this with a call to
the pinMode() function, inside of the sketch's setup() function:
void setup()
pinMode(ledPin, OUTPUT);
Finally, we have to turn the LED on and off with the sketch's loop() function. We do this with two calls to
the digitalWrite() function, one with HIGH to turn the LED on and one with LOW to turn the LED off. If we simply
alternated calls to these two functions, the LED would turn on and off too quickly for us to see, so we add two calls
to delay() to slow things down. The delay function works with milliseconds, so we pass it 1000 to pause for a
second.
void loop()
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
full sketch code: digital_write
Upload the sketch to the board and you should see the on-board LED begin to blink: on for one second, off for the
next.
Connecting an LED
LEDs have polarity, which means they will only light up if you orient the legs
properly. The long leg is typically positive, and should connect to a digital pin on
the Arduino board. The short leg goes to GND; the bulb of the LED will also
typically have a flat edge on this side.
In order to protect the LED, you will also need use a resistor "in series" with the
LED.
If the LED doesn't light up, trying reversing the legs (you won't hurt the LED if
you plug it in backwards for a short period of time).
LED Blinking Using Arduino UNO
int ledPin = 13;
void setup()
pinMode(ledPin, OUTPUT);
void loop()
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
/*
Blink
Turns an LED on for one second, then off for one
second, repeatedly.
Most Arduinos have an on-board LED you can control. On
the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6.
LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is
connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage
level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the
voltage LOW
delay(1000); // wait for a second