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

From Microduino Wiki
Jump to: navigation, search
(Program)
Line 35: Line 35:
 
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 is0-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。
+
  //Mapping the analog value(0~1024)to(0~255),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.
+
  //analogWrite(11,val/4);    //The max PWM value is 255,so the analog value is divided by 4.
 
}
 
}
 
</source>
 
</source>

Revision as of 09:16, 2 July 2015

Language: English  • 中文

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. The difference between them is that the button use the digital voltage signal (0 and 1) to control which only has two states. When the signal changed, LED increases brightness by 5 units (0 ~ 255). 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. Conversely,if use the button, you need consider the button shaking.

Equipment

  • Microduino-Core
  • Microduino-FT232R
  • Other hardware equipment
    • Breadboard Jumper one box
    • Breadboard one piece
    • LED Light-emitting diodes one
    • 220Ω resistor one
    • Precision potentiometer one
    • USB Data cable one

Experimental schematic

Lesson 5-schematic.jpg

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, the measurement accuracy of the voltage variation is relatively high. 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

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 is0-5V,corresponding value is 0-1204)
  val = map(val, 0, 1023, 0, 255);
  //Mapping the analog value(0~1024)to(0~255),the Max PWM value is 255。
  analogWrite(3, val);
  //analogWrite(11,val/4);     //The max PWM value is 255,so the analog value is divided by 4.
}

map() function

  • Function: Map a certain range values to a different range
  • Grammer:map(value, fromLow, fromHigh, toLow, toHigh)
    • value:return value
    • fromLow: Paternal interval lower limit
    • fromHigh:Paternal interval upper limit
    • toLow:Mapping range lower limit
    • toHigh:Mapping range upper limit

Result

With the rotation of the potentiometer, LED's brightness changes softly.

Video