Created on: 31 July 2012
Updated on: 13 January 2023
How to start using an Arduino for beginners in electronics. In this tutorial, you will first load a program, known as an Arduino sketch, to an Arduino Uno board. The sketch flashes or blinks the on-board LED on and off. You will then connect an external LED to the Arduino using a breadboard and load a new program or sketch to flash the external LED on and off.
You will learn:
This video shows what you will achieve:
First complete tutorial 1 before attempting this tutorial. Although not essential, it is also recommended to complete tutorial 2.
The following components are needed when connecting an external LED to the Arduino Uno board.
Qty | Part | Designator | Notes | Type |
---|---|---|---|---|
1 | 470 ohm resistor (yellow - violet - brown) | R1 | 1/4W, 5% or better | Resistors |
1 | 5mm red LED | D1 | Other colored and sized LEDs could also be used, e.g. 3mm green LED | Semiconductors |
You will also need:
You basically just need to download and install the Arduino IDE software for the operating system that you are using. To do this, refer to the Getting Started with Arduino web page – click the Windows, Mac or Linux link on this page, or find the direct links below.
Windows: Windows Arduino IDE installation page.
Linux: Linux Arduino IDE installation page.
Mac OS: MacOS Arduino IDE installation page.
We will initially program the Arduino board with a program that is built into the Arduino IDE. This program flashes the on-board LED on and off.
We will now interface an external LED to the Arduino board. Interface simply means to connect the LED to an Arduino pin. This is done by connecting an LED and series resistor between pin 2 of the Arduino Uno and an Arduino GND pin, as described next.
The circuit diagram below tells us to connect the anode of the LED to pin 2 of the Arduino board. Pin 2 is marked on the silk-screen of the board. The LED cathode connects to a resistor and the other lead of the resistor connects to one of the GND pins of the Arduino (the Arduino Uno has three pins marked as GND, use any one of them).
Build the Arduino LED circuit on a breadboard using the components described near the top of this page, and shown in the circuit diagram. This is a very simple circuit for beginners. Also refer to the images that follow when following the build instructions below.
Bend the longer lead of the LED out and down and then plug the LED into the breadboard across the middle insulating channel of the breadboard. See the images below.
Plug the 470 ohm resistor into the breadboard so that one of its leads connects to the cathode (shorter lead) of the LED. Connect the other lead of the resistor to one of the GND pins of the Arduino board using a long wire link.
Connect the anode (longer lead) of the LED to pin 2 of the Arduino using a long wire link.
The following images show two ways of building the same circuit on breadboard. One shows how the circuit was built that features in the video at the top of this page. The other circuit is wired according to the photo that follows.
Your circuit should now look something like this if you used the alternate wiring image above:
The new program that follows is an adaptation of the previous program. It has been modified to flash an LED on and off that is connected to pin 2 of the Arduino, as in our circuit diagram above.
In Arduino terminology, a software program is referred to as a sketch.
Start the Arduino IDE. If an existing sketch is open in the IDE, then select File → New Sketch from the top IDE menu. This opens a new blank sketch that contains a few basic lines of code. Either type the following program (from the screen-capture below, or from the listing that follows) into the IDE and save it, or select the code from the listing below and then copy it and paste it into the IDE. If copying and pasting, first remove the existing lines of code from the new sketch.
The LED2_blink Program: Copy the following Block of Code and Paste it into the Arduino IDE
void setup() { // set pin 2 as an output pinMode(2, OUTPUT); } void loop() { digitalWrite(2, HIGH); // switch LED on delay(1000); // keep LED on for 1s digitalWrite(2, LOW); // switch LED off delay(1000); // keep LED off for 1s }
After either typing or pasting the code into a new sketch in the Arduino IDE, save it using File → Save from the top IDE menu. Name the new sketch LED2_blink when prompted.
Click the Verify button on the top toolbar of the IDE to make sure that the program has no errors in it (the verify button is the first icon on the toolbar – the tick mark).
If the program has any errors, then make sure that you typed the program in exactly as shown. Make corrections, save and verify again.
Click the Upload button to load the program to the Arduino board.
After the upload completes, the external LED interfaced to pin 2 of the Arduino board should start to flash on and off just like the on-board LED did after loading the first program.