How to blink a LED using microcontrollers

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to blink a LED using microcontrollers

Post by Shane » Fri Jun 24, 2011 8:33 pm

This is the simplest Arduino demo, used to verify that everything is working - and will result in a blinking LED connected to a DIGITAL IN/OUT pin on the Arduino.

When a DIGITAL OUT pin on the Arduino is set HIGH, 5VDC will be sent out on the pin. 5VDC is too much voltage for a typical LED to handle, so a current limiting resistor must be used between the LED and the path to GROUND. A 270 OHM resistor is an appropriate value.

Connect the long lead (the anode) of the LED to the DIGITAL OUT pin on the ARDUINO (In our example, we have chosen to use PIN 13). Connect the short lead (the cathode) to one lead of the 270 OHM resistor. LEDs are polarized components - current can flow through the anode to the cathode, but not in the reverse direction. Connect the other lead of the resistor to GROUND. Resistors are not polarized components - current will flow through them exactly the same regardless of their orientation.
1.jpg
1.jpg (162.15 KiB) Viewed 2932 times

Code: Select all

 // LED BLINK DEMO

// This example turns an LED connected to digital pin 13 (D13) on the ARDUINO
 // on and then off in a cyle of 1 second on and 1 second off using the delay
 // function. The LED is connected to pin D13 by going through a 270 OHM resistor,
 // to the LED, then to ground.

int ledPin = 13; // declare variable for pin LED is connected to

void setup()
 {
 pinMode(ledPin, OUTPUT); //sets Digital Pin 13 as an OUTPUT
 }

void loop()
 {
 digitalWrite(ledPin, HIGH); //sets Pin 13 HIGH, turns on LED
 delay (1000); //waits for 1 second (1000 milliseconds) w/ LED on
 digitalWrite(ledPin, LOW); //sets Pin 13 LOW, turns off LED
 delay (1000); //waits for 1 second (1000 milliseconds) w/ LED off
 }
2.JPG
2.JPG (52.37 KiB) Viewed 2929 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”