Maple Lesson 04 - Breathing LED experiment

From Microduino Wiki
Jump to: navigation, search

Objective

In previous experiment, LED only has two states, on and off. This experiment implemented a led fade in dimming, named breathing lamp.

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

This experiment uses PWM to control the LED. This method adjusts the digital signal (" 0 ", "1") value within a period of time that is the time of the duty ratio to control the brightness of LED. If the high voltage "1" lasts long time, the LED will light longer. For PWM's detailed information, please refer to: http://www.geek-workshop.com/thread-125-1-1.html

But not all of the I/O ports can be used for the PWM, only several special port can be used.

Microduino-CoreSTM32's PWM I/O ports:0,1,4,11,12,14(A0),15(A1),16(A2),17(A3),18(SDA),19(SCL),20(A6),21(A7), we can use the LED on board directly, because it connects to the D4 pin. You also can use other PWM port to try.

Program

int ledPin=4;// 0,1,4,11,12,14(A0),15(A1),16(A2),17(A3),18(SDA),19(SCL),20(A6),21(A7) is the PWM port
void setup()
{
  pinMode(ledPin, PWM);
}
void loop(){
  for(int fadeValue=0;fadeValue<=65535;fadeValue+=500)
    //Control the PWM value using variable fadeValue, PWM will increase.
  {
    pwmWrite(ledPin,fadeValue);   //Write the brightness level to LED
    delay(30);                       //Set the during timer, the unit is ms 
  }
  for(int fadeValue=65535;fadeValue>=0;fadeValue-=500)
    //Control the PWM value using variable fadeValue, PWM will decrease.
  {
    pwmWrite(ledPin,fadeValue); //Write the brightness level to LED
    delay(30);                     //Set the during timer, the unit is ms 
  }
  delay(400);
}

Grammar:

  • pwmWrite(ledPin,fadeValue);, this function writes a PWM value to I/O port, this port will output a square wave. You can adjust the input value to change the duty ratio to realize the PWM control.
    • ledPin :PWM input pin
    • fadeValue: Duty ratio setting.

You need pay attention to that:Define the output as PWM mode, that is“pinMode(ledPin, PWM);”

Debug

  • Open the Maple IDE, copy the program to IDE, choose board type (Microduino-CoreSTM32 to Flash), click the download button or use the (Ctrl+U) to finish the download.
  • After download, you can see that the LED's brightness changes from off to on then to off softly in cycle.

Video