Difference between revisions of "Lesson 5--Microduino “LED Brightness and Potentiometer PWM”"
(7 intermediate revisions by 3 users not shown) | |||
Line 4: | Line 4: | ||
| | | | ||
==Objective== | ==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 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. | |
− | + | ||
+ | |||
+ | [[File:lesson5All.jpg|600px|center|thumb]] | ||
==Equipment== | ==Equipment== | ||
Line 14: | Line 16: | ||
*'''[[Microduino-FT232R]]''' | *'''[[Microduino-FT232R]]''' | ||
*Other hardware equipment | *Other hardware equipment | ||
− | ** | + | **1x Box of breadboard jumper wires |
− | **Breadboard | + | **1x Breadboard |
− | **LED | + | **1x LED |
− | **220Ω resistor | + | **1x 220Ω resistor |
− | **Precision potentiometer | + | **1x Precision potentiometer |
− | **USB Data cable | + | **1x USB Data cable |
− | == | + | ==Experiment Schematic== |
[[File:lesson 5-schematic.jpg|600px|center|thumb]] | [[File:lesson 5-schematic.jpg|600px|center|thumb]] | ||
− | + | *Connecting the Potentiometer <br> | |
− | + | 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== | ==Program== | ||
Line 35: | Line 33: | ||
void setup() | void setup() | ||
{ | { | ||
− | + | 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 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); | |
− | |||
} | } | ||
</source> | </source> | ||
===map() function=== | ===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: | + | **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== | ==Result== | ||
− | + | 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 • 中文 |
---|
ContentsObjectiveIn 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.
Equipment
Experiment Schematic
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. Programvoid 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
ResultAs you turn the potentiometer, the LED's brightness changes gradually. Video |