Lesson 19 Sound Sensor

Share for us

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

– 1 * PCF8591

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

– 1 * Sound sensor module

– 1 * 3-Pin anti-reverse cable

– Several Jumper wires (M to F)

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:

Experimental Procedures

 Step 1: Build the circuit

Raspberry PiPCF8591 ModuleSound Sensor Module
SDASDA
SCLSCL
3V3VCCVCC
GNDGNDGND
*AIN0SIG

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