Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Tutorial 3: Starting with Arduino

Created on: 31 July 2012

In this tutorial, you will first load a program to the Arduino Uno board that will flash 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 to flash the external LED on and off.

You will learn:

  • How to set up and program the Arduino.
  • How to interface (connect) an LED to the Arduino.

This video shows what you will achieve:

Can't see the video? View on YouTube →

Prerequisites

First complete tutorial 1 before attempting this tutorial. Although not essential, it is also recommended to complete tutorial 2.

Components

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 coloured and sized LEDs could also be used, e.g. 3mm green LED Semiconductors

You will also need:

  1. A breadboard
  2. Breadboard wire links
  3. An Arduino Uno board
  4. Standard USB cable
Parts needed for this tutorial
Parts Needed for this Starting with Arduino Tutorial

Installing the Arduino Software

You basically just need to download and install the Arduino IDE software as described below, but rather refer to the step-by-step installation instructions on the Getting Started with Arduino web page - click the Windows, Mac or Linux link on this page.

Windows: Download the latest version of the Arduino IDE from the Download the Arduino Software web page (click the Windows link). Unzip the downloaded file and extract the contents to a folder in a convenient location – e.g. to your desktop. Install the drivers as described on the Windows getting started page. To run the Arduino IDE, open the folder and double-click the arduino icon.

Ubuntu Linux: Install the Arduino IDE from the Ubuntu Software Center. For other Linux distributions and information on installing the latest version, see the Linux link on the Getting Started with Arduino web page. If you have installed an older version of the IDE from the Ubuntu Software Center, then all of the dependencies will be installed. To use the newest version of the IDE, download the Linux edition from the Download the Arduino Software web page. Unzip the downloaded file and extract the contents to a folder on your desktop or home folder. To run the IDE, open the folder and double-click the arduino icon. Click the "Run" button in the dialog box that pops up.

Programming the Arduino

We will initially program the Arduino board with a program that is built into the Arduino IDE. This program will flash the on-board LED on and off.

Plug in the Arduino Uno board and open the program to load as follows:

  1. Plug the USB cable into the Arduino USB connector and the other end into a spare USB port on the PC that you loaded the Arduino IDE to.
  2. Start the Arduino IDE.
  3. Make sure that you have selected the correct board and serial port as described in the in the installation instructions.
  4. On the top menu bar, click File → Examples → 1.Basics → Blink
  5. A new window will open showing some program code as shown below.
Arduino IDE with the Blink program open
Arduino IDE with the Blink Program Open

Load the program into the Arduino:

  1. Click the Upload button (shown in red below) on the top toolbar to load the program to the Arduino.
  2. The program should load and then start running - you will see the LED marked 'L' on the board start to flash on and off.
Arduino IDE upload button
The Arduino IDE Upload Button

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual

Circuit Diagram

We will now interface an external LED to the Arduino board.

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).

Circuit diagram: LED interfaced to the Arduino
Connecting a Single LED to the Arduino

Building the Circuit

Bend the longer lead of the LED out and down and then plug the LED into the breadboard.

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.

Your circuit should now look something like this:

LED interface circuit on breadboard
LED Connected to the Arduino on Breadboard

Loading a New Program

The new program 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.

Start the Arduino IDE and either type the following program (from the screen-capture below) into the IDE and save it, or select the code below and then copy it and paste it into the IDE.

LED2 blink program
The LED2_blink Program

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);            // keeep LED off for 1s
}

Verify the Program

In the Arduino IDE, save the program that you typed or copied and pasted.

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.

Upload the Program

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.

As an Amazon Associate I earn from qualifying purchases: