Lesson 5--Microduino “LED Brightness and Potentiometer PWM”
From Microduino Wiki
Revision as of 04:32, 13 July 2015 by Changliu818@yahoo.com (talk)
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.
Equipment
Experiment Schematic
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. 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 |