Arduino Quick Start

Set-up

Controlling a single LED to make it flash or display at various levels of brightness is the standard introduction to Arduino, so that’s where we’ll begin with this quick-start tutorial.

Arduino Unos have a built in LED that can be used for testing, but for this tutorial, we are going to wire an LED up on a breadboard (see Figure 1 below).

  • Connect a 220 ohm resistor (red red brown) to pin number 11 on your Arduino.
  • Connect the other leg of the resistor to the positive leg of an LED.
  • Connect the negative leg of the LED to a ground (GND) pin on the Arduino to complete the circuit.

Image of Arduino circuit

The resistor is essential here because LEDs have very low resistance, and will thus attempt to draw too much current risking damage to either the Arduino or the LED. A typical 5mm red LED has an upper current rating of about 20mA. The pins on the Arduino can deliver a maximum of 40mA before damage will occur. To calculate the size of resistor that is needed to protect the LED and the Arduino, we can apply Ohms Law. LEDs have a built-in voltage drop however, so we’ll need to account for this in our math. Using the following assumptions we can solve for the required resistor:

  • The output voltage of the Arduino pins when set HIGH is 5V.
  • The voltage drop across the LED will be around 2V.
  • The voltage drop across the resistor will thus be 3V.
  • We want our current draw to be just under 20mA.

Using V = I * R, we can solve for R:

R = V / IR
R = 3V / 0.02A
R = 150 ohms.

The smallest safe value for R is 150 ohms. A 220 ohm resistor is a commonly found part, and gives us a little wiggle room, so it’s a good choice here.

Now that the circuit is set up, connect the Arduino’s USB port to your computer and launch the Arduino application.

Don’t worry if the LED lights immediately, or begins to flash, The Arduino will run whatever code was last installed. It wont do any harm here.

Above we chose pin 11 on the Arduino as the control pin for the LED, we will control the brightness of the LED by changing the value of this pin in code that we send to the Arduino.

Flashing an LED

Turning an LED on or off is as simple as setting the corresponding Arduino pin HIGH or LOW. These terms warrant more discussion, but for now consider them as approximately five volts and zero volts DC (I.E. power or no power). The command to set the LED pin HIGH is:

digitalWrite(11, HIGH);
Note that instructions in Arduino code are always followed by a semi-colon.

Similarly setting the pin to LOW is:

digitalWrite(11, LOW);

Above we are explicitly referencing the pin number in the code. This is not good practice though, as in complex code handling many different input/output pins, it will be hard to keep track of the purpose of each pin. Instead it is a good idea to give the pin a label at the start of your code. To do this we define a constant as such:

const int LED_PIN = 11;

We can now use this value when setting the pin state:

digitalWrite(LED_PIN, HIGH);

Before we can use a pin we need to tell the Arduino how we intend for it to be used. In our case we want a digital (on or off) output pin. The Arduino provides a special function called setup() for your program to do any preparation work it may wish to do. We can use the setup() function to configure pin 11.

const int LED_PIN = 11;

void setup() {
    pinMode(LED_PIN, OUTPUT);
}

The code above is pretty simple. First we define a label to refer to pin 11 to keep the code more readable. We then define a function called setup() which will be called automatically by the Arduino when it first starts, or after a reset. In this function we call a built-in function called pinMode() to tell the Arduino that our LED pin will be used as an output pin. With this done we’re ready to build an actual Arduino application.

The main work of an Arduino program is performed in the loop() function, which as with setup() will be called automatically by the Arduino. In contrast to the setup() function however, the loop() function is called repeatedly, giving code a chance to react to the passing of time, or changing levels on input pins, typically by altering the value of output pins. In our case we wish to flash an LED, so our code will simply set the LED pin HIGH, wait a while, set it LOW, wait a while and then repeat. Expressed as a simple Arduino program, this translates to:

const int LED_PIN = 11;
void setup() {
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_PIN, HIGH);
    delay(500);
    digitalWrite(LED_PIN, LOW);
    delay(500);
}

The delay() function tells the Arduino to count off the specified number of milliseconds before moving to the next instruction. As such, the LED will light for half a second, and go dark for half a second, before repeating the process.

If you enter the above code into the Arduino code editor, and send it to your Arduino, you should see the LED begin to flash.

Dimming an LED

So far we’ve seen how to switch an LED on and off, but if we want to to vary the brightness we need to make a few changes to our code. First though, let’s take a moment to examine what it actually means to dim an LED. Typically LEDs perform at their best — giving the most accurate color, and consistent brightness — when the supply voltage and current are held within a fairly narrow operating range. As such, naively varying voltage or current in an attempt to adjust the brightness of the LED will yield unsatisfactory results. Instead we can use a persistence-of-vision trick to give the impression of differing levels of intensity. To do this, we turn the LED on and off at a rate that is faster than our eyes can detect. Typically this is anything over 20 times a second. In fact this trick is so commonly used that support for it is built into the Arduino. If you look at the labels for pins 3, 5, 6, 9, 10, and 11 on your Arduino, or in Figure 1 above, you will see that the pin number is prefixed by a tilde (~) which is intended to denote analogue output. Where a digital pin can only describe two states: on, or off, an analogue pin can describe 256 states. These states range from the value zero which equals off, through to 256 which equals on, with values one to 255 forming a gradient between those states. This is accomplished internally by a technique called Pulse Width Modulation or PWM.