Blink - My First Arduino Project

In this project, you'll learn about the Arduino IDE programming environment and how to make a LED light blink.

Parts needed: Software stuff you'll learn: Arduino USB cable
1 If the Arduino IDE program has not been installed on your computer, you can click here for instructions on how to download and install the Arduino program. After installing the Arduino program and the driver, come back here and continue with step 2.
2 Double-click on the Arduino icon to start the Arduino program. Arduino icon
3 Create a new program by selecting File from the menu and then select New.

You will see this template.

You will be writing commands inside either the void setup() or the void loop() routines in between the two curly braces { and }.


Commands that you put inside the void setup() routine are executed only once on startup or on reset.

"Inside" means between the two curly braces { and }. You always write commands inside of the curly braces.


Commands that you put inside the void loop() routine are executed repeatedly forever (or until you turn off the power)


Lines starting with the two slashes // are comments, and they are ignored by the computer. You write comments to tell people what the program is about and what it is supposed to do. It is a good habit to write good comments. But the computer doesn't care what you write and it'll just throw the comments away.

Bare minimum
4 There is a built-in light (LED) on the Arduino board that is connected internally to digital I/O pin 13. Arduino digital I/O pins and built-in LED
5 To control this LED we need to issue two commands to the Arduino:







First, we use the pinMode command to tell the Arduino that we will be controlling the LED that is connected to pin 13 for OUTPUT. The first parameter value inside the parentheses of this command is the pin number 13, and the second parameter is the word OUTPUT.

Second, we use the digitalWrite command to send a HIGH signal to pin 13. HIGH means to turn on the LED. The first parameter inside the parentheses is again the pin number 13, and the second parameter is the word HIGH.

Type in these two commands inside the void setup() routine as shown. Commands are inside the void setup() routine if they are placed between the open and close braces {  }. Do not put commands outside of the braces.

A semicolon is placed after each command line; in this case the pinMode and digitalWrite commands. Always remember to do this.
commands to turn on LED
6 Connect your Arduino board to your computer using the USB cable. USB cable
7 From the Arduino program menu, select Tools. On the Mac, the Tools menu is at the top left corner of the screen.

For Windows
  • Under the Tools menu for the Board select the Arduino Uno.
  • For the Port select the COM port that your Arduino is connected to. If there are more than one listed then it usually is the one with the largest number.

    (Another way to find out what COM port to use, first unplug the USB cable and note the COM ports listed. Then plug the USB cable back in and compare this list of COM ports with the previous list. The extra COM port that is in the second list but not in the first list is the one to use.)

    If you don't see the COM port then make sure that you have plugged in the USB cable tightly. If you still don't see the port then make sure that you have installed the device driver. Follow these instructions to install the driver.
selecting COM port for the PC
For Macs
  • Under the Tools menu for the Board select the Arduino/Genuino Uno.
  • For the Port select the one that your Arduino is connected to. The port will have the word usbserial as part of the name.

    If you don't see the COM port then make sure that you have plugged in the USB cable tightly. If you still don't see the port then make sure that you have installed the device driver. Here's the device driver for Macs.
selecting COM port for the Mac
8 Click the Upload button (with the arrow pointing to the right) to upload the program onto the Arduino board. Upload icon

If the upload is successful you should see the words Done uploading at the bottom, and the Arduino will automatically run your program.

Done uploading

If you have selected the wrong COM port or if there are errors in your program, you will see an orange error message at the bottom. You'll have to correct ALL the error(s) and then upload your program again. Go back to step 5 and double check your work.

COM port error
9 You should see this LED on the Arduino board blink several times and then stays on.

If you missed that or if you want to see it again then press the red reset button on the Arduino board to run the program again.

After uploading a program or after pressing the red reset button, this LED will always blink several times at the beginning. This is caused by the initialization sequence and not from executing the commands in your program. You always ignore this initial sequence of blinks. What we are interested in is that this LED stays on afterwards because it executed the digitalWrite command where we tell it to turn on the LED.
10 If we want to turn off the LED after turning it on, we add another digitalWrite command after the first one, but this time we send a LOW signal instead. LOW means to turn off the LED. commands to turn on and off LED
11 Click the Upload button to upload the program again.

Remember that you always need to press the Upload button everytime after you make some changes to your program.

After the initial blinking sequence you should see the LED turn on very quickly and then turns off.

Again, if you missed that or if you want to see it again then press the red reset button on the Arduino board to run the program again.

The Arduino executes the commands very fast that's why the LED turned on just for a brief second from executing the first digitalWrite command and then it is turned off from executing the second digitalWrite command.
12 To keep the LED on a little longer so that you can see it, we need to use the delay command to tell the Arduino to wait a bit before continuing.









The number specified in the delay command is the number of milliseconds (ms) to wait.

1000 ms = 1 sec

So to wait for 1 second, we put the number 1000 inside the delay command.
delay
13 Upload the program again. What do you see? --- The LED turns on for 1 second and then it turns off.
14 To make the LED blink continuously, we need to tell the Arduino to keep repeating this on and off command sequence.













To do that, you move the two digitalWrite commands and the delay command from the void setup() routine to the void loop() routine. Remember that commands inside the void loop() routine are executed repeatedly forever.
half blink
15 Upload the program again. What do you see? --- The LED just stays on. Why?

Because there's no delay after the digitalWrite LOW command and so the program goes back right away to the top of the loop and executes the digitalWrite HIGH command again.
16 Solution, we need another delay command after turning off the LED before it repeats the loop to turn the LED on again as shown in this final code. blink
17 Upload the program again. This time you'll see the LED blink on for 1 second and then off for 1 second.
18 If you like you can save your program by selecting File from the menu and then Save. Type in the name blink for this program.


Congratulations! You've completed your first Arduino project to make a LED blink!

Now click on the Experiments button below to test yourself.