26. Chapter High-sensitivity microphone sensor

In this chapter, we will learn how to use High-sensitivity microphone sensor.

26.1. Project High-sensitivity microphone sensor and LED

This project will use a high-sensitivity microphone sensor to make a sound-controlled light.

26.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

Jumper Wires x6

jumper-wire

High-sensitivity microphone sensor x1

microphone

LED x1

red-led

Resistor 220Ω x1

res-220R

26.1.2. Component knowledge

26.1.2.1. High-sensitivity microphone sensor

The high-sensitivity microphone sensor module is a component that accepts sound waves and converts them into electrical signals, which can detect the sound intensity in the surrounding environment.

When using it, it should be noted that this sensor can only identify the presence or absence of sound (according to the vibration principle), but cannot identify the size of the sound or the sound of a specific frequency.

This module has 4 pins: digital output (DO), analog output (AO), power supply positive pin and power supply negative pin. AO can output the voltage signal of the microphone in real time. When the ambient sound intensity does not reach the set threshold, the DO outputs a low-level signal, and when the ambient sound intensity exceeds the set threshold, it outputs a high-level signal, and the sensitivity can be adjusted by a potentiometer. When in use, adjust the potentiometer to make the sensitivity to sound reach a more appropriate value, and then read the digital output signal of the module through a pin on the development board. You can speak to the sensor. When the sensor detects a speaking sound, the DO pin outputs a high level; when the sensor does not detect a speaking sound, the DO pin outputs a low level.

Below is the pinout of the high-sensitivity microphone sensor.

Pin description:

symbol

Function

DO

Digital signal output

VCC

Power supply pin, +3.3V~5.0V

GND

GND

AO

Analog signal output

Since the default sensitivity of the high-sensitivity microphone sensor is high, the two LED lights on the module are lit up after power-on, and the sensitivity should be adjusted to an appropriate value at this time. When the potentiometer is adjusted clockwise, the module identification sensitivity increases; When counterclockwise adjustment potentiometer, module recognition sensitivity decreases. Please adjust the potentiometer before using the module to make its sensitivity reach the appropriate value. Under normal circumstances, you need counterclockwise rotation of the potentiometer, so that the output of the module LED off, when the sensitivity is low can be appropriate clockwise adjustment of the potentiometer, please ensure that your sensor output LED is extinguished when energized, in order to identify the sound.

Please do not use voltage beyond the power supply range to avoid damage to the high-sensitivity microphone sensor.

26.1.3. Circuit

Schematic diagram

microphone_Sc

Hardware connection. If you need any support,please feel free to contact us via:

support@freenove.com

microphone_Fr

26.1.4. Code

26.1.4.1. C Code VoiceLamp

First observe the project result, and then learn about the code in detail.

Hint

If you have any concerns, please contact us via: support@freenove.com

  1. Use cd command to enter 26.1.1_VoiceLamp directory of C code.

$ cd ~/Freenove_Kit/Code/C_Code/26.1.1_VoiceLamp
  1. Use following command to compile VoiceLamp.c and generate executable file VoiceLamp.

$ gcc VoiceLamp.c -o VoiceLamp -lwiringPi
  1. Run the generated file VoiceLamp

$ sudo ./VoiceLamp

The following is the program code:

 1/**********************************************************************
 2* Filename    : VoiceLamp.c
 3* Description : Make sound control lamp with high-sensitivity microphone sensor.  
 4* Author      : www.freenove.com
 5* modification: 2022/4/20
 6**********************************************************************/
 7#include <wiringPi.h>
 8#include <stdio.h>
 9#include <stdlib.h>
10#define ledPin    0  	    //define the ledPin
11#define sensorPin 1		//define the sensorPin
12
13void  main(void)
14{
15    printf("Program is starting ... \n");
16    wiringPiSetup();	//Initialize wiringPi.	
17    pinMode(ledPin, OUTPUT); //Set ledPin to output
18    pinMode(sensorPin, INPUT);//Set sensorPin to input
19    while(1){
20        if(digitalRead(sensorPin) == HIGH){ // The sensor is blocked 
21            digitalWrite(ledPin, HIGH);  //Make GPIO output HIGH level
22            delay(5000);
23            digitalWrite(ledPin, LOW);
24            printf("led turned on >>>\n");    //Output information on terminal
25        }
26        else {							// The sensor is not blocked
27            digitalWrite(ledPin, LOW);  //Make GPIO output LOW level
28            printf("led turned off <<<\n");//Output information on terminal
29        }
30    }
31}

Read the signal pin of the high-sensitivity microphone sensor, and determine whether the state of the sensor is high level. If it is high level, the LED will continue to turn on for 5 seconds.

 1if(digitalRead(sensorPin) == HIGH){ // The sensor is blocked 
 2    digitalWrite(ledPin, HIGH);  //Make GPIO output HIGH level
 3    delay(5000);
 4    digitalWrite(ledPin, LOW);
 5    printf("led turned on >>>\n");    //Output information on terminal
 6}
 7else {							// The sensor is not blocked
 8    digitalWrite(ledPin, LOW);  //Make GPIO output LOW level
 9    printf("led turned off <<<\n");//Output information on terminal
10}