Lesson 24 Microphone Sensor

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp


There are two kinds of microphone sensor in this kit: microphone sensor and high-sensitive voice sensor (as shown below). The only difference between them is sensitivity. In this experiment, we will take the microphone sensor for example. You may try to apply the other sensor based on what you’ve got during the process.

Both sensors have two outputs:

AO: analog output, to output voltage signals from microphone in a real-time manner

DO: When the sound intensity reaches a certain threshold, the sensor outputs high or low level (you can adjust the threshold by the potentiometer)

 Microphone Sensor                                                                             High-sensitive Voice Sensor


– 1 * Raspberry Pi

– 1 * Breadboard

– 1 * ADC0832

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

– 1 * Microphone sensor module

– 1 * High-Sensitive voice sensor module

– Several jumper wires

Experimental Principle

Microphone can convert audio signal into electrical signal (analog quantity), then convert analog quantity into digital value by ADC and transfer it to MCU to process.

The schematic diagram:

LM393 is a voltage comparator. When the voltage of the in-phase terminal (pin 3) is higher than that of the inverting terminal (pin 2), the output terminal (pin 1) will output high. Otherwise, it outputs low. First, adjust the potentiometer to make the voltage for pin 2 of LM393 less than 3.3V. When there is no voice input, the resistance of the microphone is very large. The voltage for pin 3 of LM393 is close to power supply voltage (3.3V), pin 1 outputs high and the LED is on; when there is voice input, the resistance of the microphone decreases, pin 1 outputs low and the LED is off. And connect pin 1 to IO of the SunFounder Uno board to detect sounds by programming.

ADC0832 is an 8-bit two-channel A/D conversion chip. Connect the output terminal (AO) to CH0 of ADC0832 so as to make real-time detection of the strength of voice signals.

Experimental Procedures

 Step 1: Build the circuit

Connect pin DO on the microphone sensor module to pin GPIO3 on Raspberry Pi; pin AO of the module to CH0 of the ADC0832.

Step 2: Edit code and save (see path/Rpi_SensorKit_code/23_microPhoneSensor/microPhone.c)

Step 3: Compile

              gcc  microPhone.c  -lwiringPi

Step 4: Run


Now, speak or blow to the microphone, and you will see “voice in” and “Current analog value***” printed on the screen.


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

#define     ADC_CS    0
#define     ADC_CLK   1
#define     ADC_DIO   2

#define  MIC_DO_Pin   3

typedef unsigned char uchar;
typedef unsigned int uint;

uchar get_ADC_Result(void)
	uchar i;
	uchar dat1=0, dat2=0;

	digitalWrite(ADC_CS, 0);
	digitalWrite(ADC_DIO,1);	delayMicroseconds(2);
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);

	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);

	digitalWrite(ADC_DIO,0);	delayMicroseconds(2);
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
		digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);

		pinMode(ADC_DIO, INPUT);
		dat1=dat1<<1 | digitalRead(ADC_DIO);
		dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<<i);
		digitalWrite(ADC_CLK,1); 	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);

	return(dat1==dat2) ? dat1 : 0;

void micISR(void)
	uchar analogVal;

	printf("voice in \n");
	pinMode(ADC_DIO, OUTPUT);

	analogVal = get_ADC_Result();
	printf("Current analog : %d\n", analogVal);

int main(void)

	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	if(wiringPiISR(MIC_DO_Pin, INT_EDGE_RISING, &micISR) < 0){
		fprintf(stderr, "Unable to init ISR\n",strerror(errno));	
		return 1;

	pinMode(ADC_CS,  OUTPUT);
	pinMode(ADC_CLK, OUTPUT);

	printf("Please speaking...\n");


	return 0;

Python Code

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


def init():
	GPIO.setup(MIC_DO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def micISR(ev=None):
	print "voice in..."
	analogVal = ADC0832.getResult()
	print 'res = %d' % res

def loop():
	GPIO.add_event_detect(MIC_DO_PIN, GPIO.FALLING, callback=micISR)
	while True:

if __name__ == '__main__':
	except KeyboardInterrupt: 
		print 'The end !'