Skip to content

Simple LED output

The Arduino Uno has an orange LED which is connected to digital pin 13. You can control this LED by sending a signal to the pin. This is the simplest exercise you can do because it only requires the Arduino board itself and no additional components. You can load the sketch into the Arduino IDE from the File menu: File → Examples → 01.Basics → Blink

Note that a digital pin can function as an input or an output. As the software developer, you must ensure that the very important task of hardware initialisation is performed at the start of your code. Here you say which pins you are using as inputs and which as outputs. This is the job of the setup() function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void setup() {
    // initialize digital pin LED_BUILTIN as an output.
    pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
    digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000);                     // wait for a second
    digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
    delay(1000);                     // wait for a second
}

Make sure your Arduino is connected and that you have selected the correct port.

Then, open the sketch and upload it to the Arduino.

You should see the onboard orange LED repeatedly coming on for one second and going off for one second.

Simple changes

See if you can do the following:

  • Change the blink rate to once every two second (time the blinks to make sure)
  • Change the sketch to send SOS in Morse code (∙∙∙ −−− ∙∙∙). Leave a long pause between each message.

Add some hardware

The onboard LED is connected internally to digital pin 13. If you also add an external LED on the same pin it will also blink at the same time with no changes to the sketch.

NB. Before making any hardware changes you should disconnect the Arduino from the computer.

Look at the circuit below, run the simulation and then reproduce the connections using your own Arduino and breadboard. If this is the first time you have used an LED, please see the component information page

Further reading

Original tutorials