Basic LED Flash

This tutorial will detail how to create an LED light which will flash every second.

Initializing inputs/outputs:

Before we start, we have to tell the Arduino which pins to pins to make an output. To do this, we use a function called pinMode(). pinMode() has 2 elements; the pin you want to affect, and then a statement stating the mode of the pin. For example, let’s say we want to make pin 2 an output, then we would write:

pinMode(2, OUTPUT);

in void setup(). Now that you know how to initialize outputs. To do this, we use digitalWrite(). digitalWrite() has 2 elements, the first one tells the circuit what pin to look at, the second element will determine if it send voltage out(HIGH) or not(LOW). For example, if pin 2 was in output, then the line

digitalWrite(2, HIGH);

would send voltage out of pin 2.

Delay Function:

Now lets explain the delay() function. This function (like it implies) delays the next part of the code for the amount of time in the brackets. The amount in the brackets is measured in milliseconds. So the following line:

delay(1000)

would make the program wait 1 second before executing the rest of the code.

Getting Started:

Now let’s start wiring the code. In void setup, make pin 13 an output. Then in void loop, send voltage out of pin 13, wait 1 second, then stop sending voltage out of pin 13. Put a delay of 1 second after you stop sending voltage out of pin 13. Your code should look like this:

void setup() {
 pinMode(13, OUTPUT);
}

void loop() {
 digitalWrite(13, HIGH);
 delay(1000);
 digitalWrite(13, LOW);
 delay(1000);
}
   

This program would make the LED flash, since once voltage is sent to pin 13, after 1 second passes voltage is stopped being sent. Then after 1 second this process repeats, making the voltage turn on and off every second.

Previous Tutorial Next Tutorial