개발자/라즈베리파이4

라즈베리파이 4 조도 센서, 빛 센서 실습

지구빵집 2022. 5. 25. 08:05
반응형

 

 

 

조도센서 (Photo Resistor)는 주변의 밝기를 측정하는 센서입니다. 광에너지 (빛)를 받으면 내부에 움직이는 전자가 발생하여 전도율이 변하는 광전효과를 가지는 소자를 사용합니다. 황화카드뮴을 (Cds)를 소자로 사용한 경우, CdS 센서라고 합니다.

 

센서 파트 넘버: 아두이노 광 포토셀 CdS 조도센서 모듈

 

이미지

 

아두이노 광 포토셀 CdS 조도센서 모듈

 

우선 디지털 출력을 확인하는 연결도와 소프트웨어 코드를 첨부한다.

 

 

조도센서 디지털 출력 연결

 

조도센서의 디지털 값을 출력하므로 결과는 light 혹은 dark를 출력한다.

 

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>


//#define LIGHTSEN_OUT 2  //gpio27 - J13 connect
#define LIGHTSEN_OUT 23  //gpio27 - J13 connect

// -------------------------------------------------------------------------
// main
int main(void) 
{
	// sets up the wiringPi library
	if (wiringPiSetup () < 0) 
	{
		fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
		return 1;
	}
	
	pinMode(LIGHTSEN_OUT, INPUT);


	if ( wiringPiISR (LIGHTSEN_OUT, INT_EDGE_RISING, &myInterrupt) < 0 ) 
	{
		fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
		return 1;
	}

	// display counter value every second.
	while ( 1 ) 
	{
		//printf( "%d\n", eventCounter );
		//eventCounter = 0;
		
		if(digitalRead(LIGHTSEN_OUT) == 0)
			printf("light full ! \n");
		if(digitalRead(LIGHTSEN_OUT) == 1)
			printf("dark \n");		
	
		delay( 200 ); // wait 1 second
	}

	return 0;
}

 

 

다음은 중요하고 약간 어려운 조도센서의 아날로그 출력을 라즈베리파이에서 확인하는 과정이다.

 

연결도를 보기 전에 반드시 알아야 할 것은 라즈베리파이 임베디드 온보드 컴퓨터에는 RTC(리얼타임클럭)과 ADC(아날로그 디지털 변환기)가 없다. 두가지 기능이 필요한 경우 모듈을 사용하든가 따로 설계를 해서 넣어준다. 여기서는 MCP3208 ADC를 SPI 인터페이스로 연결하여 사용해서 연결도는 아래 회로도를 참고하시면 된다. 참고 문서는 아래 링크를 참고한다.

 

 

 

라즈베리파이와 MCP3208 ADC 컨버터 사용하기 - 회로와 소스코드 

 

라즈베리파이와 MCP3208 ADC 컨버터 사용하기 - 회로와 소스코드

라즈베리파이와 MCP3208 ADC 컨버터 사용하기 - 회로와 소스코드 라즈베리파이의 GPIO는 아두이노와는 다르게 디지털 입력만 가능하도록 되어있다. 라즈베리파이는 ADC(Analog Digital Converter)가 내장되

fishpoint.tistory.com

 

 

MCP3208 회로도

 

 

- 파일 이름: motiondetect.c

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

 

* 주의 사항 

 

아날로그 출력 데이터를 얻는 코드

 

#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include <wiringPi.h> 
#include <wiringPiSPI.h> 

#define CS_MCP3208 8 //GPIO 8 
#define SPI_CHANNEL 0 
#define SPI_SPEED 1000000 //1Mhz

// spi communication with Rpi and get sensor data 

int read_mcp3208_adc(unsigned char adcChannel) 
{
	unsigned char buff[3];
	int adcValue = 0;
	
	buff[0] = 0x06 | ((adcChannel & 0x07) >> 2);
	buff[1] = ((adcChannel & 0x07) << 6);
	buff[2] = 0x00;
	
	digitalWrite(CS_MCP3208, 0);
	wiringPiSPIDataRW(SPI_CHANNEL, buff, 3);
	
	buff[1] = 0x0f & buff[1];
	adcValue = (buff[1] << 8 ) | buff[2];
	
	digitalWrite(CS_MCP3208, 1);
	
	return adcValue;
}

int main(void) {

	unsigned char adcChannel_light = 0;

	int adcValue_light = 0;

	float vout_light;
	float vout_oftemp;
	float percentrh = 0;
	float supsiondo = 0;
	
	printf("start");

	
	if(wiringPiSetupGpio() == -1)
	{
		fprintf(stdout, "Unable to start wiringPi :%s\n", strerror(errno));
		return 1;
	}
	
	if(wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1)
	{
		fprintf(stdout, "wiringPiSPISetup Failed :%s\n", strerror(errno));
		return 1;
	}
	
	pinMode(CS_MCP3208, OUTPUT);
	
	while(1)
	{
		adcValue_light = read_mcp3208_adc(adcChannel_light);
		
		//printf("Humiity = %u temparature = %u\n", adcValue_humi, adcValue_temp);
		printf("light sensor = %u\n", adcValue_light);
		
		delay(100);
	}
	return 0;
}

 

고생하셨습니다.

 

오눌 아침 작성한 꼭 지키기

 

 

반응형