Arduino - LED - Blink

by DIYables in Circuits > Arduino

38 Views, 0 Favorites, 0 Comments

Arduino - LED - Blink

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-led-blink

This project turns an LED on and off with an Arduino Uno, first using the board's built-in LED, then an external LED wired to a digital pin. It is the basic building block for controlling any on/off device from code, from a single indicator light to a relay driving bigger machines.

Supplies

Wiring

The LED has two pins: cathode (-) and anode (+). Connect the cathode to GND. Connect the anode, through a 220 ohm resistor, to an Arduino digital pin such as pin 9. Using the LED Module with a built-in resistor simplifies this, since you only need to run two wires. The first example needs no external wiring at all, since it uses the Uno's built-in LED.

How It Works

A pin set to digital output mode can be driven to GND or to VCC by code. With the cathode tied to GND, driving the anode to GND turns the LED off, and driving it to VCC turns the LED on. Sending a PWM signal instead of a fixed HIGH/LOW would let you change the brightness, but this project only switches the LED fully on or off.

Code

The sketch sets a pin to OUTPUT with pinMode(), then uses digitalWrite() to alternate the pin between LOW and HIGH, with delay() pausing between each state. The first version toggles LED_BUILTIN every 1000 ms. For the external LED, change LED_BUILTIN to 9 and shorten the delay to 500 ms, which makes the LED blink faster.

arduino-led-blink.ino - the sketch from the tutorial, unchanged.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-led-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
}


Things That Go Wrong

If the LED never lights, check that the cathode goes to GND and the anode goes to the pin through the resistor; a reversed LED won't light. If you changed the pin number in code, make sure the wiring uses that same pin. Make sure the correct board and port are selected in the Arduino IDE before uploading.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-led-blink