Difference between revisions of "Lesson 6--Microduino "Breathing Light""

From Microduino Wiki
Jump to: navigation, search
(Program)
 
(8 intermediate revisions by 3 users not shown)
Line 4: Line 4:
 
|
 
|
 
==Objective==
 
==Objective==
The last two experiment uses the external device to generate PWM to control the LED, how to control the LED by program instead of the external device?
+
In the last two lessons we used external devices (a button and a potentiometer) to generate PWM to control the LED. From Lesson 1, we also learned how to turned an LED on and off using software. But how would we go about controlling an LED's brightness through PWM using software only? This lesson will teach you how to implement an LED "Breathing Light".
This lesson will give you an example. This experiment implemented an led fadein dimming, named breathing lamp.
 
  
 
==Equipment==
 
==Equipment==
Line 11: Line 10:
 
*'''[[Microduino-FT232R]]'''
 
*'''[[Microduino-FT232R]]'''
 
*Other hardware equipment
 
*Other hardware equipment
**Breadboard Jumper            one box 
+
**1x Box of breadboard jumper wires             
**Breadboard               one piece 
+
**1x Breadboard                
**LED Light-emitting diodes    one
+
**1x LED  
**220Ω resistor           one
+
**1x 220Ω resistor      
**USB Data cable              one
+
**1x USB Data cable               
  
==Experimental schematic==
+
 
 +
[[File:lesson6All.jpg|600px|center|thumb]]
 +
 
 +
==Experiment Schematic==
 
[[File:lesson 6-schematic.jpg|600px|center|thumb]]
 
[[File:lesson 6-schematic.jpg|600px|center|thumb]]
Using high level lighting, the output also need connect to Microduino's PWM I/O port. If use the low level lighting, it has the same result.
+
The schematic above uses a high level connection. Since we want the LED to be more than just on and off, we still need to connect the anode to a Microduino PWM I/O port. Using a low level connection achieves the same result.  
  
 
==Program==
 
==Program==
 
<source lang="cpp">
 
<source lang="cpp">
int ledPin=11;//D3、D5、D6、D9、D10、D11 is Microduino PWM I/O output port
+
int ledPin=11;//D3, D5, D6, D9, D10, D11 is Microduino PWM I/O output port
 
void setup()
 
void setup()
 
{
 
{
 +
pinMode(ledPin,OUTPUT);
 
}
 
}
 
void loop()
 
void loop()
Line 43: Line 46:
 
}
 
}
 
</source>
 
</source>
Program uses the loop statement, it's convenient to automatically control the brightness of the leds.
+
This program uses for loops, which allows for convenient control of the brightness of the LEDs.
  
 
==Result==
 
==Result==
LED lights from off to on softly then off, followed by cycle.
+
LED gradually goes from off to on, then off. It then repeats.
 +
 
 +
[[File:lesson6Result.jpg|600px|center|thumb]]
  
 
==Video==
 
==Video==
 +
http://v.youku.com/v_show/id_XNzA5OTk3MTU2.html
 
|}
 
|}

Latest revision as of 07:21, 12 September 2016

Language: English  • 中文

Objective

In the last two lessons we used external devices (a button and a potentiometer) to generate PWM to control the LED. From Lesson 1, we also learned how to turned an LED on and off using software. But how would we go about controlling an LED's brightness through PWM using software only? This lesson will teach you how to implement an LED "Breathing Light".

Equipment


Lesson6All.jpg

Experiment Schematic

Lesson 6-schematic.jpg

The schematic above uses a high level connection. Since we want the LED to be more than just on and off, we still need to connect the anode to a Microduino PWM I/O port. Using a low level connection achieves the same result.

Program

int ledPin=11;//D3, D5, D6, D9, D10, D11 is Microduino PWM I/O output port
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
  for(int fadeValue=0;fadeValue<=255;fadeValue+=5)
  //Increase PWM value, control the LED brightness by adjusting the value of fadeValue.
  {
    analogWrite(ledPin,fadeValue);   //Write the brightness value to LED.
    delay(30);                       //Keep the current brightness 30ms. 
  }
   for(int fadeValue=255;fadeValue>=0;fadeValue-=5)
   //Decrease PWM value,control the LED brightness by adjusting the value of fadeValue.
  {
    analogWrite(ledPin,fadeValue); //Write the brightness value to LED.
    delay(30);                     //Keep the current brightness 30ms. 
  }
}

This program uses for loops, which allows for convenient control of the brightness of the LEDs.

Result

LED gradually goes from off to on, then off. It then repeats.

Lesson6Result.jpg

Video

http://v.youku.com/v_show/id_XNzA5OTk3MTU2.html