Embedded Systems

STM32 Sound System

Hands-Free Light Switch โ€” Real-time clap detection with <10ms latency

Overview

A sound-controlled lighting system built on the STM32L476VG Discovery board. The system detects acoustic events (hand claps) and toggles an onboard LED in real-time, demonstrating the integration of analog input processing, digital decision-making, and output control within a single embedded application.

Technical Specifications

Microcontroller

STM32L476VG (ARM Cortex-M4)

ADC Resolution

12-bit (0-4095 range)

Response Time

<10ms

Sensor

Gravity Analog Sound Sensor

Hardware Architecture

The sound sensor produces an analog voltage proportional to sound intensity, connected to PA1 (ADC1_IN6). The STM32 converts this signal using continuous conversion mode with a 47.5-cycle sampling time. When the ADC value exceeds the calibrated threshold of 1000, the system toggles the red LED on PB2.

Pin Connections

  • VCC โ†’ 3.3V Output
  • GND โ†’ Common Ground
  • Signal โ†’ PA1 (ADC1_IN6)
  • LED โ†’ PB2 (GPIO Output)

Calibration

Ambient noise levels were measured in a quiet environment, yielding typical ADC readings of 100-300. A hand clap produced readings between 2500-3800. The threshold was set to 1000 โ€” safely above ambient noise but well below typical clap intensity. An oscilloscope confirmed the sensor returns to baseline within 50-100ms after a sound event.

Software Implementation

The firmware uses STM32 HAL with a polling-based approach inside a while(1) loop. A 250ms software debounce prevents multiple toggles from a single clap event. The ADC operates in continuous conversion mode with right-aligned 12-bit data, ensuring zero wait time for samples.

while (1) {
    if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
        sound_level = HAL_ADC_GetValue(&hadc1);
        if (sound_level > threshold) {
            HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2);
            HAL_Delay(250);  // Debounce
        }
    }
}

Team & Course

Course: CENG 315 โ€” Microprocessors
Instructor: Dr. Arfan Ghani
Team: Alaa Eddin, Ali Alhaeri, Mariam Hemeida, Mustafa Turani, Omar Al Nuaimi, Zaineh Khawaja

Full Report

View the complete technical report below:

If the PDF doesn't load, open it directly โ†—

View on GitHub โ†—