Text editor powered by tinymce.
Arduino Lesson 1. Blink
Overview
Text editor powered by tinymce.
However, the Arduino also has a single LED that you can control from your sketches. This LED is built onto the Arduino board and is often referred to as the 'L' LED as this is how it is labelled on the board.
The position of this LED is circled in red on the pictures of the Arduino Uno and Leonardo below.
Text editor powered by tinymce.
In this lesson, we will reprogram the Arduino with our own Blink sketch and then change the rate at which it blinks.
In Lesson 0, you setup your Arduino IDE and made sure that you could find the right serial port for it to connect to your Arduino board. The time has now come to put that connection to the test and program your Arduino board.
The Arduino IDE includes a large collection of example sketches that you can load up and use. This includes an example sketch for making the 'L' LED blink.
Load the 'Blink' sketch that you will find in the IDE's menu system under File → Examples → 01.Basics
Text editor powered by tinymce.
We are going to change this sketch, so, the first thing you need to do is save your own copy that you can change however you like.
From the File menu on the Arduino IDE select the option 'Save As..' and then save the sketch with the name 'MyBlink'.
Text editor powered by tinymce.
The Arduino IDE will show you the current settings for board at the bottom of the window.
If you get this, go back to Lesson 0 and check your installation.
Once the upload has completed, the board should restart and start blinking.
Text editor powered by tinymce.
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Everything between /* and */ at the top of the sketch is a block comment, that explains what the sketch is for.
There are also single line comments that start with // and everything up intil the end of the line counts as being a comment.
The first actual line of code is:
Next, we have the 'setup' function. Again, as the comment says, this is run when the reset button is pressed. It is also run whenever the board resets for any reason, such as power first being applied to it, or after a sketch has been uploaded.
void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }
void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }
Every Arduino sketch must have a 'setup' function, and the part of it where you might want to add instructions of your own is between the { and the }.
In this case, there is just one command there, which, as the comment states tells the Arduino board that we are going to use the LED pin as an output.
It is also mandatory for a sketch to have a 'loop' function. Unlike the 'setup' function that only runs once, after a reset, the 'loop' function will, after it has finished running its commands, immediately start again.
void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Text editor powered by tinymce.
Upload the sketch again and you should see the LED start to flash more quickly.
Text editor powered by tinymce.
About the Author.
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino, 30 Arduino Projects for the Evil Genius and Programming the Raspberry Pi.
Text editor powered by tinymce.