개발자/라즈베리파이4

라즈베리파이4 소리 센서, 사운드 센서 실습

지구빵집 2022. 6. 7. 08:12
반응형

 

 

 

사운드 센서(Sound Sensor)는 센서 주변에서 발생되는 소리를 그림의 (a)부분 마이크로 모아, 소리의 크기를 증폭 시켜 출력합니다. 만약 원하는 대상의 소리가 잘 안들리시면 그림의 (b)부분이 가변저항으로서 가변저항을 통해 감도를 통해 조절 할 수 있습니다. 

 

이미지 출처 https://wikidocs.net/91343

 

아래 그림은 출력 단자가 D0와 A0즉 디지털 출력단자와 아날로그 출력단자 두개로 나누어져 있는 경우로 소리의 세기에 따라 각각 다른 동작을 할 수가 있다. 이러한 센서들이 작동하는 원리는 공기의 진동을 통해서 전달되는 파동을 통해서 신호를 만들어 내는 것이다. 즉 우리의 귀가 듣는 것과 아주 비슷하다. 이러한 진동을 전기신호로 바뀌어 아두이노에 전달하게 되면 소리가 신호로 바뀌게 된다.  

 

소리센서

 

센서 파트 넘버: 아두이노 사운드 센서모듈 [SZH-EK033]

 

센서 특징

 

-주변의 소리 강도를 감지하기 위해 사용

-환경에 민감하며, 일반적으로 주변 사운드의 강도를 감지하는 데 사용

-설정된 소리 임계값에 도달할경우 출력핀에서 하이 신호 출력, 도달하지 못할경우 로우 신호 출력

-보드에 내장된 가변저항을 이용해 소리 설정 임계값 설정 가능 

 

전기적 특징

 

-가변저항을 이용한 조절 가능한 소리 감도

-작동 전압 : 3.3V ~ 5V

-출력 형태 : 디지털 신호 출력(로우 및 하이 신호)

-소형 PCB 크기 : 3.2cm*1.7cm 

 

연결도

 

 

- 파일 이름: sounddetect.c

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

 

* 주의 사항

 

사운드 센서 출력을 폴링 방식으로 얻는 코드

 

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

// Use GPIO Pin 7, which is Pin 11 for wiringPi library

#define SOUND_IN 6  //gpio27

unsigned char soundFlag = 0;
// -------------------------------------------------------------------------
// myInterrupt:  called every time an event occurs
void myInterrupt(void) {
	soundFlag = 1;

}

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

	// set Pin 17/0 generate an interrupt on high-to-low transitions
	// and attach myInterrupt() to the interrupt
	if ( wiringPiISR (SOUND_IN, INT_EDGE_RISING, &myInterrupt) < 0 ) {
		fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
		return 1;
	}
	
	while(1)
	{
		if(soundFlag == 1){
			printf( "SoundDetect \n");
			soundFlag = 0;
		}
		else
		{
			printf("No Sound \n");
			soundFlag = 0;
		}
		delay(100);
	}
	return 0;
}

 

 

 

 

사운드 센서 아날로그 신호를 얻는 코드를 보기 위해서는 우선 라즈베리파이의 ADC 데이터를 얻는 포스팅을 참고하세요.

 

https://fishpoint.tistory.com/2050

 

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

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

fishpoint.tistory.com

 

#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_sound = 3;

	int adcValue_sound = 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_sound = read_mcp3208_adc(adcChannel_sound);
		
		printf("sound = %u\n", adcValue_sound);
		
		delay(100);
	}
	return 0;
}

 

 

 

반응형