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

From Microduino Wiki
Jump to: navigation, search
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Language|Lesson_5--The_potentiometer_PWM_control_of_LED_brightness}}
+
{{Language|第五课--电位器PWM控制LED亮度}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
 
|
 
|
 
==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 deffience 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 use the analog voltage to generate PWM which is a linear change of state, so the LED's brightnees can be changed coherently and softly.
+
Another downside of using a button is that electronic interference can cause unintended noise. That was why we had to add a pull-up/pull down resistor to our button.
Conversely,if use the button, you need consider the button shaking.
+
 
 +
 
 +
[[File:lesson5All.jpg|600px|center|thumb]]
  
 
==Equipment==
 
==Equipment==
Line 14: 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. The center pin of the potentiometer connects to analog ports A0 ~ A5. The potentiometer's other two pins connect to GND and 5V.
Potentiometer had better choose winding precision linear potentiometer,
+
The analog interface can measure 0-5V, and analogRead() returns corresponding values 0-1024.
because some cheap nonlinear potentiometer on the market doesn't have a good electrical characteristics.
 
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==
 
<source lang="cpp">
 
<source lang="cpp">
   void setup()
+
void setup()
   {
+
{
     pinMode(3,OUTPUT); //Choose the PWM output Port
+
  pinMode(3,OUTPUT); //Choose the PWM output Port
   }
+
}
   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 brightness changes softly.
+
As you turn the potentiometer, the LED's brightness changes gradually.
 +
 
 +
[[File:lesson5Result1.jpg|600px|center|thumb]]
 +
[[File:lesson5Result2.jpg|600px|center|thumb]]
 +
 
 
==Video==
 
==Video==
 +
http://v.youku.com/v_show/id_XNzA5OTk1Mzky.html
 
|}
 
|}

Latest revision as of 07:20, 12 September 2016

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. That was why we had to add a pull-up/pull down resistor to our button.


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. The center pin of the potentiometer connects to analog ports A0 ~ A5. The potentiometer's other two pins connect to GND and 5V. The analog interface can measure 0-5V, and analogRead() 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