Time for your first program! Your Arduino UNO has several small LEDs – you will now repeatedly turn one of them on and off, making it blink.
First, open the Arduino IDE and create an empty sketch. To do this, select “New sketch” from the “File” menu. Now you will see the two empty functions void setup()
and void loop()
.
The Setup Function
Let’s start with the setup function. Enter the following line of code between the two curly braces { }:
pinMode(LED_BUILTIN, OUTPUT);
The setup function should then look like this:
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
}
Let’s take a closer look at this line. First, there’s another function here: pinMode()
. This function also performs a specific action – it sets a direction or mode for a specific pin on your Arduino: either sending a signal (OUTPUT) or receiving a signal (INPUT).
Unlike the setup and loop functions, this function “expects” something between the round brackets ( ), namely so-called parameters.
The first parameter determines which pin’s direction should be set. In our case, this isn’t an analog or digital pin on the sides of the Arduino, but the internal LED. This is referred to in the sketch as LED_BUILTIN
.
The second parameter then determines the direction – either OUTPUT or INPUT. If you want to control an LED, the Arduino needs to send appropriate signals to it. From the Arduino’s perspective, this is an OUTPUT. However, if a sensor sends measurement data to the Arduino, this is an incoming signal – so it’s an INPUT.
Note: Make sure that every line that doesn’t end with an opening or closing bracket ends with a semicolon (;). Otherwise, your sketch cannot be uploaded. There are exceptions to this rule, but they aren’t important right now.
You only need to set the direction of the pin once at the beginning of your program. That’s why the pinMode()
function is in the setup function.
The Loop Function
Now let’s move on to the loop. In the previous lesson, you learned that the code within the loop constantly repeats. We can use this to make the LED blink.
Actually, blinking is nothing more than 1) turning the light on and 2) turning the light off – and then starting over:
Turning the LED on
First, enter the following line in your loop function between the curly braces { }:
digitalWrite(LED_BUILTIN, HIGH);
This is another function, digitalWrite()
. This function can send either a HIGH or LOW signal via a digital pin. HIGH means something like “on” or 1 – LOW, on the other hand, stands for “off” or 0.
Using an LED as an example, this is easy to understand: HIGH turns the LED on, LOW turns it off.
The digitalWrite()
function also expects two parameters: the pin it should control and the signal. In our case, that’s the LED_BUILTIN
pin and initially the “Turn on” signal – so HIGH.
You now have a line to turn on the LED. But that’s not proper blinking yet.
Wait a moment
An Arduino is amazingly fast. If you turned the LED on and immediately turned it off again, you wouldn’t notice any blinking. The change from on and off would be so fast that it would look like the LED was constantly lit.
So we need to wait a moment.
In the sketch, this works with the – yes, correct – delay()
function. This function delays the further execution of your program. For this, it expects a single parameter, namely the duration of the delay – in milliseconds.
Enter the following line next in the loop function:
delay(1000);
This delays further execution by 1,000 milliseconds, which is exactly one second. The delay ensures that the LED, which you turned on in the previous line, stays on for one second. Only then is the next line executed.
Turn the LED off and wait again
In the next two lines of code, you reverse the process – first, you turn the LED off and make sure it stays that way for a second:
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
You already know the digitalWrite()
function. This time, however, it sends the LOW signal to the internal LED (LED_BUILTIN
). So it turns it off – also for one second.
The complete loop function then looks like this:
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
Start all over again
Now you have everything you need for your blinking LED: You turn the light on briefly and turn it off again briefly. The loop function takes care of the rest, namely the repetition, all by itself.
As soon as your program has completed delay(1000);
for the second time, it has reached the end of the loop and jumps back to its beginning – to the line digitalWrite(LED_BUILTIN, HIGH);
Have you already loaded the sketch from the downloads for this lesson onto your Arduino? If not, try it out right away! Afterward, play around a bit with the parameters in the delay function and change them. Maybe you can even practice Morse code by making the LED light up for different lengths of time.
You can find the complete sketch in the Exercise Files tab above.