Today we will learn one of the most basic concepts in the Arduino world, digital outputs, and how to apply that concept on a very cool LED.

Final Project

Hardware Components

Component NameQuantity
Arduino Uno1
5mm LED1
330ohm Resistor 1
Jumpers Male-male2
Breadbaord1

Introduction


Today we will cover the very first concept that any Arduino beginner must have to know, we will talk about Arduino digital output pins, what does digital output signal mean? and lastly, we will apply those concepts to a very cool LED blinking application.

LED! What’s it?


LED stands for “Light Emitting Diode” which is a very easy-to-use component, it’ has two terminals, lights when a specific voltage is applied across its terminals. you can use the LED as a light indicator that lights when some event happens and more

LED
LED

Today we will use the most common 5mm LED, there’s a lot of LED sizes like the 3mm, 5mm, 10mm this number refers to the diameter of the LED, also it has many colors like red, green, yellow, blue,…

LED polarity
LED polarity

The LED is polarized, which means that it requires a specific way of wiring to work properly.

  • Long terminal: is the positive lead, should get connected to the power source positive lead
  • short terminal: is the negative lead, should get connected to the power source negative lead
Take Care!
Any LED has a positive and negative terminal and will not light if you wired it in the wrong way around with a power source.

The Current Limiting Resistor


Resistors all its job in life is to resist the flow of electrons “current” in the circuit, you can tell that from its name.

the higher the value of the resistor the less current will flow through it, and vice versa.

Take Care!
Actually, we can’t connect the LED directly with a power source like a battery that will cause some money losses because you will damage your poor LED.

The LED resistance is very low. which will cause a large amount of current to flow through it causes the LED to heat up very quickly and it will get damaged after few seconds.

To solve this problem we are using a current limiting resistor in parallel with the LED to limit or ‘choke’ the current flowing through it.

the LED is actually a simple diode which after a certain amount of voltage drop on its terminals will allow all the available current (unlimited) to flow through it which is not a good idea at all. Here comes the current limiting resistor job is to choke that current flow.

There is a simple formula V = IR, where V is voltage, I is current, R is resistance. this formula is called ohm’s law. We will use this formula to get out the right resistor value, but to get the right resistor value we need to know the right V and I value to put it in the formula to get the right resistor value. To get the V value we need to know two things, the voltage source and the forward voltage (voltage drop) on the LED.

LED & current limiting resistor circuit
LED & current limiting resistor circuit

So to Let’s say that we are using a 5V power source with a red LED connected in series with it. LEDs have a characteristic called forward voltage “Vf” which means the amount of voltage that got lost across the LED terminals. when operated at a certain current which is usually 2omA. Vf primarily depends on the LED color, for example, red, orange, the yellow LED Vf is around 1.8V while green, blue, white, and UV LEDs have a Vf of about 3.3 V. So, the forward voltage “Vf” on our red LED is 1.8V.

so by subtracting the LED forward voltage “Vf”(1.8V) from the power source voltage(5V) you will get the voltage drop across the resistor that we need to calculate its resistance. and that’s exactly what we need.

So the V value that we will use in our formula is equal to 5 – 1.8 = 3.2V

Now, we need to get the I value that we will use in the formula, which is the amount of current that we need it to flow through the LED to light properly without killing it. in any LED datasheet you find a characteristic called If or Imax which is the maximum continuous current rating this often around 25mA or 30mA, which means the typical current rating is around 20mA

So, 20mA is the current that we are hoping to get when we are picking the resistor value and that is the current value “I” that we will use in the formula.
Take Care!
Actually, LEDs can handle higher current values upto 25mA but it comes at cost of your batteries, also it’s not very recommended to run the LED near the maximum current rating values because it will decrease it’s life span and may kill it due to the over heating. I think 20mA is a very good current value.

After getting the formula’s V and I values. Now, we are ready to calculate the resistor value easily.

3.2 V / 20 mA = 3.2 V / 0.02 A = 160 Ω

Now, the 160Ω resistor value will allow 20mA of current to flow through the LED at 5V which is exactly what we need.

Don't have a 160Ω resistor?!
If you don’t have a 160Ω resistor, don’t worry! it’s not necessarily to use the same exact calculated value. To be in the safe side, we generally select the next higher nearest resistor value.

the higher the value of the resistor you go with the less current will flow through the LED the less LED brightness.

LED ColorForward Voltage(Vf)Forward Current (If)
Blue, White, Warm White,
Green, Pink, UV
3.2V – 3.8V20mA – 30mA
Red, Yellow, Orange1.8V – 2.2V20mA – 30mA

Wiring Diagram


Wiring Diagram
Wiring Diagram

The wiring is pretty straightforward, we are connecting the long leg of the LED to the Arduino digital pin 7 through a resistor, and the LED short leg to the GND. That’s it!

Arduino Code


const int redLED = 7; 			//initialize a constant named "redLED" it's value 7 which refers to the pin number which the red LED is connected to.

void setup()
{
  pinMode(redLED, OUTPUT);	    //set the GPIO pin 7 as an output pin.
}

void loop()
{
  digitalWrite(redLED, HIGH);	//output a logic level "HIGH" which means 5V on the digital pin number 7 to turn the LED ON. 
  delay(1000); 					// Wait for 1000 millisecond(s).
  digitalWrite(redLED, LOW);	//output a logic level "LOW" which means 0V on the digital pin number 7 to turn the LED OFF.
  delay(1000); 					// Wait for 1000 millisecond(s).
}

Code Explanation


const int redLED = 7;

initializing a constant named “redLED” with a value 7 that refers to the Arduino pin number 7 which the LED is connected to.

This step is not mandatory, you can use the pin number directly in the code instead of the variable name, but it’s a good practice to give each pin a name to easily debug and understand your code
void setup()
{
  pinMode(redLED, OUTPUT);
}

the setup() function is called once at the beginning of the program when the sketch starts or after the reset of the board, we use it to initialize variables, constants, take library objects, or to write some actions that we need to execute only once at the beginning of the program.

inside the setup() function, we set the Arduino pin number 7 as an output pin, which means that we are telling the Arduino board “Yo, make the pin number 7 ready to write a 5V or 0V output at any time.”

void loop()
{
  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(redLED, LOW);
  delay(1000);
}

After creating the setup() function which initializes and sets the initial values, the loop() function does precisely what its name suggests, inside the loop() function you write the main program that you need to get executed over and over again until you force shutdown your Arduino board.

inside the loop() function, we are telling the Arduino pin number 7 to write a logic level HIGH which means output 5V to the LED to light it.

Then wait 1 sec.

In the Next line, we are telling the Arduino pin number 7 to write a logic level LOW which means output 0V (GND) to the LED to turn it off.

Then wait another 1 sec. before it returns back to the first line inside the loop() function.

Troubleshooting


If your LED doesn’t take any actions:

  • Make sure that your LED wiring polarity is correct, LED long lead with the positive and the short one with the negative.
  • make sure that you are connecting a current limiting resistor that more than 160Ω
  • Make sure that you uploaded the Arduino code to the board Successfully.

Final


CONGRATS on your very first Arduino project fam! Today we have learned how to connect an LED on the Arduino board, and how to use it. Then we blinked an LED by writing some lines of code.

Author

junky Egyptian hardcore maker passionate about making some cool electronics random stuff, software and meme addict.

Write A Comment