Thursday, June 11, 2015

Blink LED on Arduino

This example shows the simplest thing you can do with an Arduino to see physical output: it blinks an LED.

Circuit

To build the circuit, connect one end of the resistor to Arduino pin 13. This resistor is required as current limiting resistor to an LED.  Connect the long leg of the LED (the positive leg, called the anode) to the other end of the resistor OR if two legs have same length then see inside LED which have two parts big and small part, so big part is cathode(negative). Connect the short leg of the LED (the negative leg, called the cathode) to the Arduino GND, as shown in the diagram and the schematic below.


After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and enter the code below.


After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and enter the code below.

In the program below, the first thing you do is to initialize pin 13 as an output pin with the line

pinMode(13, OUTPUT);

In the main loop, you turn the LED on with the line:

digitalWrite(13, HIGH);

This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:


digitalWrite(13, LOW);

That takes pin 13 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 Arduino 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.

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);               // wait for a second
}


Once you've understood this example, check out the DigitalReadSerial example to learn how read a switch connected to the Arduino.

No comments:

Post a Comment