Get tutorials Raspberry Pi Sensor Kit V2.0 for Raspberry Pi B+

Lesson 19 Sound Sensor

Introduction

Sound sensor is a component that receives sound waves and converts them into electrical signal. It detects the sound intensity in ambient environment like a microphone. 


 


Components

- 1 * Raspberry Pi

- 1 * Breadboard

- 4 * Jumper wires (Male to Male, 2 red and 2 black)

- 1 * Network cable (or USB wireless network adapter)

- 1 * PCF8591

- 1 * Sound sensor module

- 1 * 3-Pin anti-reverse cable

- Several Jumper wires (Male to Female)

 

Experimental Principle

The microphone on the sensor module can convert audio signals into electrical signals (analog quantity), then convert analog quantity into digital quantity by PCF8591 and transfer them to MCU.


LM358 is a dual-channel operational amplifier. It contains two independent, high gain, and internally compensated amplifiers, but we will only use one of them in this experiment. The microphone transforms sound signals into electrical signals and then sends out the signals to pin 2 of LM358 and outputs them to pin 1 (that's, pin SIG of the module) via the external circuit. Then use PCF8591 to read analog values.


PCF8591 is an 8-bit resolution, 4-channel A/D,1-channel D/A conversion chip. We connect the output terminal (SIG) to AIN0 of PCF8591 so as to detect the strength of voice signal in a real-time manner.

 

The schematic diagram of the module is as shown below:

 


Experimental Procedures

 Step 1: Build the circuit according to the following method


Raspberry Pi

T-Cobbler

PCF8591 Module

SDA

SDA1

SDA

SCL

SCL1

SCL

3V3

3V3

VCC

GND

GND

GND

 

Sound Sensor Module

T-Cobbler

PCF8591 Module

SIG

*

AIN0

VCC

3V3

VCC

GND

GND

GND


 

 

For C language users:

Step 2: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/C/19_sound_sensor/

Step 3: Compile

gcc sound_sensor.c –lwiringPicd

Step 4: Run

sudo ./a.out

 

For Python users:

Step 2: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/

Step 3: Run

sudo python 19_sound_sensor.py


Now, say something or blow to the microphone, and you can see “Voice In!! ***” printed on the screen.


 

 

C Code



#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>

#define PCF       120

int main (void)
{
	int value;
	int count = 0;
	wiringPiSetup ();
	// Setup pcf8591 on base pin 120, and address 0x48
	pcf8591Setup (PCF, 0x48);
	while(1) // loop forever
	{
		value = analogRead  (PCF + 0);
		//printf("%d\n", value);
		if (value < 50){
			count++;
			printf("Voice In!!  %d\n", count);
		}
	}
	return 0;
}




Python Code



#!/usr/bin/env python
import PCF8591 as ADC
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

def setup():
	ADC.setup(0x48)

def loop():
	count = 0
	while True:
		voiceValue = ADC.read(0)
		if voiceValue:
			print 'Value:', voiceValue
			if voiceValue < 50:
				print "Voice detected! ", count
				count += 1
			time.sleep(0.2)

if __name__ == '__main__':
	try:
		setup()
		loop()
	except KeyboardInterrupt: 
		pass	


 

 

 


Copyright © 2012 - 2016 SunFounder. All Rights Reserved.


Previous chapter: Lesson 18 Temperature Sensor

Next chapter: Lesson 20 Photoresistor Module

SunFounder
Apr 15 2017 at 01:31 am



© Developed by CommerceLab