LED Control with Arduino

Practical Guide to LED Control

Learn electronics and programming basics with simple projects

Introduction

Project Components

In this lesson you will learn:

  • How LEDs work
  • How to calculate the right resistor
  • Programming different light patterns
  • Control through a computer

Safety Warning!

● Always use a resistor to protect your circuits
● Ensure the correct polarity
● Avoid exposing the LED to direct current

Ohm's Law for Resistor Calculation

To calculate the resistor needed to protect your LED, use the following formula:


R = (Vcc - Vd) / I
            

Where:

Resistor Calculation for Different LED Colors

Below is the resistor value required for different LED colors based on various supply voltages.

Color Forward Voltage (Vd) Ω (3.3V) Ω (5V) Ω (9V) Ω (12V)
Infrared 1.4V 150Ω 270Ω 510Ω 680Ω
Red 1.8V 100Ω 220Ω 470Ω 680Ω
Orange 2.1V 100Ω 200Ω 470Ω 680Ω
Yellow 2.2V 100Ω 200Ω 470Ω 680Ω
Green 3.2V 10Ω 150Ω 330Ω 560Ω
Blue 3.5V - 100Ω 330Ω 560Ω
Violet 3.6V - 100Ω 330Ω 560Ω
White 3.8V - 100Ω 330Ω 560Ω

Programming Examples


// Basic Blinking Example
const int ledPin = 9;

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

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn LED on
  delay(1000);                 // Wait for one second
  digitalWrite(ledPin, LOW);   // Turn LED off
  delay(1000);                 // Wait for one second
}
                    

const int ledPIN = 9;
int option;

void setup(){
  Serial.begin(9600);
  pinMode(ledPIN, OUTPUT);
}

void loop(){
  if (Serial.available() > 0) {
    char option = Serial.read();
    if (option >= '1' && option <= '9') {
      option -= '0';
      for(int i = 0; i < option; i++) {
         digitalWrite(ledPIN, HIGH);
         delay(100);
         digitalWrite(ledPIN, LOW);
         delay(200);
      }
    }
  }
}
                    

const int ledPIN = 5;
byte outputValue = 0;

void setup() {
   Serial.begin(9600);
   pinMode(ledPIN, OUTPUT);
}

void loop() {
   if (Serial.available() > 0) {
      outputValue = Serial.read();
      if(outputValue >= '0' && outputValue <= '9') {
         outputValue -= '0';
         outputValue *= 25;
         analogWrite(ledPIN, outputValue);
      }
   }
}