본문 바로가기

개발자/라즈베리파이4

라즈베리파이4 rgb led 코드

반응형

 

 

 

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);
}

 

 

 

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

당신의 성공을 위해 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩