Arduino Fundamentals 101: How to Blink an LED

Though there are plenty of maker boards around such as the Raspberry Pi and Odroid XU4. However, the Arduino offers different capabilities. Whereas the Raspberry Pi focuses on maker projects ranging from basic Linux desktops, retro gaming, and home theatre PC use to advanced robotics, the Arduino concentrates on electronic platforms. It's a circuit board boasting loads of functionality. Learn about Arduino basics and how to use Arduino to blink an LED.

What is an Arduino?

The Arduino is an open-source single-board microcontroller. It differs from single-board computers in that it's focused on microcontroller use rather than general computer use. As such, Arduino boards boast tons of input/output (I/O) devices, breadboards, and circuits.

There are loads of Arduino projects, from using Arduino buzzers to creating wearables and even building smart home gadgets like thermostats. Arduino basics include simple endeavors such as blinking an LED.

Arduino Blink LED

First, you'll need to understand how an Arduino functions. Programs interface with Arduino hardware to perform basic tasks. A program for the Arduino may run in any language. The Arduino IDE, or integrated development environment, features a code editor and easy means of uploading and compiling code. 

An Arduino blink experiment is similar to the entry-level "Hello, World!" for programmers. You can blink an LED with an Arduino using several methods. We'll be using an LED connected to the Arduino board. Select an LED of any color. The short leg, or the negative LED or cathode goes into the GND or ground slot. The long leg gets put in the D13 space. This is a typical set up with the resistor connected to pin 13 as well as the ground. First up, we'll blink an LED continuously. I used an Arduino UNO R3

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
}​

What's happening here is the LED BUILTIN pin gets initialized with the output command. Then the LED gets turned to high when 5 volts are sent to its anode. The LED in turn gets shut off with the low command which then sends 0 volts. This uses the with the pinMode(), digitalWrite(), and delay() commands. 

Alternatively, you may blink an LED by running:
#define LED_PIN 13 // Pin number attached to LED.

void setup() {
pinMode(LED_PIN, OUTPUT); // Configure pin 13 to be a digital output.
}

void loop() {
digitalWrite(LED_PIN, HIGH); // Turn on the LED.
delay(1000); // Wait 1 second (1000 milliseconds).
digitalWrite(LED_PIN, LOW); // Turn off the LED.
delay(1000); // Wait 1 second.
}
This is essentially the same command but with slightly different wording. What's neat about the Arduino is the room for creativity in creating commands. Since it's on a loop, your LED will continue to blink until you stop it.

But you can also perform an RGB blink test. An RGB LED features four leads: one connects to the positive elements of the solitary LED, and then the blue, green, red, and cathode negative leads. You'll want to insert the blue lead into the number 3 pin, green into 5, and red into the 6. Now, execute the code below:

//www.elegoo.com
//2016.12.8
// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}
// define variables
int redValue;
int greenValue;
int blueValue;
// main loop
void loop()
{
#define delayTime 10 // fading time between colors
redValue = 255; // choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;
// this is unnecessary as we've either turned on RED in SETUP
// or in the previous loop ... regardless, this turns RED off
// analogWrite(RED, 0);
// delay(1000);
for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
redValue -= 1;
greenValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(RED, 255 - redValue);
// analogWrite(GREEN, 255 - greenValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(delayTime);
}
redValue = 0;
greenValue = 255;
blueValue = 0;
for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
{
greenValue -= 1;
blueValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(GREEN, 255 - greenValue);
// analogWrite(BLUE, 255 - blueValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(delayTime);
}
redValue = 0;
greenValue = 0;
blueValue = 255;
for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
{
// The following code has been rearranged to match the other two similar sections
blueValue -= 1;
redValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(BLUE, 255 - blueValue);
// analogWrite(RED, 255 - redValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
delay(delayTime);
}
}


As you can see, there's quite a bit going on, but it's fairly digestible. You'll need to define your pins with: 

// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6


The void output section writes the setup function, and here specifically, this means defining the three pins which we're using as outputs:

void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}


Next, the define variables specify brightness:

redValue = 0;
greenValue = 0;
blueValue = 255;

Zero means off while 255 mandates max brightness.

How to Blink an LED With an Arduino: Final Thoughts

Overall, blinking an LED with an Arduino is a simple project but it's a fantastic means of getting hands-on with Arduino hardware and software. While it may seem limited, there's ample room for experimentation. Try blinking single-color LEDs, an RGB LED, or tweaking code so as to change pins, brightness, and delay between blink time. The blinking LED provides a tangible, gratifying sight. 

Your turn: Which Arduino projects for beginners are your favorites?

Leave your feedback...