Security systems work by measuring different physical parameters. One of these parameters is sound. Every moving object produces sounds and vibrations. These sounds and vibrations can be sampled and the object can be identified. Sampling sound waves and training the system to distinguish objects requires a lot of time.
For this tutorial, we're going to take a simple sound sensor and interface it with an STM32 microcontroller. The sound sensor will recognize a clapping sound and turn on the LED.
The sensor we are using is the most popular KY-038. In the KY-038, the sound sensor is placed in a circuit with other electrical components. Namely
- Differential amplifier
- Variable resistor (potentiometer)
Why differential amplifier?
The output voltage/signal of the sound sensor is much lower than the standard operating voltages of the microcontroller (3.3 or 5 volts). To read the sound sensor output, it must be increased to 3.3 or 5 volts, depending on the operating voltage of the microcontroller.
Many variants of KY-038 are available in the market and various types of differential amplifiers are present in them. The most popular integrated differential amplifier is the LM-393. Lm-393 is actually a comparator, but on our sensor board the output voltage increases. Furthermore, the sensor module can output a digital and analog signal.
Variable resistor
A variable resistor sets the threshold for sound recognition. The output of the variable resistor is input to the differential amplifier. The other input of the differential amplifier is the sound sensor output.
So how does this work?
A fixed voltage (threshold) is fed to a comparator input by a variable resistor. In any disturbance, the sound sensor emits a voltage. The comparator compares threshold voltage versus disturbance. If the disturbance is greater than the threshold, amplify and output 1. If it is less than no output.
By turning the variable resistor knob, we can adjust the sound detection level.
Project circuit:
The Stm32f103 discovery development board is used to test the clap sensor. An LED at the output will light up for a while if pressed close to the sensor.
Two GPIO pins of the STM32 microcontroller are required for this purpose. One as an input to detect the clap signal from the sensor module and the other as an output to turn on the LED. The circuit diagram of the project is below.

I used PC8 as the input pin and PC9 as the output pin. The LED is pulled high in series with a 550 ohm resistor. Both the LED module and the sensor can be powered via the STM32 discovery power output pins. The STM32's integrated regulator can manage the power of all three components.
Project code
The project code is simple and straightforward. First, enable the clock for port C of the stm32. We must enable the clock because we are using two pins of port C in our application. If this step is missed in any application, nothing will be seen in the output. So be sure to enable clocks for the ports in use.
Next, GPIO pins PC8 and PC9 are declared. We need to define all parameters for each pin. Pin 8 is declared as input, the sampling speed is set to 50 MHz, and finally no pull-up or down resistors are enabled.
The only difference between PC9 and PC8 is that it is declared as an output pin. Push up and down resistors can be enabled depending on the circuit requirements.
Another option is to use the integrated STM32 discovery LED. A single instruction in the code above labeled 3 can be uncommented to use the integrated LED. Behind the built-in LED declaration function, the same code written in box 2 is executed to declare the LED.
Next comes the while loop. The While1 loop runs continuously unless discovery is turned off. First in the while loop if the instruction is checking the sensor output status. If found high, control enters the body of the if statement.
Suppose the statement body consists of just two statements. The first instruction sets PC9 or led to high. The second statement is a for loop, which iterates 1000 times. These 1,000 iterations produce a delay in milliseconds. This delay is very significant. It allows the person to see the LED turning on and then turning off visually. The Led off statement is outside the if statement.
During testing, it was found that it is impossible to see the LED turning on and off without delay in the loop. The LED transition is so fast because of the operating frequency of the stm32.

If you are using the built-in stm32 discovery led, you must uncomment the STM_EVAL_LEDToggle statement. LED switching can also be achieved if instead of GPIO_SetBits directly GPIOC->BSSR and BSR are used. BSR and BSSR are output registers associated with stm32 ports. All instructions are present in the code above.
Download full code from Github
The above concept can be applied to various applications, such as security systems, opening and closing doors, vehicle start-up theft, etc. The sensor's analog output provides accurate voltage change in the event of a disturbance. The analog feature can be used for accurate sound differentiation. Working with analog resources requires knowledge of ADC, which we will learn in the next tutorials.
Let's do the DIY project: Where to buy parts?
StM32 Discovery: from Mouser
Sound sensor: From Mouser
LED: From Mouser
Resistor: From Mouser