LED Basics (2)

This will be our second circuit.

Before we start, let us talk about PWM.
PWM is the abbreviation for pulse-width modulation. What is it?

You can control the power of many components. You can do this by reduce the voltage. This is a good idea for an engine, but not with an LED. If you try this with an LED, the LED does not seem darker. The LED is at a certain point off. We need another way to reduce the energy. We need pulse-width modulation. Look here:

PWM-1

Normaly, we have 5V over a period of time. In this time, the LED get a certain amount of energy. The LED shines brightly. What can we do to reduce the energy? It’s not possible to reduce the voltage. Ok, so we reduce the time! Look here:

PWM-2

We produce a pulsating flow of energy. You can compare it with an switch that you always switch on and off. If you do this more then 28 times per second, the human eye does not detect a flicker. The frequency is fix. What we can change is the pulse width. Look here:

PWM-3

We use the same (fix) frequency, we change the pulse width only. Pulse A is longer, pulse B is shorter.
Take a closer look at pulse A: We use 50% of the time with energy, 50% time without. Pulse B use 25% of the time with energy, 75% without. Our LED get more energy with pulse A, it shining brighter. If we use pulse B, the LED gets less energy, it’s appears darker.

This is pulse-width modulation!

Some words about pulse-width Modulation and Arduino Uno.

The Arduino Uno has 6 pins which can be use with pulse-width modulation. If possible, don’t use pin 5 and 6. This pins are shared one timer together with the millis() and delay() functions. And so it’s possible sometimes, that an LED isn’t powered off with pulse-width 0.

Each pin has an 8 bit output. You can use values between 0 and 255. The Arduino function analogWrite() delivers a rectangle wave Signal with a pulse-width comparable to the value passed.

     analogWrite(0)     -> 0% energy
     analogWrite(255) -> 100% energy

The PWM frequency is fix. It is round about 490 heart (Hz).

Task:
Control an LED with pulse-width Modulation (fade an LED)!

The circuit:

1.0-LED

For this tutorial, wee need only two components:

  • LED (i.e. 2V, maximum 20 mA)
  • resistor

The LED ist a standard component. We use here an LED with 2V and a maximum amperage of 20 mA. We need this informations to calculate the resistor:

We get 5V from the Arduino Pin – so we can calculate:     5V (Arduino) – 2V (LED) = 3V (resistor)

You know the formula:        

1g2u

We use the following:        

1g2v

You see, it’s the same construction like tutorial 1.0 LED – the first sketch!

Let’s  start our Arduino software to create the  sketch.

/*
 LED fade
 playground2014.wordpress.com
*/

int pinled = 9;           // PinPort LED (we use Pin 9)

void setup()  { 
  pinMode(pinled, OUTPUT);  // declare pin 9 to be an output: 
} 

void loop()  { 
  digitalWrite(pinled, HIGH);    // set pin 9 to HIGH -> LED on
  delay(1000);                   // 1.000 ms delay

  analogWrite(pinled, 100);      
  delay(1000);

  digitalWrite(pinled, LOW);     // set pin 9 to HIGH -> LED off
  delay(1000);                   // 1.000 ms delay

  for (int i=0; i<=255; ++i) {       
   analogWrite(pinled,i);    
   delay(10);    
  }

  for (int i=255; i>=0; --i) {
   analogWrite(pinled,i);
   delay(10); 
  }

  delay(1000);
}

The first 4 lines (/* … */) declare a comment.

int pinled = 9;
We define a variable with the name “pinled“. The type is integer, and we assign the value 9. You remember, we use Pin 9 to communicate with the Arduino.

void setup() { … }
Commands in this block will perform on start (or reset) of our sketch. Once only.

pinMode(pinled, OUTPUT);
We define the direction – we want to use Pin 8 as an output Pin.

void loop() { … }
Commands in this block will perform endless.

digitalWrite(pinled, HIGH);
We send a “HIGH” to Pin 9. This means, Pin 9 has 5V in this moment. Current flows, and we see the LED is on!

delay(1000);
We wait one second to “see” that the LED is on

analogWrite(pinled,100);
We modulate the pulse-width! We can use values between 0 and 255. The value 100 means, the LED is significantly darker. After this statement, we wait 1 second to register the dark LED. 

digitalWrite(pinled, LOW);
We send a “LOW” to Pin 9. The LED is powered off.

delay(1000);
We wait one second to “see” that the LED is off.

  
for (int i=0; i<=255; ++i) {       
   analogWrite(pinled,i);    
   delay(10);    
}

Now, we use a “For-loop” to manipulate the brightness of the LED. We define a variable i. The first value is 0. In each pass we increase i by one. The maximum is 255, not more!
In the loop we send i to pin 9 and wait a little bit to see the change of brightness. With the maximum of 255, the LED is on (like digitalWrite(pinled, HIGH).

The second loop is similar. We change the direction only. We start with a value of 255 and decrease the value in each pass. The last statement here is analogWrite(pinled,0) – the LED is off (like digitalWrite(pinled, LOW).

Thats all! You can play with the delay times or with the increment of “i” … that’s your part.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s