Difference between revisions of "Lesson 5--Microduino “LED Brightness and Potentiometer PWM”"

From Microduino Wiki
Jump to: navigation, search
Line 4: Line 4:
 
|
 
|
 
==Objective==
 
==Objective==
Last lesson we use the button to generate PWM to control the LED, this lesson we will use precision potentiometer to control the LED.
+
In the last lesson, we used a button to generate PWM to control the LED. Now, we will use a precision potentiometer to control the LED.
The difference between them is that the button use the digital voltage signal (0 and 1) to control which only has two states.
+
The difference between the two is that a button uses a digital signal (0 and 1) to control the LED.  
When the signal changed, LED increases brightness by 5 units (0 ~ 255).  
+
A potentiometer uses an analog signal to generate PWM which is a linear change of state, so the LED's brightness can be changed clearly and gradually.
Potentiometer uses the analog voltage to generate PWM which is a linear change of state, so the LED's brightnee can be changed coherently and softly.
+
Another downside of using a button is that electronic interference can cause unintended noise.  
Conversely,if use the button, you need consider the button shaking.
 
  
  
Line 17: Line 16:
 
*'''[[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        
**Precision potentiometer      one 
+
**1x Precision potentiometer       
**USB Data cable               one
+
**1x USB Data cable            
  
==Experimental schematic==
+
==Experiment Schematic==
 
[[File:lesson 5-schematic.jpg|600px|center|thumb]]
 
[[File:lesson 5-schematic.jpg|600px|center|thumb]]
Connection method, LED connects to the PWM output pin, and potentiometer connects to analog port A0 ~ A5. Analog interface can measure 0-5V voltage, and the corresponding return value is 0-1024,
+
*Connecting the Potentiometer <br>
the measurement accuracy of the voltage variation is relatively high.
+
The LED connects to any PWM output pin, and potentiometer connects to analog ports A0 ~ A5. The analog interface can measure 0-5V, and returns corresponding values 0-1024.
Potentiometer had better choose winding precision linear potentiometer,
 
because some cheap nonlinear potentiometer on the market doesn't have a good electrical characteristic.
 
Numerical drift is big which easy to cause the led flashing, Resistance is nonlinear variation, so
 
the brightness change is not obvious, easy to produce the sense of hierarchy just like the button dimmer experiments,
 
impact the test results.
 
  
 
==Program==
 
==Program==
Line 42: Line 36:
 
void loop()
 
void loop()
 
{
 
{
   int val= analogRead(A0);      //Read the analog port A0's value(voltage range is0-5V,corresponding value is 0-1204)
+
   int val= analogRead(A0);      //Read the analog port A0's value(voltage range is 0-5V,corresponding value is 0-1204)
 
   val = map(val, 0, 1023, 0, 255);
 
   val = map(val, 0, 1023, 0, 255);
   //Mapping the analog value(0~1024)to(0~255),the Max PWM value is 255。
+
   //We want to map the analog value(0~1024)to(0~255) since the max PWM value is 255.
 
   analogWrite(3, val);
 
   analogWrite(3, val);
  //analogWrite(11,val/4);    //The max PWM value is 255,so the analog value is divided by 4.
 
 
}
 
}
 
</source>
 
</source>
 
===map() function===
 
===map() function===
*Function: Map a certain range values to a different range
+
*Function: Maps a certain range of values to a different range
*Grammer:map(value, fromLow, fromHigh, toLow, toHigh)
+
*Parameters:map(value, fromLow, fromHigh, toLow, toHigh)
**value:return value
+
**value:value to be mapped
**fromLow: Paternal interval lower limit
+
**fromLow: Start value of source range
**fromHigh:Paternal interval upper limit
+
**fromHigh:End value of source range
**toLow:Mapping range lower limit
+
**toLow:Start value of target range
**toHigh:Mapping range upper limit
+
**toHigh: End value of target range
  
 
==Result==
 
==Result==
With the rotation of the potentiometer, LED's brightness changes softly.
+
As you turn the potentiometer, the LED's brightness changes gradually.
  
 
[[File:lesson5Result1.jpg|600px|center|thumb]]
 
[[File:lesson5Result1.jpg|600px|center|thumb]]
Line 65: Line 58:
  
 
==Video==
 
==Video==
 +
http://v.youku.com/v_show/id_XNzA5OTk1Mzky.html
 
|}
 
|}

Revision as of 04:32, 13 July 2015

Language: English  • 中文

Objective

In the last lesson, we used a button to generate PWM to control the LED. Now, we will use a precision potentiometer to control the LED. The difference between the two is that a button uses a digital signal (0 and 1) to control the LED. A potentiometer uses an analog signal to generate PWM which is a linear change of state, so the LED's brightness can be changed clearly and gradually. Another downside of using a button is that electronic interference can cause unintended noise.


Lesson5All.jpg

Equipment

  • Microduino-Core
  • Microduino-FT232R
  • Other hardware equipment
    • 1x Box of breadboard jumper wires
    • 1x Breadboard
    • 1x LED
    • 1x 220Ω resistor
    • 1x Precision potentiometer
    • 1x USB Data cable

Experiment Schematic

Lesson 5-schematic.jpg
  • Connecting the Potentiometer

The LED connects to any PWM output pin, and potentiometer connects to analog ports A0 ~ A5. The analog interface can measure 0-5V, and returns corresponding values 0-1024.

Program

void setup()
{
  pinMode(3,OUTPUT); //Choose the PWM output Port
}
void loop()
{
  int val= analogRead(A0);      //Read the analog port A0's value(voltage range is 0-5V,corresponding value is 0-1204)
  val = map(val, 0, 1023, 0, 255);
  //We want to map the analog value(0~1024)to(0~255) since the max PWM value is 255.
  analogWrite(3, val);
}

map() function

  • Function: Maps a certain range of values to a different range
  • Parameters:map(value, fromLow, fromHigh, toLow, toHigh)
    • value:value to be mapped
    • fromLow: Start value of source range
    • fromHigh:End value of source range
    • toLow:Start value of target range
    • toHigh: End value of target range

Result

As you turn the potentiometer, the LED's brightness changes gradually.

Lesson5Result1.jpg
Lesson5Result2.jpg

Video

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