Introduction

In this first tutorial, we will be showing you how to set up a basic circuit using an LED. By the end of this tutorial you will have an understanding of how circuits work and will be familiar with the arduino’s programming environment.


Components you need

  • Arduino UNO or similar
  • Breadboard
  • Jumper Wires
  • LED
  • Resistor

Components

Breadboard

A breadboard is a tool that allows for easier and more organised connections of electric components. It is organised by rows and columns.

The two blue and red color-coded rows being the power connection rows. All holes in a row are connected.

Columns are marked by numbers. All holes in one column are connected.


LED

LEDs are small and powerfull lights.

LEDs have two legs: short (negative side) and long (positive side). They have to be connected in the right direction on the breadboard.


Resistor

An LED can only accept a range of voltages, and supplying it with a higher voltage than allowed will cause it to burn out. This is why we need to use a resistor that limits the current output.

Resistors need their legs bent in right angles to fit in the breadboard. One can also cut the legs shorter.


Jumper Wire

Jumper wires are used to connect components on the breadboard and to connect them to Arduino.


Arduino

Arduino is a platform, which allows to control electronic components through a microcontroller. The board can be connected and programmed a USB connection on a computer.

Connecting step-by-step

Step 1

Connect the LED to the breadboard. The two legs should be in different columns. Note where you insert the longer and shorter legs.


Step 2

Insert one leg of the resistor in the same row as the short (negative) leg of the LED.

Insert the other leg of the resistor into a free column.


Step 3

Connect the ground (GND) power pin of the Arduino to the column with the only resistor leg using a jumpwire.


Step 4

Connect the arduino’s digital pin 13 to the same column as the LED’s positive leg (long leg).

The number of the pin we connect it to is important as we will have to set it as a power output in our program.


Step 5

Connect one side of the USB cable to the Arduino and the other one to your computer.

The final circuit should look similar to the diagram below:

Code

Setup the environment

First, download and install the Arduino programming environment.

Guides:

Arduino IDE Download page


Create new sketch

To create a new program, start a new sketch in the Arduino IDE.


Code structure

The first thing you will see are the two main loops that you will need for every program:

  • Setup function: This is where all the initial parameters are set for your program. For example modes for different pins, initialising variables. The setup function runs only once when you power the arduino on or when you reset it.
  • Loop function: This is where all of your main code goes. As the name states, the function will run continuously.

Blinking LED

Write the code for blinking the LED in Arduino IDE.

This is the code for a blinking LED:




				

Code explanation

Set the LED pin

#define LED_PIN 13

We connected the LED to pin 13. We are going to use the pin number later in the code.

We don't want to change the pin number in each part of the code and want to understand what 13 means.

The compiler will replace LED_PIN with 13 everywhere in the code.

Configure LED pin as output

pinMode(LED_PIN, OUTPUT);

The pinMode(pin, mode) function sets a specific pin number in specific mode: INPUT or OUTPUT.

A button could use a INPUT mode and a LED could use OUTPUT mode.

Turn LED on or off

digitalWrite(LED_PIN, HIGH);

The digitalWrite(pin, mode) function sets a specific pin number to a specific voltage: HIGH (5V) or LOW (0V).

  • HIGH will turn the LED on.
  • LOW will turn the LED off.

Delay

delay(1000);

The delay(ms) pauses the program for a specific amount of time in milliseconds.

Without this statement the program would jump to next line and then to next loop (almost) instantly, so we would not see the LED blink.


Check connection

Make sure you connected the Arduino to your computer via USB.

Then choose the right Arduino board type and the correct port if they were not selected automatically.


Upload to Arduino

Click the upload button (right arrow, second button from the left).

The program will compile and upload to Arduino. A 'Done uploading' message will appear.

If something went wrong, you will get an error message, which you can use to figure out what went wrong by using Google.


Exercises

Exercise 1

Add a second LED.

Exercise 2

Add a few more LEDs. Then turn on/off LEDs in a row.

After the first LED is turned off the second should turn on then off. Then the same with the 3rd.

Exercise 3

Fade a LED. The LED should slowly turn on and off.

Find out about analog pins and analogWrite()


Challenge

Create a traffic light with 3 LEDs. Think about each possible cycle of the 3 lights and their order.

Extra: Move to next cycle by pressing a button. Find out about using buttons with Arduino.

Next Lesson