The first program you usually write when learning a new programming language is called “Hello World”. The program outputs those words as its only function. When learning to program micro-controllers such as the Arduino, the equivalent of “Hello World” is a program that blinks an LED. Guess what it is called – Blink.
You Will Need:
1 LED (any color works fine)
A 220 Ohm Resistor
An alligator clip (not essential but makes the circuit easier)
Fourteen small and smooth rocks from the a western pacific island (not essential but adds an esoteric feel)
NOTE: On most Arduino boards there is an LED soldered right by pin 13 – it is actually connected to pin 13 – so if you do not have an LED laying around (or a resistor for that matter), you can use the board mounted LED – it will blink with the same sketch.
Insert the short leg of the LED into the pin labeled GND on your Arduino.
Connect the 220 Ohm resistor to pin 13 on the Arduino. It doesn’t matter which way you connect the resistor.
Now use the alligator clip to connect the long leg of the LED to the other leg of the resistor. If you do not have an alligator clip, you can twist the two leads together as best as you can to get a steady electrical connection.
Plug your Arduino board into your computer with a USB cable.
Open up the Arduino IDE.
Go to File, Examples , 01.Basics, Blink
Click the verify button on the top left. It should turn orange and then back to blue.
Click the upload button. It will also turn orange and then blue once the sketch has finished uploading to your Arduino board.
Now monitor you Arduino board – the LED should be blinking.
Make sure to read the comments at the top. Note the use of the multi-line comments syntax /* */. It is always a good idea to take time and see what the programmer has to say about the sketch they have written. She will likely be terse about how the program works or what it is supposed to accomplish. She might tell you how to set up the circuit.
The first block of code you come to after the description is where they initialize and declare variables. Here there is only one variable – and it gets declared and initialized on the same line.
int led = 13; //declare and initialize a variable with a single statement
You should be familiar with this from the last tutorial on variables. All we have done is made a variable and named it “led”. This variable will hold integers and we put the value 13 into the variable. If this seems confusing, it might not hurt to review the last tutorial on variables – or you can voyage on – your call.
The next block of code you encounter in the Blink sketch is…
// initialize the digital pin as an output.
Recall that the setup() function will be in almost every Arduino sketch that you encounter. Inside the curly braces is code that will only be run once by your Arduino. For this program we see the function pinMode() is being used.
Let me start by saying that pinMode() is a wonderful function. If you recall, functions can take arguments. The pinMode() function takes two arguments – it wants to know which pin you are going to assign a mode to, and what mode you want that pin to be. The pin number is easy, 0-13 for any of the digital pins, and A0 through A5 for any of the analog pins.
The mode is an easy designation also – you are going to want the pin to be an INPUT – good for reading a sensor. Or an OUTPUT – good for powering an LED.
So in this example, we set the mode of pin 13 as an OUTPUT, because we want to light up an LED.
Moving on to the final block of code, we come to out favorite and ubiquitous function 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