Check the 34 other Arduino tutorials

#1 · Blinking Arduino's builtin LED


Tutorial goals

  • Blinking the onboard LED
  • Working with the Arduino IDE
  • Knowing how to upload a sketch to your Arduino

Components needed

  • 1× Arduino
  • 1× USB cable
  • 1× Computer

Introduction

When I bought my first Arduino there was no YouTube with fancy instruction videos. What I saw was: a printed circuit board with chips, pins, a button and LEDs. The only thing I could think of was to start as fast as possible by just trying. I skipped explanations of the basics because I wanted to get my LCD display working now, not after reading boring theory.

Soon I discovered that getting things to work was not as simple as it looked in the first place. A tough lesson was that I could even damage components when I wired things the wrong way. If you don't mind things being damaged, by all means go ahead and discover yourself 😄. If you want a little bit more guidance, please continue reading.

I could a start an endless story on electronics, bombarding you with circuit diagrams and stories about signals. That is exactly what Bas on Tech tends not to do. You find this information on many webpages with a click of the mouse. This is our first Arduino tutorial and therefore I have to explain some things to get you started. This will make it easier for you to understand the working of the circuit. Something which I prefer over just copy/pasting the Arduino code (can't wait? You can download the code with the course material button at the bottom of this page).

Course material

At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

What is an Arduino?

The Arduino is a so called microcontroller. It processes analog and digital signals and can react to these. The heart of the Arduino is the AVR-chip. What this chip has to do is written in a program written in Arduino code. Via an USB cable the compiled program could be uploaded to the Arduino. The possibilities are endless: mp3 player, mobile phone, robot, weather station, game computer, RC cards, home automation and much much more!

The design of the Arduino is open source. This means that the manufacturer is allowed to bring its own Arduino to market. There are lots of Chinese "Arduino clones" sold. These can do exactly the same as the original Arduino. Often cheaper components are being used which is not an advantage. Clones could need special drivers being installed, while the original is just plug and play.

In this tutorial I am using the Arduino Uno, but there are many more like the Nano, Pro Mini, Micro and Mega. Each one has its own pros and cons. This could be the amount of inputs / outputs, speed but als the form factor. In this video I show the differences between several Arduino boards.

How to progam with the Arduino IDE?

In this Arduino tutorial we're going to program the builtin LED. The tool we are going to use is the Arduino IDE which is freely available on the Arduino website

IDE is an abbreviation of Integrated Development Environment. A tool which converts your code into the bits and bytes which the Arduino understands. This process is called compiling. Besides compiling the IDE also checks if the code is correct. In the next tutorials you'll learn more about this.

Arduino CH340/CH341 drivers

Some non-original Arduinos might require a driver to be installed. This is because these boards are using the CH340/CH341 chip for USB communication with your computer. You'll find more information about this driver on the Sparkfun website.

Which pins does the Arduino have?

For this tutorial I use the Arduino UNO, which has many pins to connect components to. When reading the word pins you might expect solid metal pins. For the UNO this is not the case. The UNO uses female headers. Instead the Arduino Nano uses real pins. Since the hardware implementation can differ per Arduino the common name is pins.

Arduino UNO with large AVR-chip
Arduino UNO with large AVR-chip by Arduino.cc

At the top of the Arduino you'll see the digital pins. These can only have one of these two values: HIGH or LOW. When high, a small current flow through the pin. For low it does not. On a 5V Arduino, this current is 5V, for the 3.3V variant this is 3.3V.

The bottom right shows the analog pins, which has 1024 possible values: 0 to 1023. On the bottom left you'll see the current pins like 5V, 3.3V, ground GND and the reset pin RES.

The Arduino UNO is also equipped with some special pins. One of these is pin 13 (top right). This pin is connected with the builtin LED. This is a LED which you can program and is not being used by the Arduino. The builtin LED is marked L on the PCB.

In the picture of the Arduino UNO you see a large chip. This is the AVR-chip, the heart of the Arduino. You might see a smaller chip in the center of your Arduino. This is the SMD variant of the chip. It is a lot smaller and not easy to replace.

Arduino Code

Time to dive into the code 🤓 For this tutorial we are going to use off the shelf example code. In the op menu of the Arduino IDE you can choose:

File ▸ Examples ▸ 01. Basics ▸ Blink

The IDE should open the code to blink the builtin LED automatically. Take some time to read the code before you continue. Are you able to figure what each line does?

1 // the setup function runs once when you press reset 
2 // or power the board
3 void setup() {
4     // initialize digital pin LED_BUILTIN as an output.
5     pinMode(LED_BUILTIN, OUTPUT);
6 }
7 
8 // the loop function runs over and over again forever
9 void loop() {
10     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
11     delay(1000);                       // wait for a second
12     digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
13     delay(1000);                       // wait for a second
14 }

In the code you see all kinds of commands. These commands are written in a syntax that the computer understands. Misplacing a dot or comma could result in the computer being unreadable to read your code. No worries, as a software developer this where you run into many times and is part of the job.

Comments in the code

Line 1, 2 and 8 start with two slashes //. This line of code is what we call comment. Comments allow you to provide human readable additional information which is completely ignored by the computer. Another form of comment starts with /* and ends with */. Everything between the start and end will be seen as part of the comment e.g.:

1 /*
2     This is comment
3     over
4     multiple lines
5 */

You can use this syntax for a single line comments as well:

1 /* This is a single line comment */

The setup() function

The code has been split into two parts: setup and loop. We refer to these blocks as functions. In another tutorial we are going to dive deeper in how to make your own functions. The indenting is not mandatory, but helps to quickly see what lines belong to the function.

When the program starts it executes the setup() function once. After this the program continues with the loop(). The setup() is for example used to assign pins.

In this example we are using the pinMode function to specify we want to use the LED_BUILTIN pin as a OUTPUT. On the Arduino UNO, LED_BUILTIN is an alias for 13 (the builtin LED pin). Therefore you could have typed 13 as well. The advantage of using LED_BUILTIN is that it works on all Arduinos. Even when the builtin LED is connected to another pin.

The loop() function

After the setup(), the program continues with the loop(). This function will be called over and over again. Every time the function has finished, it will be called again. The only way to stop the program is to disconnect the power or to upload a new program.

If you look closely at the code you see two other functions being called: digitalWrite() and delay(). digitalWrite() writes the value (LOW or HIGH) specified to a given pin. The pin we are using is specified by the function's first parameter, in our case LED_BUILTIN. The second parameter specifies the written value, here HIGH. digitalWrite() in this case, makes sure that the LED on pin 13 will be lit.

The next line shows the delay() function with a single parameter. Can you think about what the value of this parameter represents?

Its value is the amount milliseconds the program has to wait. In our case this is 1000 milliseconds, which is the equivalent of 1 second.

After this brief pause, the program continues and writes LOW to the LED_BUILTIN pin. As a result the LED goes off.

Uploading code to the Arduino

Now our program is ready to upload to the Arduino. First we have to connect our Arduino to the computer with the USB cable. Make sure you've selected the correct board in the IDE:

Tools ▸ Board ▸ Arduino/Genuino UNO

and the correct port:

Tools ▸ Port

If you are not sure which port to use, try them all until you can successfully upload your code.

The top left shows a round button with a checkmark. By pressing this button you tell the IDE to verify your code for possible errors. The IDE only checks if it can read your code. It does not check if you have written correct code for what you are trying to program.

If everything works the IDE shows the Compiling completed message. You can now upload your code by pressing the round button with the arrow to the right. The uploading is complete when the Avrdude done. Thank you. messages appears. Your program will immediately start after uploading. As a result you should now see your Arduino LED blink with 1000ms intervals.

Challenge 🚀

Great to see you made it this far! Are you ready for a challenge?

Change the code so the LED tuns on, wait for 0.5 second, turn off the LED and wait for 2 seconds.

Good luck!

Bas van Dijk

About Bas on Tech


My name is Bas van Dijk, entrepreneur, software developer and maker. With Bas on Tech I share video tutorials with a wide variety of tech subjects i.e. Arduino and 3D printing.

Years ago, I bought my first Arduino with one goal: show text on an LCD as soon as possible. It took me many Google searches and digging through various resources, but I finally managed to make it work. I was over the moon by something as simple as an LCD with some text.

With Bas on Tech I want to share my knowledge so others can experience this happiness as well. I've chosen to make short, yet powerful YouTube videos with a the same structure and one subject per video. Each video is accompanied by the source code and a shopping list.