Tutorial-0007.BlinkingLED History

Hide minor edits - Show changes to markup

August 16, 2014, at 09:49 PM by Scott Fitzgerald -
Changed line 19 from:
  pinMode(ledPin, HIGH);
to:
  pinMode(ledPin, OUTPUT);
November 18, 2006, at 02:04 PM by David A. Mellis -
Added lines 40-41:
November 18, 2006, at 01:45 PM by David A. Mellis -
Added lines 5-6:

LEDs

Added lines 9-10:

Code

Changed lines 36-38 from:

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 pin 13. The short leg connects to GND; the bulb of the LED will also typically have a flat edge on this side. 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).

to:

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

November 18, 2006, at 01:40 PM by David A. Mellis -
Deleted lines 35-70:

Code

The example code is very simple, credits are to be found in the comments.

 
/* Blinking LED
 * ------------
 *
 * turns on and off a light emitting diode(LED) connected to a digital  
 * pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino 
 * board because it has a resistor attached to it, needing only an LED

 *
 * Created 1 June 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 * based on an orginal by H. Barragan for the Wiring i/o board
 */

int ledPin = 13;                 // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
 
November 18, 2006, at 01:39 PM by David A. Mellis -
Changed lines 16-18 from:

} @]

to:

} @]

Changed lines 26-28 from:

} @]

to:

} @]

November 18, 2006, at 01:37 PM by David A. Mellis -
Changed lines 30-31 from:
to:

full sketch code: digital_write

November 18, 2006, at 01:37 PM by David A. Mellis -
Changed line 11 from:

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:

to:

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:

Changed lines 19-20 from:
to:

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

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.

November 18, 2006, at 01:29 PM by David A. Mellis -
Added line 8:
November 18, 2006, at 01:29 PM by David A. Mellis -
Added lines 8-10:

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:

Deleted lines 11-14:

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: [@

November 18, 2006, at 01:29 PM by David A. Mellis -
Deleted line 10:
November 18, 2006, at 01:29 PM by David A. Mellis -
Changed line 7 from:

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

to:

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

November 18, 2006, at 01:29 PM by David A. Mellis -
Changed line 7 from:

To blink the LED takes only a few lines of code. The first 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:

to:

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

Added lines 9-13:

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: [@

November 18, 2006, at 01:25 PM by David A. Mellis -
Changed lines 13-16 from:
 @]

to:

@]

November 18, 2006, at 01:25 PM by David A. Mellis -
Changed lines 8-9 from:

[@void setup()

to:

[@ void setup()

November 18, 2006, at 01:25 PM by David A. Mellis -
Changed line 8 from:
 [@void setup()
to:

[@void setup()

November 18, 2006, at 01:25 PM by David A. Mellis -
Changed line 8 from:
 [@ void setup()
to:
 [@void setup()
November 18, 2006, at 01:25 PM by David A. Mellis -
Changed lines 8-9 from:
 [@

void setup()

to:
 [@ void setup()
November 18, 2006, at 01:25 PM by David A. Mellis -
Deleted line 7:
November 18, 2006, at 01:24 PM by David A. Mellis -
Deleted line 9:
Deleted line 13:
November 18, 2006, at 01:24 PM by David A. Mellis -
Changed lines 9-10 from:

[=

to:
 [@

void setup() {

Changed lines 14-18 from:

=]

to:

}

 @]

November 18, 2006, at 01:23 PM by David A. Mellis -
Added lines 1-55:

Blinking LED

The first program every programmer learns consists in writing enough code to make their code show the sentence "Hello World!" on a screen. The blinking LED is the "Hello World!" of physical computing.

An LED is a small light (it stands for "light emitting diode") that works with relatively little power. The Arduino board has one built-in on digital pin 13.

To blink the LED takes only a few lines of code. The first 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:

pinMode(ledPin, HIGH);

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 pin 13. The short leg connects to GND; the bulb of the LED will also typically have a flat edge on this side. 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).

Attach:LedOnPin13.jpg Δ

Code

The example code is very simple, credits are to be found in the comments.

 
/* Blinking LED
 * ------------
 *
 * turns on and off a light emitting diode(LED) connected to a digital  
 * pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino 
 * board because it has a resistor attached to it, needing only an LED

 *
 * Created 1 June 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 * based on an orginal by H. Barragan for the Wiring i/o board
 */

int ledPin = 13;                 // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
 

Share