The site for those who love tinkering with Arduino, Raspberry Pi & Co.

Arduino Led: Beyond pin 13, let's learn how to use the other Arduino digital pins

In our first arduino project, we made a led flash by connecting one pin directly to an Arduino digital output and the other directly to the ground of the circuit (negative pole).

This is fine for the Arduino pin 13 feature: pin 13 is a pin with special features that Arduino offers us to get to our first results faster: it does not require configuration.

Arduino and Led, Introduction

Normally, however, especially when the LEDs to be activated are more than one, we must insert a resistor to correctly establish the current that must pass on our diode for correct lighting and avoid burning the LED and / or the Arduino ports (our pin 13 it is equipped with a resistance of 1 kOhm in series, as well as being coupled to a small LED on the board which lights up according to the state of the pin).

Arduino led pin 13
Pin13 has a led that shows its status integrated in the board

If we remove the led and start arduino our program will in fact work the same and we will be able to follow its operation (the flashing) on the led mounted on the board.

If the insertion is then done on other pins, which are not 13, it is necessary to insert a protection resistor otherwise, even if we may not burn the LED, we will drastically reduce its life span.

resistors, arduino led
Some of resistances

For further information on what resistances are and how they are used, I refer you to the link of another article on our site: The Resistance, while as regards the lighting of our LEDs we need to know some parameters that characterize them to know how to size correctly our resistor.

Arduino and led: pay attention to consumption

A lit LED diode consumes about 15 to 20 mA while the voltage drop across it varies depending on the color and type of LED diode, in the list we can see the voltage drop across the LEDs of the main colors:

  • red color: 1.8V
  • yellow color: 1.9V
  • green color: 2.0V
  • orange color: 2.0V
  • blue color: 3.0V
  • white color: 3.0V

Now that we know these values through Ohm's law we can calculate the correct size to choose for our resistor using the formula:

R = (5 – 1.8) / 0.02 = 160 Ohm

To understand this formula we must think that the voltage on the Arduino pin in the HIGH state is 5 volts, of these 5 volts 1.8 fall across our led then to get to ground (0 volts) another 3.2 (which are precisely 5 – 1.8 …) will have to fall at the ends of our resistance while maintaining a correct current passage to make our diode work (0.02 A ie 20 milliamps).

I hope I have not confused you … (in this case you can ask questions in the comments and I will be happy to answer them ed).

When we have to buy our resistor, however, we will not be able to ask for a 160 Ohm resistor as it is not possible to find it normally on the market, the value we can recover is 180 Ohms but resistances up to 1K will be fine anyway.

Arduino and led, let's get to work

To test our new knowledge we will create a small circuit that can have more than one LED, in the example we are going to see three were used, with respective three 220 Ohm resistors (these I had at home :)).

Arduino Led, arduino and led connection
Arduino circuit and three LEDs

The program we are going to run on this project will be a "slightly enhanced" version of the blink.ino that we used in the first project: instead of switching on and off a single LED, it must be able to switch on the three LEDs, one in sequence to the other.


/ * Blink 123 * /

int led1 = 11;
int led2 = 10;
int led3 = 9;

void setup () {
   pinMode (led1, OUTPUT);
   pinMode (led2, OUTPUT);
   pinMode (led3, OUTPUT);

void loop () {
   digitalWrite (led1, HIGH);
   digitalWrite (led3, LOW);
   delay (1000);
   digitalWrite (led2, HIGH);
   digitalWrite (led1, LOW);
   delay (1000);
   digitalWrite (led3, HIGH);
   digitalWrite (led2, LOW);
   delay (1000);

With the naked eye you will probably be able to see how each instruction has been repeated three times.

Unlike blink.ino we declare three variables (one for each led) and assign to these variables the number of the Arduino digital output pin that we will use to drive the led (11, 10 and 9 in my case).

In the setup function () we configure the three pins indicated above as OUTPUT (output) in order to turn on the LEDs.

The loop () function turns on a led and turns off the one previously lit, then waits a second as in blink.ino and repeats the same block of operations for the other two leds, and then repeats itself indefinitely by lighting the three leds consecutively .

At the first start of Arduino, you may have noticed that we are going to turn off a led that was not lit:

digitalWrite (led3, LOW);

It doesn't do much if we turn off something that is already off … off remains! However, this instruction will be useful in all the other cycles after the first one because it will turn off LED 3, that is the last one we turned on in the loop.

Here is the video of the running program:

I hope you liked it. Hello!

Arduino starter Kit
Arduino Starter Kit Ufficiale Per iniziare alla grande con Arduino

Ti è rimasta una domanda o un dubbio?

Iscriviti e chiedi nella community

Leave a Reply

Your email address will not be published. Required fields are marked *