January 11, 2015

Driving a LED strip with Arduino




Do you know those cheap LED strips?

These are often called "analog" LED strips because you cannot control each pixel individually like you do with "digital" strips.


Today we want to drive them using our Arduino board.



Hardware

  • Arduino Board
  • MOSFET
  • White LED strip

As MOSFET i have used I have used the IRF520 that comes with the Arduino starter kit but you can use any general purpose MOSFET (for example the popular STP16NF06).

Schematic









Code


#define PIN 11      // control pin
#define DELAY 10    // 20ms internal delay; increase for slower fades

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

void loop() {
  // fade in
  for(int i=0; i<255; i++) {
    analogWrite(PIN, i);
    delay(DELAY);
  }

  // fade out
  for(int i=0; i<255; i++) {
    analogWrite(PIN, 255-i);
    delay(DELAY);
  }
}

This is the code if you prefer a "strobo effect".

#define PIN 11      // control pin
#define DELAY 20    // 20ms internal delay; increase for slower pulse

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

void loop() {
  digitalWrite(PIN, HIGH);  // turn on
  delay(DELAY);             // wait few milliseconds
  digitalWrite(PIN, LOW);   // turn off
  delay(DELAY*10);          // wait some more time
}


References


Dimming a 12V LED Strip With an Arduino
RGB LED Strips
RGB Led Strip Controlled by an Arduino


January 10, 2015

Fading colors with an RGB LED

In this post I will explain how to make a simple fading RGB LED.
The code smoothly fades an RGB LED using PWM through 3 different basic colors (red, green and blue).

Hardware

  • Arduino Board
  • RGB LED
  • 3x Resistors 220 Ohm

Schematic

The schematic is very simple. Just connect the common cathod (+) to the GND pin. Then connect the 3 other leads to 3 PWM pins on your Arduino with a 220 Ohm resistor between.






Code


#define RED_PIN 9      // where the red pin is connected to
#define GREEN_PIN 10   // where the green pin is connected to
#define BLUE_PIN 11    // where the blue pin is connected to
#define DELAY 20       // 20ms internal delay; increase for slower fades

void setup() {
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
}

void loop() {
  // fade from green to red
  for(int i=0; i<255; i++) {
    analogWrite(RED_PIN, i);
    analogWrite(GREEN_PIN, 255-i);
    analogWrite(BLUE_PIN, 0);
    delay(DELAY);
  }

  // fade from red to blue
  for(int i=0; i<255; i++) {
    analogWrite(RED_PIN, 255-i);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, i);
    delay(DELAY);
  }

  // fade from blue to green
  for(int i=0; i<255; i++) {
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, i);
    analogWrite(BLUE_PIN, 255-i);
    delay(DELAY);
  }
}

References

Fading RGB LED (Arduino)
Arduino ColorCrossfader
RGB LEDs
Make an RGB LED Fader

January 9, 2015

LED Blink

This is the very first project of every Arduino owner... make a LED blink.


Hardware

  • Arduino Board
  • LED
  • 220 Ohm resistor

Schematic



Circuit



Code


int pinLed = 13;

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

void loop() {
  digitalWrite(pinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                  // wait for a second
  digitalWrite(pinLed, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                  // wait for a second
}

References

Arduino Blink example
Instructables tutorial

https://www.sparkfun.com/tutorials/219

January 8, 2015

LED basics

A light-emitting diode (LED) is a diode which emits light when activated. To limit the current flowing into the diode, it is important to introduce a current-limiting resistor in the loop.

The value of the appropriate resistor can be calculated with the following formula:

R=(V-Vd)/Id

where Vd is the voltage drop and Id is the current you want to use on your LED, usually the forward current. Those values depends on the specs of each LED and varies depending on the color of the LED.

So… you just want to light up an LED. What resistor should you use?

We can can make some rough assumptions to simplify the calculation:
  • Common LEDs runs at 20m.
  • The voltage drop is typically around 3V.
  • Arduino runs at 5V
This lead to the following calculation:
R = (5-3)/0.02 = 100 (Ohm)

We can say that 100 Ohms is the absolute minimum resistance we need to make sure that we do not damage the LED. To be safe, it's a good idea to use something a little higher, just in case your LED has slightly different ratings that what I've used here.
I always use 220 Ohm resistors because they are a safe choice for all kind of LEDs and are easy to find.

If you know the ratings of your LED (you can find it on the LED's datasheet) and you want to do this calculation yourself or use an online calculator like this or this.

References

All about LEDS
LED resistance calculator
Arduino Blink example
Instructables tutorial
Turn on LED with 5V
LED Current Limiting Resistors