Maple Lesson 05 - The button PWM controls LED brightness

来自Microduino Wikipedia
跳转至: 导航搜索

Objective

LED has only two states on and off in other experiments, now we will use the button to control LED brightness light gradually and gradually out

Equipment

Microduino-CoreSTM32 is an ARM development board using STM32F103CBT6 chip. It use special Upin7 interface, the size is similar with a coin and fully compatible other Microduino modules.

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

Schematic

Buttons are used with internal pull-up and external pull-down, and then connect to the digital I/O port D0-D13. PWM must use following I/O port. 0,1,4,11,12,14(A0),15(A1),16(A2),17(A3),18(SDA),19(SCL),20(A6),21(A7)。

Program

int led=4;
int butt0n_1=2;
int butt0n_2=3;
int n=0;

void setup ()
{
  pinMode(butt0n_1, INPUT_PULLDOWN); //Set to internal pull-down
  pinMode(butt0n_2,INPUT_PULLUP);//Set to internal pull-up
  pinMode(led,PWM);//This pin only can be set to port 0,1,4,11,12,14(A0),15(A1),16(A2),17(A3),18(SDA),19(SCL),20(A6),21(A7) for PWM.
}

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

Result

One button makes the LED brightness progressively decreasing, and another button makes LED brightness incremental.

Video