8. LED Blink

Let’s connect hardware first,

LED-and-Symbol
  • Before connecting the LED, we need to understand its terminals properly.As we can see, there are 2 terminals, cathode (-ve) and anode (+ve).
  • We must learn to identify LED terminals by observing them. If we observe properly, inside the LED, there is one terminal, which is flat (bigger size), which is always a cathode, and the remaining one is an anode.
  • To turn ON the LED with proper brightness, it usually requires 10 to 20 mA of current through it. Exceeding the current above its higher limit will damage the LED.
  • So, connecting the proper resistor is always important to avoid LED damage.
  • Also, the LED drop voltage is around ~2 Volts, depending on LED color and specifications. It is always recommended to check the LED datasheet for LED current & drop-down voltage. 

Note: "Always run LEDs below their maximum current to avoid damage. For example, if the max current is 25mA, aim for 10–15mA. This helps keep the LED cool and extends its life, especially since heat can reduce the safe current limit."

Calculating the Resistor Value

To ensure a 10 mA current through the LED, we need to select an appropriate resistor based on the supply voltage.

Case 1: 5V Supply

led-resistor-calculation
  • LED forward voltage (Vf) = 1.8V (from datasheet)
  • Voltage across resistor (VR) = Supply voltage – Vf = 5V – 1.8V = 3.2V
  • Resistor value (R) = VR / I = 3.2V / 10 mA = 320 Ω

Standard resistor values near 320 Ω: 330 Ω or 300 Ω (whichever is available).

Similarly, Case 2: 3.3V Supply

  • Voltage across resistor (VR) = 3.3V – 1.8V = 1.5V
  • Resistor value (R) = 1.5V / 10 mA = 150 Ω

Standard resistor value: 150 Ω.

Methods of LED Connection with GPIO

The LED can be connected to a GPIO pin in two ways

Method 1: Sourcing Current from GPIO

  • The anode is connected to the GPIO pin.
  • The cathode is connected to ground via a resistor.
  • The LED turns ON when the GPIO is HIGH.

Method 2: Sinking Current to GPIO

  • The cathode is connected to the GPIO pin.
  • The anode is connected to VCC via a resistor.
  • The LED turns ON when the GPIO is LOW (acting as a sink to ground).
Led-interfacing-methods

So, by selecting a proper resistor and connecting the LED correctly, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!