개발자/라즈베리파이4

라즈베리파이4 rgb led 코드

지구빵집 2022. 6. 23. 08:15
반응형

 

 

 

RGB LED는 빛의 3원색인 Red, Green, Blue 세가지 색의 LED를 하나의 패키지로 만들어 놓은 LED 입니다. 보통 실습에 사용하는 RGB LED 모듈은 아래와 같이 4핀으로, 각각 Red, Green, Blue LED의 공통 애노드(+)와 공통 캐소드(-)인 핀으로 구성됩니다. 애노드와 캐소드를 구분하실 때는 A+는 공부도 잘하고 A급이니까 플러스 +이고 C는 점수가 낮으니 - 마이너스 라고 생각하시면 됩니다.  

 

 

공통 Anode, Cathod 구분 이미지 https://m.blog.naver.com/steamedu123/221495272123

 

공통 Cathod 타입의 RGB Led 모듈, 이미지 - https://juahnpop.tistory.com/165

 

RGB 각각의 LED에는 전류 제한용 150Ω 이 직렬로 연결되어 있어, 별도의 전류 제한 저항 없이 라즈베리파이나 아두이노 GPIO 출력에 직접 연결하여 사용 가능합니다.

 

RGB LED는 Red, Green, Blue LED 각각의 밝기를 조정하여 다양한 색을 표시 할 수 있습니다. 아두이노나 라즈베리파이에서는 PWM(Analog Output) 방식으로 R,G,B의 밝기를 0~255 로 조정할 수 있습니다. 따라서 이론적으로 256 × 256 × 256 수 만큼의 다른 color를 낼 수 있습니다.

 

 

RGB led 모듈도 직접 설계하여 제작한 것을 액츄에이터로 사용했습니다. 기술적인 스펙은 아래와 같습니다.

 

품명 RGB LED
Rated Voltage(V) 12V
표시 색 1600만
소비전류 Red=2V/20mA
GREEN=3.4V/20mA
Blue=3.4V/20mA
품번 IWS-506-RGB-K3

 

참고 자료를 아래에 올립니다.

 

rgb led module

 

가산 혼합의 반대는 감산 혼합

 

 

아래 코드는 순차적인 점등 코드입니다. 

 

- 파일 이름: rgbtest.c

- 소스코드: 할당된 gpio 번호는 예고없이 변경할 수 있습니다.

 

 

RGB LED 테스트 코드

 

#include <signal.h> //Signal 사용 헤더파일
#include <unistd.h>
#include <stdio.h> 
#include <string.h> 
#include <errno.h>
#include <stdlib.h> //exit() 사용 헤더파일

#include <wiringPi.h>

#define RGBLEDPOWER  24 //BCM_GPIO 19

#define RED	8 //27
#define GREEN	7 //28
#define BLUE	9 //29

void sig_handler(int signo); // SIGINT 사용 마지막 종료 함수

int main (void)
{
	signal(SIGINT, (void *)sig_handler);	//시그널 핸들러 함수
	
	if (wiringPiSetup () == -1)
	{
		fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
		return 1 ;
	}
  
	pinMode(RGBLEDPOWER, OUTPUT);
	pinMode(RED, OUTPUT);
	pinMode(GREEN, OUTPUT);
	pinMode(BLUE, OUTPUT);

	int i;

	for (i =0; i<10 ;i++)
	{
 
		digitalWrite(RGBLEDPOWER, 1);

		digitalWrite(RED, 1);
		digitalWrite(BLUE, 0);
		digitalWrite(GREEN, 0);
		
		delay(1000);
		
		digitalWrite(RED, 0);
		digitalWrite(BLUE, 1);
		digitalWrite(GREEN, 0);
		
		delay(1000);
		
		digitalWrite(RED, 0);
		digitalWrite(BLUE, 0);
		digitalWrite(GREEN, 1);
		
		delay(1000);		
	}
	digitalWrite(GREEN, 0);
  return 0 ;
}

void sig_handler(int signo)
{
    printf("process stop\n");
	
	digitalWrite(RED, 0);
	digitalWrite(GREEN, 0);
	digitalWrite(BLUE, 0);
	digitalWrite(RGBLEDPOWER, 0); //Off
	
	exit(0);
}

 

 

아래 rgb led 색이 변화하는 테스트 코드 파일 이름은 allrgbcolor.c 로 한다.

 

컴파일

$gcc –o allrgbcolor allrgbcolor.c –l wiringPi

 

실행

$sudo ./allrgbcolor

 

#include <signal.h> //Signal 사용 헤더파일
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h> //exit() 사용 헤더파일

#include <wiringPi.h>
#include <softPwm.h>

#include <wiringPi.h>

#define RGBLEDPOWER  24 //BCM_GPIO 19
#define RED		7	// BCM_GPIO 16 - OUT
#define GREEN	9	// BCM_GPIO 20 - OUT
#define BLUE	8 // BCM_GPIO 21 - OUT

void Bpluspinmodeset(void);

void setRGB(int r, int g, int b);
void sig_handler(int signo); // SIGINT 핸들러 함수
void  Fade( int fromColor, int toColor, int offColor );

int main (void)
{
	if(wiringPicheck()) printf("Fail");
		
	Bpluspinmodeset();
	
	signal(SIGINT, (void *)sig_handler);
	
	digitalWrite(RGBLEDPOWER, 1);
    
    int i = 0, j = 0, k=0;
	printf("RGB LED Various Color");
 
	softPwmCreate(RED, 0, 255);
	softPwmCreate(GREEN, 0, 255);
	softPwmCreate(BLUE, 0, 255);
	
	while(1)
	{
        // R -> G 불빛 변화
        Fade( RED, GREEN, BLUE );  // R에서 G로 FADE 전환
        
        // G -> B 불빛 변화
        Fade( GREEN, BLUE, RED );  // G에서 B로 FADE 전환
        
        // B -> R 불빛 변화
        Fade( BLUE, RED, GREEN );  // B에서 R로 FADE 전환
	}
  
  return 0 ;
}

int wiringPicheck(void)
{
	if (wiringPiSetup () == -1)
	{
		fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
		return 1 ;
	}
}

void Bpluspinmodeset(void)
{
	pinMode(RGBLEDPOWER, OUTPUT);
	pinMode (RED, OUTPUT);
	pinMode (GREEN, OUTPUT);
	pinMode (BLUE, OUTPUT);	
	
}

////////////////////////////////////////
// fromColor 번호의 핀과 연결된 색상에서
// toColor 번호의 핀과 연결된 색상으로
// 점차 색상을 변화시키는 함수

void  Fade( int fromColor, int toColor, int offColor )
{
  int  color;  // fromColor 빛의 세기
  
  // fromColor -> toColor 빛깔 변화
  
  softPwmWrite( offColor, 0 );  // 먼저 offColor는 처음부터 끔 (OFF)
  
  for( color = 255; color >= 0; color-- )  // 255 에서 0 까지 1씩 감소 
  {
    softPwmWrite( fromColor, color );      // 시작할 때의 색상 ON
    softPwmWrite( toColor, 255 - color );  // 끝날 때의 색상 ON
    delay(30);  // 0.03초 동안 불빛 지속(지연)
  }
}

void sig_handler(int signo) // ctrl-c 로 종료시 실행되는 함수
{
    printf("process stop\n");
	digitalWrite(RED, 0);
	digitalWrite(GREEN, 0);
	digitalWrite(BLUE, 0);
	digitalWrite(RGBLEDPOWER, 0); //Off
	exit(0);
}

 

 

 

 

 

반응형