Difference between revisions of "Lesson 4--Microduino "LED Brightness and PWM""

From Microduino Wiki
Jump to: navigation, search
Line 1: Line 1:
{{Language|Lesson_4--The_button_PWM_control_of_LED_brightness}}
+
{{Language|第四课--按钮PWM控制LED亮度}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-

Revision as of 14:19, 2 March 2014

Language: English  • 中文

Objective

LED has only two states on and off in other three experiment, now through button realize LED brightness light gradually and gradually out That is PWM pulse width modulation. Adjusting digital signal (" 0 ", "1") within a period of time that is the time of the duty ratio, high level "1", the longer and more bright.

Detailed information for PWM, please refer to: http://www.geek-workshop.com/thread-125-1-1.html

Equipment

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


Experimental schematic

Pwm schematic.jpg

Buttons are used with internal pull-up and external pull-down, then connect to the digital I/O port D0-D13. PWM must use I/O port D3,D5,56,D9,D10 and D11.

Program

int n=0;
void setup ()
{
  pinMode(2,INPUT);
  pinMode(7,INPUT_PULLUP);//Set to internal pull-up
  pinMode(11,OUTPUT);//The PWM only can use I/O port 3、5、11、9、10、11
}

void loop()
{
  int up =digitalRead(2);          //Read port 2's state
  int down = digitalRead(7);      //Read port 7's state   
  if (up==HIGH)                    
  { 
    n=n+5;                         
    if (n>=255) {
      n=255;
    }            //The max limitation is 255   
    analogWrite(11,n);   //Using PWM control the output of port 11, the range of the variable n is 0-255
    delay (300);
  } 
  if (down==LOW)             
  {
    n=n-5;
    if (n<=0) {
      n=0;
    }
    analogWrite(11,n); //Using PWM control the output of port 11, the range of the variable n is 0-255
    delay (300);
  }
}

analogWrite()usage

  • Usage:Write the simulation value to the specified pin
  • grammar:analogWrite(pin, val),pin:Microduino I/O port number;val:values from 0 to 255

Result

Left button to make LED brightness progressively decreasing, and the right button to make LED brightness incremental.

Video