Introduction
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:
- R is the resistor value in ohms (Ω).
- Vcc is the supply voltage (e.g., 3.3V, 5V, 9V, or 12V).
- Vd is the forward voltage of the LED (which varies by color).
- I is the nominal current of the LED, usually around 20mA (0.02A).
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);
}
}
}