$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS Arduino

Arduino Intermediate Kit Tutorial 8: Fading LED

DFRobot Sep 01 2017 970

In previous sessions we’ve learned how to turn an LED on and off. In this project we’re going to build a fading LED. The LED will go from dim to bright and bright to dim continuously.

To achieve this, we are going to introduce the concept of PWM.


COMPONENTS

Digital Piranha LED-R

DFRduion UNO R3 (similar as Arduino UNO)
IO Expansion Shield for Arduino V7.1


HARDWARE CONNECTIONS

Attach the Digital Piranha LED-R to digital pin 10.


Use the diagram below for reference:


Be sure that your power, ground and signal connections are correct or you risk damaging your components.

When you have connected the components and checked your connections, plug the Arduino in to your PC with the USB cable so you can upload a program.


CODE INPUT
Sample Code 5-1:
// Item Five- Fading LED
int ledPin = 10;
void setup() {
      pinMode(ledPin,OUTPUT);
}
void loop(){
  for (int value = 0 ; value < 255; value=value+1){ 
     analogWrite(ledPin, value);
     delay(5);
  }
  for (int value = 255; value >0; value=value-1){ 
     analogWrite(ledPin, value); 
     delay(5); 
  }  
}	

Use this sample code to implement the behavior we want.
You can copy and paste it in to the Arduino IDE, but if you want to develop you skills we recommend typing it out.

When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your Arduino.

Once the code has uploaded, you should see the LED gradually brightening and dimming.


HARDWARE ANALYSIS (ANALOG OUTPUT)
This device is similar to experiment 1 where we made an LED blink. We also only have one output unit. The difference in this project is that we are using the LED for analog output, rather than digital output. Refer to the code section for more information.


CODE ANALYSIS
When we need to loop one line of code, we can use the “for” structure.


 “for” loops are written in the following structure:
for(init; condition; update){
statement(s);
}



Here is the flow:
1. The “init” statement is executed first and only once.
2. The “condition” is evaluated. If it is false, stop the loop.
3. The “statement(s)” is executed.
4. The “update” is executed. Go back to step 2.
    
Let’s return to our code:
for (int value = 0; value < 255; value=value+1){ 
        ...
}
for (int value = 255; value >0; value=value-1){ 
        ...
}
 


These two “for” loops make the value increase from 0 to 255 and then decrease from 255 to 1.

Now let’s take a look at the function analogWrite().

Digital pins only have two values: 0 or 1. How can we send an analog value to a digital pin? We can use this function. Six digital pins on the Arduino UNO are marked with the symbol “~”, which means they can send out PWM signal.

The function is written in the following form:
analogWrite(pin,value)
The function analogWrite() is used to write an analog value between 0 and 255 to the PWM pins. analogWrite() can only be used for PWM pins, which are pins 3, 5, 6, 9, 10, 11 on an Arduino UNO.

PWM stands for pulse width modulation. Very simply, PWM allows us to achieve analog results with digital signals by switching the signals HIGH and LOW very rapidly. Using this, we can simulate voltages between 0v and 5v.
If you were to look at the PWM signal on an oscilloscope, you would see a square wave. The top of the square is the HIGH signal, and the bottom is LOW. The greater the width of the square, the longer the HIGH pulse – hence the name pulse width modulation.


Let’s explore PWM in more detail by examining five square waves.
Fig 4-2 PWM Illustrations

Green vertical lines on the graph represent an interval.
The black line represents the electric pulse.
Each value included in the function of “analogWrite(value)” can be mapped to a certain percentage, which is also called Duty Cycle.
The duty cycle refers the length of time the pulse goes from LOW to HIGH and HIGH to LOW.
The duty cycle of the first square wave is 0% and the corresponding value is 0. 
If the duty cycle is 100%, the analog value will be 255. 
The longer the duty cycle, the brighter the light is.


PWM’s electronic applications include adjusting the brightness of an LED, or varying the speed of a DC motor. Can you think of any other applications for it?
 
With the same hardware connections we’ve used in this session, the LED can achieve completely different effects by using a different code.
Have a play with the code and see if you can make different lighting effects. Use your imagination and have fun with it! 


If you have any questions, please feel free to comment.
There is also another best selling arduino starter kit for you to choose.
Related category: arduino kits > education kits
Last Arduino Tutorial:  Arduino Intermediate Kit Tutorial 7: Sound Activated LED
Next Arduino Tutorial: Arduino Intermediate Kit Tutorial 9: Light Regulator

REVIEW