Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Clap sensor using STM32 MCU

By Usman ali Butt March 5, 2021

Security systems work by measuring different physical parameters. One such parameter is sound. Every moving object produces sound and vibrations. These sounds and vibrations can be sampled, and the object can be identified. Sampling sound waves and training the system to distinguish between objects requires plenty of time.

For this tutorial, let’s take a simple sound sensor and interface it with an STM32 microcontroller. The sound sensor will recognize a clap sound and switches on the LED.

The sensor we are using is the most popular KY-038. On KY-038 sound sensor is placed in a circuit with other electrical components. Namely

  • Differential amplifier
  • Variable resistor (Potentiometer)

Why differential amplifier?

Sound sensor output voltage/signal is much lower than the standard microcontroller operating voltages (3.3 or 5 volts). To read the sound sensor’s output, it must be brought up to 3.3 or 5 volts, depending on the microcontroller operating voltage.

Many variants of KY-038 are available in the market, and various kinds of differential amplifiers are present on them. The most popular differential amplifier onboard is LM-393. Lm-393 is actually a comparator, but on our sensor board, the output voltage is pulled up. Moreover, the sensor module can output a digital as well as an analog signal.

Variable Resistor

A variable resistor is setting 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 output of the sound sensor.

So how does it work? 

A fixed voltage(threshold) is fed to one input of the comparator by a variable resistor. On any disturbance sound sensor outputs a voltage. The comparator compares the voltage threshold vs. disturbance. If disturbance greater than threshold amplify and output 1. If less than no output.

By rotating the knob of the variable resistor, we can adjust the sound detection level.

Project circuit:

Stm32f103 discovery development board is used to test the clap sensor. An led at the output will turn on for some time if clapped near the sensor.

Two GPIO pins of the STM32 microcontroller are required for this purpose. One as input to detect the clap signal from the sensor module and one as 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. Led is pulled high in series with a 550-ohm resistor. Both the led and sensor module can be powered through stm32 discovery power output pins. The onboard regulator of stm32 can manage power for all three components.

Project code

The Code of the project is simple and straightforward. First, enable the clock for port-C of 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 at the output. So be sure to enable clocks for ports under use.

Next, GPIO pins PC8 and PC9 are declared. We need to set all the parameters for each pin. Pin 8 is declared as input, sampling speed is set to 50MHz, at last, no pull-up or down resistor is enabled.

PC9 only difference with respect to PC8 is it’s declared as an output pin. Push up and down resistors can be enabled depending on the circuit requirements. Another option is to use the onboard led of STM32 discovery. A single statement in the above code snap labeled 3 can be uncommented to use the onboard led. Behind the onboard led declaration function same code as written in box 2 is executed to declare the led.

Next comes the while loop. While1 loop executes continuously unless discovery is powered off. In the while loop first, if the statement is checking the status of the sensor output. If it is found high, the control enters if statement body.

Suppose statement body is composed of just two statements. The first statement sets the PC9 or led as high. The second statement is a for loop, which iterates 1000 times. These 1000 iterations produce a delay in milliseconds. This delay is very significant. It enables the person to see the led switching on and then back off visually. Led off statement is outside if statement.

During testing, it was found that it is impossible to see the led turning on and off without for loop delay. Led transition is so fast because of the operating frequency of the stm32.

If you are using onboard led of stm32 discovery, you must uncomment the STM_EVAL_LEDToggle() instruction. LED togging can also be achieved if instead of GPIO_SetBits() directly GPIOC->BSSR and BSR are used. BSR and BSSR are output registers associated with ports of stm32. All the instructions are present in the above code.

Download full code from Github

The above concept can be applied in different applications, such as security systems, opening and closing doors, vehicle start theft, etc. The analog output of the sensor provides precise voltage change on disturbance. The analog feature can be used for precise sound differentiation. Working with analog features requires knowledge of ADC, which we will learn in the upcoming tutorials.

Let’s DIY the Project: Where to Purchase parts?

StM32 Discovery: From Mouser

Sound sensor: From Mouser

LED: From Mouser

Resistor: From Mouser

 

You may also like:


  • Microcontroller Project: STM32 low power modes analysis

  • What are different types of industrial robots?

  • How does LoRa modulation enable long-range communication?

  • What is the role of embedded software in electric vehicles?

  • What are the components of robotic arms and industrial robots?

  • What drone parts you need to build a quadcopter?

Filed Under: Electronic Projects, Microcontroller Projects, STM32

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • CMOS Xtal connection to ST Controller STM32L031K6U6
  • How to evaluate input impedance in HFSS
  • Battery charger with LED
  • pCad DOS update to 2006 or hi-res PDF
  • General purpose CMOS Op Amp and PMOS & NMOS from LTSpice library

RSS Electro-Tech-Online.com Discussions

  • going out on a limb and praying the schematic is correct
  • Easy PC Demo version Schem and Layout program questions
  • nicd charger for li-ion
  • Can't get a small CRT display to focus
  • JBL charge 4 dead motherboard?

Featured -USB Series

  • Controller Chip Selection for Developing USB Enabled Device (Part 6/6)
  • Signal and Encoding of USB System (Part 5/6)
  • USB Requests and Stages of Control Transfer (Part 4/6)
  • USB Descriptors and their Types (Part 3/6)
  • USB Protocol: Types of USB Packets and USB Transfers (Part 2/6)
  • Introduction to USB: Advantages, Disadvantages and Architecture (Part 1/6)

Recent Articles

  • Littelfuse driver achieves less than 1 µA standby current for energy-efficient designs
  • Microchip optimizes power consumption in transceiver-less FPGA design for automotive applications
  • What is an IoT platform and when is one useful?
  • Silanna launches laser driver IC with sub-2 ns FWHM pulse for LiDAR application
  • LEM introduces current sensors with bandwidth up to 2.5 MHz for precision applications

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe