본문 바로가기
  • 경제적 자유를 얻는 그날까지
엔지니어링/임베디드

[아두이노 기초교육] RGB LED 제어

by 베니스상인 2020. 2. 1.

RGB LED에 대한 기초 전자회로에 대해 알아보고, Push Button 3개를 이용하여 RGB LED의 밝기를 제어해본다.

 

1. 회로도 구성

 

 

- RGB LED의 밝기를 제어하기 위한 Push buttion이 3개 있으며 순서대로 디지털 입력 4,3,2에 연결됨

- RGB LED는 순서대로 PWM 포트(물결무늬) 11, 10, 9에 연결됨(2^24까지 색을 표현 가능)

 

fritzing으로 작성한 브레드보드 구성

 

 

2. 회로설명

1) RGB LED

- 빛의 3원색: Red, Green, Blue

- PWM을 이용하여 빛의 밝기를 제어함

- 아두이노 UNO는 약 500Hz(2ms)까지 주기 변경이 가능함

 

2) PWM

- ~(물결무늬)가 있는 포트는 PWM 사용가능

- 아두이노에서는 AnalogWrite를 통해 PWM을 생성(https://www.opentutorials.org/module/2106/12246)

 

 

 

 

3. 소스코드

 

AUNO_Basic_02_RGB_Button.ino
0.00MB

 

 


/*****************************************************/
/*  Project   : Arduino Basic Training Course
/*  Title     : 02_RGB_Button
/*  Descript  : RGB LED control example with 3 Push Button
/*  Platform  : Arduion UNO
/*  IDE: Arduino Sketch 1.6/1.8 or vMicro
/*  Author    : shlee853
/*  History   : Rev 1.0  2020.01.31  - Initial release 
/*
/*****************************************************/



#define RED   11
#define GREEN 10
#define BLUE  9

#define RB    4
#define GB    3
#define BB    2


unsigned char r=0;
unsigned char g=0;
unsigned char b=0;



void setup() {
  // put your setup code here, to run once:
  pinMode(RB, INPUT);
  pinMode(GB, INPUT);
  pinMode(BB, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  if(digitalRead(RB) == HIGH) {
    r=r+16;
  }
  if(digitalRead(GB) == HIGH) {
    g=g+16;
  }
  if(digitalRead(BB) == HIGH) {
    b=b+16;
  }
  
  analogWrite(RED, r);  // PWM 값을 출력
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
  delay(100);  

}

 

 

 

 

 

 

 

728x90

댓글