“Lesson 4--Microduino "LED Brightness and PWM"”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Created page with "{| style="width: 800px;" |- | ==Objective== LED has only two states on and off in other three experiment, now through button realize LED brightness light gradually and gradual...")
 
 
(未显示4个用户的6个中间版本)
第1行: 第1行:
 +
{{Language|第四课--按钮PWM控制LED亮度}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
 
|
 
|
 
==Objective==
 
==Objective==
LED has only two states on and off in other three experiment, now through button realize LED brightness light gradually and gradually out
+
In the previous three lessons, the LED was either on or off. In this lesson, you will learn how to control an LED's brightness using a button and PWM. PWM stands for pulse width modulation. The circuit adjusts the ratio of digital signals ("0", "1") to create a certain brightness. For example, if there are more 1's (HIGH), then the LED seems brighter.
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==
 
==Equipment==
第13行: 第10行:
 
*'''[[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  
**220ohm resistor           one
+
**1x 220ohm resistor            
**Button                       one 
+
**1x Button                        
**USB Data cable              one
+
**1x USB Data cable               
  
  
==Experimental schematic==
+
[[File:lesson3All1.jpg|600px|center|thumb]]
[[File:第四课-pwm原理图.jpg|600px|center|thumb]]
+
 
Buttons are used with internal pull-up and external pull-down, then connect to the digital I/O port D0-D13.
+
==Experiment Schematic==
PWM must use I/O port D3,D5,56,D9,D10 and D11.
+
[[File:pwm schematic.jpg|600px|center|thumb]]
 +
One button is connected using internal pull-up and the other using external pull-down. They are then connected to I/O ports D0~D13. For the [[Microduino-Core]], only D3, D5, D6, D9, D10, and D11 support PWM, so the user must connect the LED to one of those ports.
 +
 
 +
[[File:lesson3Setup1.jpg|600px|center|thumb]]
  
 
==Program==
 
==Program==
第33行: 第33行:
 
   pinMode(2,INPUT);
 
   pinMode(2,INPUT);
 
   pinMode(7,INPUT_PULLUP);//Set to internal pull-up
 
   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
+
   pinMode(11,OUTPUT);//PWM must use I/O ports 3、5、11、9、10、11
 
}
 
}
  
第60行: 第60行:
 
}
 
}
 
</source>
 
</source>
===analogWrite()usage===
+
===analogWrite() usage===
*Usage:Write the simulation value to the specified pin
+
*Usage:Writes the simulation value to the specified pin
*grammar:analogWrite(pin, val),pin:Microduino I/O port number;val:values from 0 to 255
+
*Parameters:analogWrite(pin, val)
 +
**pin: Microduino I/O port number
 +
**val: values from 0 to 255
  
 
==Result==
 
==Result==
Left button to make LED brightness progressively decreasing, and the right button to make LED brightness incremental.
+
Left button increases LED brightness and right button decreases LED brightness.  
 +
 
 +
[[File:lesson3Result1.jpg|600px|center|thumb]]
 +
 
 
==Video==
 
==Video==
 
|}
 
|}

2015年7月13日 (一) 03:02的最新版本

Language English

Objective

In the previous three lessons, the LED was either on or off. In this lesson, you will learn how to control an LED's brightness using a button and PWM. PWM stands for pulse width modulation. The circuit adjusts the ratio of digital signals ("0", "1") to create a certain brightness. For example, if there are more 1's (HIGH), then the LED seems brighter.

Equipment

  • Microduino-Core
  • Microduino-FT232R
  • Other hardware equipment
    • 1x Box of breadboard jumper wires
    • 1x Breadboard
    • 1x LED
    • 1x 220ohm resistor
    • 1x Button
    • 1x USB Data cable


Lesson3All1.jpg

Experiment Schematic

Pwm schematic.jpg

One button is connected using internal pull-up and the other using external pull-down. They are then connected to I/O ports D0~D13. For the Microduino-Core, only D3, D5, D6, D9, D10, and D11 support PWM, so the user must connect the LED to one of those ports.

Lesson3Setup1.jpg

Program

int n=0;
void setup ()
{
  pinMode(2,INPUT);
  pinMode(7,INPUT_PULLUP);//Set to internal pull-up
  pinMode(11,OUTPUT);//PWM must use I/O ports 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:Writes the simulation value to the specified pin
  • Parameters:analogWrite(pin, val)
    • pin: Microduino I/O port number
    • val: values from 0 to 255

Result

Left button increases LED brightness and right button decreases LED brightness.

Lesson3Result1.jpg

Video