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

Arduino Projects 7: Fading LED

DFRobot Jun 22 2017 1174

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

To achieve this we are going to use pulse width modulation (PWM).


Parts Needed:

DFRduino Uno  (similar as Arduino UNO R3)  x1
I/O Sensor Expansion Shield V7.1  x1

Digital Piranha  LED-R  x1 

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, connect the microcontroller to your PC with the USB cable so you can upload a program.


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. You can refer to the code section for more information about this.



Code Input

// 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 microcontroller.

Once the code has uploaded, you should see the LED gradually growing brighter and then dimming.


Code Analysis

When we need to repeat a line of code a number of times, we can use a for loop.


The following is the process of a for loop:
1. The “init” statement is executed first and only once.
2. The “condition” is evaluated. If it is false, the loop is stopped.
3. The “statement(s)” is executed.4. “Update” is executed and the code returns 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. These values are used to control the brightness of our LED.

Now let’s take a look at the analogWrite() function.
Digital pins only have two values: 0 or 1. How can we send an analog value to a digital pin?

- By using analogWrite().

There are six digital pins on your microcontroller that are marked with the symbol “~”, which means they can send out a PWM signal.

The function is written in the following form:
analogWrite(pin,value)

The analogWrite() function is used to write an analog value between 0 - 255 a PWM pin. analogWrite() can only be used for PWM pins, which are pins 3, 5, 6, 9, 10 and 11 on your microcontroller.

PWM stands for pulse width modulation. Very simply, PWM allows us to achieve analog results with digital signals by switching signals between HIGH and LOW thousands of times a second. Using this, we can simulate voltages between 0v and 5v.

If you were to look at the PWM signal on an oscilloscope (a device that you can use to view electrical signals), 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 these five square waves:

Orange vertical lines on this diagram represent an interval.
Black lines represent the electric pulse.
Each value included in the analogWrite(value) function can be mapped to a certain percentage. 0 = 0% and 255 = 100%.


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 will be.


PWM’s electronic applications include adjusting the brightness of an LED, controlling the speed of a DC motor, and many others. 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!

There is also another arduino starter kit for you to choose.

REVIEW