Maple Lesson 07 - Control RGB LED

来自Microduino Wikipedia
Jasonsheng讨论 | 贡献2014年8月24日 (日) 13:16的版本 (Created page with "{| style="width: 800px;" |- | ==Objective== You have learned several LED experiment, then go on studying the RGB LED that is display different color by a RGB LED, including th...")
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

Objective

You have learned several LED experiment, then go on studying the RGB LED that is display different color by a RGB LED, including the breathing lamp's effect.

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
    • RGB LED one
    • 220Ω resistor one
    • USB Data cable one


RGB

RGB LED contains three LEDs, one is red, the other is green and another it blue. By controlling three LED's brightness, you can mix up almost any color you want.

Lesson7-RGB.jpg

Connection method

  • Method 1: The longest lead wire (anode) will be connected to +5 V. The other three pins are connected the series resistance of 220Ω then connected to Microduino PWM output port. The resistance use to prevent too much current flows and burn the LED.
  • Method 2: RGB anode through 220Ω resistor connects to VCC, the other three pins connected to the PWM output port. Three LED share a resistance, then the brightness dimmed.

Schematic

The following connection uses method 1 and uses pin D4、D11 and D12. You also can use PWM pin:0,1,4,11,12,14(A0),15(A1),16(A2),17(A3),18(SDA),19(SCL),20(A6),21(A7).

Program

int redPin = 4;
int greenPin = 11;
int bluePin = 12;
 
void setup()
{
  pinMode(redPin, PWM);
  pinMode(greenPin, PWM);
  pinMode(bluePin, PWM);  
}
  
void loop()
{
  setColor(65535, 0, 0);  // Red
  delay(1000);
  setColor(0, 65535, 0);  // Green
  delay(1000);
  setColor(0, 0, 65535);  // Blue
  delay(1000);
  setColor(65535, 65535, 0);  // Yellow
  delay(1000);  
  setColor(80, 0, 80);  // Purple
  delay(1000);
  setColor(65535, 65535, 65535);//White 
  delay(1000);
  setColor(0, 0, 0);  //Black
  delay(1000);
  for(int i=0;i<65535;i+=1285)//Red coming on
  {
    setColor(i, 0, 0);  
    delay(30);
  }
  delay(100);
  for(int i=65535;i>0;i-=1285)//Red coming off
  {
    setColor(i, 0, 0);  
    delay(30);
  }
  delay(100);
  for(int i=0;i<65535;i+=1285)//Blue coming on
  {
    setColor(0, i, 0); 
    delay(30); 
  }
  delay(100);
  for(int i=65535;i>0;i-=1285)//Blue coming off
  {
    setColor(0, i, 0);  
    delay(30);
  }
  delay(100);
  for(int i=0;i<65535;i+=1285)//Green coming on
  {
    setColor(0, 0, i);  
    delay(30);  
  }
  delay(100);
  for(int i=65535;i>0;i-=1285)//Green coming off
  {
    setColor(0, 0, i);  
    delay(30);
  }
  delay(100);
}
 
void setColor(int red, int green, int blue)//Color display program
{
  pwmWrite(redPin, 65535-red); //A total of anode RGB, low level light red LED using 65535-red
  pwmWrite(greenPin, 65535-green);
  pwmWrite(bluePin, 65535-blue);  
}

Result

In a light can be seen on red, green, blue, yellow, purple, black, white, and the effect like a breathing lamp.

Video