It may be that your Arduino was already blinking when you first plugged it in. That is because the Arduino is often shipped with the Blink sketch installed.

If this is the case, then you might like to prove to yourself that you have actually done something by changing the blink rate. Let’s look at the Blink sketch to see how to change it to make the LED blink faster.

The first part of the sketch is just a comment telling you what the sketch is supposed to do. A comment is not actual program code. Part of the preparation for the code being uploaded is for all such “comments” to be stripped out. Anything between /* and */ is ignored by the computer, but should be readable by humans.

/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
… text deleted for brevity
*/

Then, there are individual line comments, just like the block comments, except they start with //.

The next part of the sketch is the setup function. Every Arduino sketch must have a setup function, and this function runs every time the Arduino is reset, either because (as the comment says) the Reset button is pressed or the Arduino is powered up.

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

The structure of this text is a little confusing if you are new to programming. A function is a section of code that has been given a name (in this case, the name is setup). For now, just use the previous text as a template and know that you must start your sketch with the first line void setup() { and then enter the commands that you want to issue, each on a line ending with a semicolon (;). The end of the function is marked with a } symbol.

In this case, the only command Arduino will issue is the pinMode(LED_BUILTIN, OUTPUT) command that, not unsurprisingly, sets that pin to be an output.

Next comes the juicy part of the sketch, the loop function.

Note that LED_BUILTIN is a recent change to Arduino that identifies the LED built in to an Arduino board. Unfortunately, this does not work for all Arduino and compatible boards. So, if the Blink example doesn’t work for your board, try changing all occurrences of LED_BUILTIN to 13 (the built-in LED on an Arduino Uno).

Like the setup function, every Arduino sketch has to have a loop function. Unlike setup, which only runs once after a reset, the loop function runs continuously. That is, as soon as all its instructions have been run, it starts again.

In the loop function, you turn on the LED by issuing the digitalWrite(LED_BUILTIN, HIGH) instruction. You then set the sketch to pause for a second by using the command delay(1000). The value 1000 is for 1000 milliseconds or 1 second. You then turn the LED off and delay for another second before the whole process starts over.

// the loop routine 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
}

To modify this sketch to make the LED blink faster, change both occur- rences of 1000 to be 200. These changes are both in the loop function, so your function should now look like this:

void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(200); // wait for a second
}

If you try and save the sketch before uploading it, the Arduino IDE reminds you that it is a “read-only” example sketch, but it will offer you the option to save it as a copy, which you can then modify to your heart’s content.

You do not have to do this; you can just upload the sketch unsaved. But if you do decide to save this or any other sketch, you will find that it then appears in the File | Sketchbook menu on the Arduino IDE. So, either way, click the Upload button again, and when the uploading is complete, the Arduino resets itself and the LED should start to blink much faster.

Leave a Reply