Sound Sensor Library for Proteus V3.0: two Arduino UNOs with blue KY-038 sound sensors, the Sound Advance pop-up at MUSIC 80 dB with P-P 12 and D0 LOW, and the TEP Serial Monitor Simple interface printing level lines with lamp ON

Sound Sensor Library for Proteus V3.0 (KY-038 + Arduino)

2.5K Views
40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the new Sound Sensor Library for Proteus V3.0. The KY-038 is the small blue sound sensor module with a microphone, a sensitivity pot and an LM393 comparator. Its A0 output carries the microphone's sound wave, and its D0 output pulses when a sound is loud enough. With this Sound Sensor Library for Proteus, you pick the sound, its distance and the pot, and your Arduino reads A0 and D0 exactly as it would on a real board.

We shared earlier versions of a sound sensor library before; version 3.0 is a new model with its own parts and files. You get two devices: Sound Simple with a scene panel on the schematic, and Sound Advance with a pop-up window and four measured sound test tools. The demo is a clap switch on two Arduino UNOs, shown in the compact Simple interface of our TEP Serial Monitor.

NOTICE: This library is very special to our team. Our KY-038 model is a real analog part in Proteus's SPICE simulator, and every number on its panels is measured from the A0 and D0 node voltages while your Arduino runs. It took our team a lot of hard work, many test runs and many design changes. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below or in the Sound Sensor Library board of our forum. And if our free libraries help you, you can buy us a coffee. So, let's get started with the Sound Sensor Library for Proteus:

Sound Sensor Library for Proteus V3.0: two Arduino UNOs with blue KY-038 sound sensors, the Sound Advance pop-up at MUSIC 80 dB with P-P 12 and D0 LOW, and the TEP Serial Monitor Simple interface printing level lines with lamp ON
Figure: The clap switch demo in Proteus 8.5: NODE 1 (Simple) and NODE 2 (Advance) with both green lamps lit, U2's Sound page playing MUSIC (base 80 dB, now 75 dB), and U4 printing "sound peak-to-peak: 13" once the music started.

What is the KY-038 Sound Sensor?

The KY-038 is a small sound sensor module, about 35 x 15 mm, on a blue board. At one end sits an electret microphone, a silver capsule with a black mesh. The rest of the board is a simple circuit around the LM393 comparator: a blue multi-turn trimmer (the sensitivity pot, marked 104), a PWR LED, a D0 LED and a 4-pin header printed A0 G + D0. Makers use it for clap switches, noise alarms and sound-activated projects.

The KY-038 is not a sound level meter. A0 is the raw microphone signal, without an amplifier, and the board has no dB scale. It hears loud, close sounds such as a clap or an alarm clock well, and normal talking only as a small wiggle. The KY-037 is the same board with a bigger microphone.

The KY-038 at a Glance

KY-038 in numbers
FeatureValue
MicrophoneElectret capsule
ComparatorLM393, drives D0
OutputsA0: analog, the microphone signal on a DC level; D0: digital, pulses on a loud sound
SensitivityA blue multi-turn trimmer pot (marked 104)
LEDs on the real boardPWR and D0 (both red)
HeaderA0, G, +, D0 (four pins)
Supply3.3 to 5 V
BoardAbout 35 x 15 mm, portrait
Typical usesClap switches, noise detection, sound triggers

Sound Sensor Terms You Will See in This Article

Sound sensor terms
TermMeaning
A0The analog output: the microphone's sound wave on a DC level
D0The digital output of the LM393: HIGH while the wave dips below VCC/2
ADC countWhat analogRead() returns: 0 to 1023 at 5 V
Quiet levelA0 in a quiet room, set by the pot (ADC 530 = 2.59 V by default)
VCC/2The comparator's reference: 2.50 V at a 5 V supply
MarginHow far the quiet level sits above VCC/2 (18.5 counts = 90 mV by default)
Peak-to-peak (P-P)How far A0 swings: the highest minus the lowest reading, in ADC counts
dBThe sound level in decibels; in our model, 6 dB louder = twice the swing
BurstThe train of D0 pulses that one loud sound makes
DebounceIgnoring D0 for a while after the first pulse, so one clap counts once

How the KY-038 Turns Sound Into A0 and D0

A0: the Sound Wave on a DC Level

The microphone signal is a wave on a DC level that the pot sets. In a quiet room A0 sits at about 2.59 V, which an Arduino UNO reads as 530; a sound moves it up and down around that level. So one analogRead() tells you little: read A0 for about 50 ms and take the peak-to-peak, as our demo does. A clap 10 cm away swings A0 by more than 100 counts; normal talking by only a few.

D0: Pulses, Not a Level

The LM393 compares A0 with half the supply, VCC/2 (2.50 V at 5 V). D0 goes HIGH while the wave dips below VCC/2 and LOW again when it comes back above. The board has no hysteresis, so during a loud sound D0 does not stay HIGH: it pulses, once per cycle of the sound, and the D0 LED flickers. A single clap close by gives a burst of 9 short pulses in about 18 ms. That is why a clap-switch sketch takes the first pulse as "a clap" and then ignores D0 for a while.

The Pot Sets the Sensitivity

The pot sets the quiet level of A0. Its distance above VCC/2, the margin, is the swing a sound needs before its dips reach VCC/2: by default 18.5 counts (about 90 mV). Turn the pot towards VCC/2 (POT - on our panel) and a quieter sound trips D0. Turn it too far, below VCC/2, and D0 is HIGH all the time: the well-known "D0 LED always on" problem.

Louder Means a Bigger Swing: the dB Rule in Our Model

Our model turns the sound level at the microphone into the size of the swing at A0:

peak swing (ADC counts) = 120 x 10^((dB - 100) / 20)

So 100 dB gives a peak of 120 counts (240 peak-to-peak), and every 6 dB louder doubles the swing. D0 starts to pulse when the peak is larger than the margin. With 18.5 counts, that is at 100 + 20 log(18.5 / 120) = 83.8 dB for a steady tone. The dB values are typical sound levels, not a calibration: no data sheet gives the board's dB scale.

The Sounds in Our Scene

The sounds of our scene (CLAP and DOUBLE CLAP play on top of the chosen source)
SoundLevel at 10 cmWhat it isWith the default pot
Quiet room35 dBSilence, with a 1-count noise floorD0 stays LOW
Talking68 dBPhrases of syllables, 150 to 600 HzNever trips D0
Music80 dBA chord with a beatBelow the threshold; needs POT 515 or a higher level
Alarm90 dB880 Hz beeps, 0.25 s on and 0.25 s offTrips D0 at NEAR only
Clap100 dBAn 80 ms burst of 410 to 1230 HzHeard at NEAR and MID, not at FAR
Double clap100 dBTwo claps 0.35 s apartAs the clap

The distance follows the inverse distance law: MID 25 cm is 8 dB and FAR 60 cm 16 dB quieter than NEAR 10 cm. A clap at FAR is too quiet for the default pot; click POT - three times (quiet level 515) and it is heard. In short: the KY-038 hears claps and alarms close by, not speech across the room.

What's New in Sound Sensor Library for Proteus V3.0

TEP published earlier versions of a sound sensor library for Proteus. Version 3.0 is a new model with its own parts and files (TEPSOUND.LIB and TEPSOUND.DLL). Here is what it brings:

  • A real analog part: A0 (the sound wave, 4.7 k output resistance) and D0 (the LM393's open collector with a 10 k pull-up) are nodes of Proteus's SPICE simulator.
  • Two devices: Sound Simple (SOUNDTEP) with the scene panel on the sheet, and Sound Advance (SOUNDADVTEP) with a pop-up window.
  • A live board with sound rings, a SOUND window, a CLAP LED and a SOUND LEVEL bar (TEP additions).
  • Everything shown is measured from the A0 and D0 node voltages of the running circuit.
  • Six sounds, three distances and the pot, plus a level slider, an event log and a 20-second chart on the Advance.
  • Four measured test tools: Level sweep, D0 threshold, Clap timing and Scope capture.
  • A two-UNO clap switch demo with the TEP Serial Monitor and its own commands.
  • A light package: about 1.47 MB, without the C++ source code.

Sound Sensor Library for Proteus: Simple vs Advance

Both devices run the same model with the same sound, distance and pot properties; only the panel differs:

Sound Simple vs Sound Advance
FeatureSimpleAdvance
Full KY-038 model (A0 sound wave, D0 pulses, the pot, six sounds, three distances)✔✔
Blue KY-038 board: sound rings, SOUND window, PWR / CLAP / D0 LEDs, SOUND LEVEL bar✔✔
Scene panel on the schematic (scene, 100 ms scope, readouts, buttons)✔✘
Pop-up window you can move, resize and minimise✘✔
Sound page: level slider 40 to 110 dB, the board's measured values, 20 s chart, pin / event log✘✔
Level sweep, D0 threshold, Clap timing and Scope capture✘✔
TEP Dark / Light theme, text size, Help page with Copy diagnostics✘✔
PANEL property (the panel open or closed at the start)✔✘

Choose Simple to keep the scene beside your circuit, and Advance when your sheet is full or you want hard numbers about your sketch. The demo has one of each.

Download Sound Sensor Library for Proteus

Click the button below to download Sound-Sensor-Library-for-Proteus-v3.0.zip (about 1.47 MB, without the C++ source code):

Sound Sensor Library for Proteus V3.0
  • README.txt: a detailed guide to the files, the wiring, the model, the test tools and real hardware.
  • Proteus Library Files: TEPSOUND.LIB (both devices), TEPSERIALMON.LIB and our Arduino UNO library ArduinoV3TEP.LIB / ArduinoV3TEP.IDX.
  • Proteus Model Files: TEPSOUND.DLL and TEPSERIALMON.DLL.
  • Proteus Simulation: Sound-Sensor-ArduinoUnoV3.pdsprj, Clap_Switch.hex (both UNOs run it) and copies of both DLLs.
  • Arduino Code: Clap_Switch.ino, the demo sketch. It needs no extra Arduino library.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install Sound Sensor Library for Proteus

  1. Close Proteus and extract the whole zip file.
  2. Copy the four files from Proteus Library Files into the LIBRARY folder, usually C:\Program Files (x86)\Labcenter Electronics\Proteus 8 Professional\LIBRARY (on some PCs C:\ProgramData\Labcenter Electronics\Proteus 8 Professional\LIBRARY; use the folder that already holds Proteus's own .LIB files).
  3. Copy TEPSOUND.DLL and TEPSERIALMON.DLL into the MODELS folder.
  4. If a TEPSOUND.IDX is in the LIBRARY folder, delete it.
  5. Start Proteus, press P and search for Sound. You get the Simple and the Advance device (category Sensors > Sound).

Tested in Proteus 8.5. Proteus 7 is not supported. The demo project runs as it is when Clap_Switch.hex and the two DLLs stay beside it.

The KY-038 Board in Proteus

We drew the sensor as a clean TEP board in the blue of the real module and in its real portrait shape: the silver microphone with its black mesh at the top, the "KY-038" print, the blue trimmer marked 104 with its brass screw, a mounting hole, the LM393, the LEDs and the header A0 G + D0. While the simulation runs, the screw turns with the pot. Here is U1, the Simple device, in the quiet room, with its scene panel beside it:

KY-038 Simple board in Proteus in a quiet room: the blue sound sensor with its microphone, pot and LM393, the SOUND window with a moon, and the KY-038 SOUND SCENE panel with QUIET - A0 AT THE POT LEVEL, D0 LOW and the 100 ms scope
Figure: U1 (Simple) at the start: QUIET ROOM at NEAR 10 cm, SOUND LEVEL 35 dB, A0 2.59 V, ADC 531, D0 LOW, CLAPS 0; the scope trace sits just above the red VCC/2 line.

The SOUND Window and the Sound Rings

Above the board, the SOUND window shows what the microphone hears: the source (a moon, a person talking, a loudspeaker, an alarm clock or clapping hands), its sound waves and a measured 60 ms mini scope of A0, with a summary line, here "QUIET ROOM A0 530 D0 LOW". The sound rings around the microphone turn blue for a sound below the trip level, orange while D0 pulses, and red for a clap that was heard.

Pinout

The four pins keep the order of the real header, A0 G + D0:

KY-038 pins in Proteus (A0 GND VCC D0)
PinName in ProteusWhat it doesDemo connection
A0A0Analog output: the microphone signal on the quiet levelArduino A0
GGNDGroundGND
+VCCPower, 3.3 to 5 V+5V terminal
D0D0Digital output: HIGH pulses while the wave dips below VCC/2Arduino D2

LEDs and Indicators

Board animations (the same on both devices)
IndicatorWhat it shows
PWR LED (red)Lit while the board has power
D0 LED (red)Lit while D0 is HIGH: it flickers with the pulses, and stays on when the pot is turned too far
CLAP LEDLit for 0.3 s after a clap that tripped D0
SOUND LEVEL bar (P-P)The peak-to-peak of A0 over the last 50 ms in 10 segments from QUIET to LOUD, each segment for twice the swing of the one before, with the pot's trip mark in red
Sound ringsBlue: a sound below the trip level; orange: D0 pulses; red: a clap that was heard
SOUND windowThe source, its waves, a 60 ms mini scope of A0 and a summary line
PANEL / OPEN PANEL, SIMPLE / ADVANCEThe panel button and the device badge

The PWR and D0 LEDs are the real module's own. The CLAP LED, the SOUND LEVEL bar, the sound rings and the SOUND window are TEP additions; the real board has none of them.

Component Properties

Sound sensor properties (double-click the sensor > Edit Properties)
PropertyMeaningDefault
SOURCEThe sound at the start: QUIET, TALKING, MUSIC or ALARMQUIET
DISTANCENEAR, MID or FAR, or a distance in cm (below 17 is NEAR, below 40 is MID, else FAR)NEAR
POTThe quiet A0 level in ADC counts, 490 to 600530
PANELSimple only: the panel at the start, OPEN or CLOSEDOPEN

Every Run starts again from these properties; the panel's clicks last until the simulation stops. The panel style comes from the device (Simple or Advance), not from a property.

Sound Simple: The Scene Panel on the Sheet

Beside the Simple board sits the KY-038 SOUND SCENE panel (on the right of the image above):

The Sound Simple scene panel
PartWhat it does
Header, red XKY-038 SOUND SCENE; the X closes the panel
BannerWhat the board does now: QUIET, SOUND BELOW THE THRESHOLD, SOUND DETECTED, CLAP!, POT TOO FAR or NO POWER
SceneThe source, its sound waves and a small KY-038 on a stand, on a floor with a 60 / 25 / 10 cm ruler
Scope "A0 - LAST 100 ms"The min and max of the A0 node in every millisecond, as Proteus solved it, with the red VCC/2 line and a D0 lane
ReadoutsSOUND LEVEL, A0, ADC, D0 and CLAPS
SOUND SOURCE rowQUIET ROOM, TALKING, MUSIC, CLAP, DOUBLE CLAP and ALARM; CLAP and DOUBLE CLAP add claps on top of the source
DISTANCE rowNEAR 10 cm, MID 25 cm and FAR 60 cm
SENSITIVITY POTPOT - and POT + in steps of 5, with the quiet level and the margin between them

In the start image, the green banner reads QUIET - A0 AT THE POT LEVEL, D0 LOW, and the pot box says QUIET A0 530 (2.59 V) MARGIN +18.5. The scope shows the real A0 node, exactly what your Arduino samples. The ADC readout is the UNO's conversion of A0 at this moment (531), while the sketch's 50 ms mean says 530.

MUSIC: a Sound Below the Threshold

KY-038 Simple board in Proteus with MUSIC: a loudspeaker in the scene, blue sound rings, SOUND BELOW THE THRESHOLD - D0 STAYS LOW, SOUND LEVEL 78 dB, A0 2.58 V, ADC 528 and D0 LOW
Figure: MUSIC at NEAR: the window says "MUSIC 80 dB P-P 12 D0 LOW", the panel 78 dB, ADC 528 and D0 LOW.

Click MUSIC: a loudspeaker plays a chord with a beat, the rings turn blue, and three segments of the SOUND LEVEL bar light, below the red trip mark. The banner says SOUND BELOW THE THRESHOLD - D0 STAYS LOW: on the scope, the music is a small wave whose dips never reach the red VCC/2 line. The window shows a peak-to-peak of 12 counts; the SOUND LEVEL readout shows the loudest level of the last 100 ms, 78 dB. Your Arduino sees the swing on A0, but D0 never reacts; click POT - three times and it does.

ALARM: D0 Pulses With Every Beep

KY-038 Simple board in Proteus with the ALARM preset: an alarm clock, orange sound rings, the D0 LED lit, SOUND DETECTED - D0 PULSES HIGH, D0 LED FLASHES, SOUND LEVEL 90 dB, ADC 497 and D0 PULSES
Figure: ALARM at NEAR: "ALARM 90 dB P-P 77 D0 PULSES"; the scope dips below the red VCC/2 line and the D0 lane is orange.

Click ALARM: an alarm clock beeps at 880 Hz, a quarter second on and a quarter second off. At 90 dB the swing is 77 counts peak-to-peak, its dips go below VCC/2, and the banner turns orange: SOUND DETECTED - D0 PULSES HIGH, D0 LED FLASHES. The rings turn orange, the D0 LED lights, five segments of the bar light (the one past the trip mark in orange) and the D0 readout says PULSES. The scope shows a dense band of the wave across the red line, with the D0 lane orange under it.

CLAPS stays 0, because the panel counts only claps. Your sketch cannot tell the difference: every beep makes a burst of D0 pulses, just like a clap, so the demo toggles its lamp with every second beep, as the results below show.

Closing the Panel

Click the red X: the board button says PANEL: CLOSED, its dot turns grey, and the sensor keeps running. Click PANEL to bring the panel back. Here, U1 is back in the quiet room:

KY-038 Simple board in Proteus with the scene panel closed: PANEL: CLOSED with a grey dot, the PWR LED lit, the SOUND window still showing QUIET ROOM A0 530 D0 LOW, and the SIMPLE badge
Figure: The panel closed: PANEL: CLOSED; the SOUND window still says "QUIET ROOM A0 530 D0 LOW".

Sound Advance: The Pop-Up Sound Window

The Advance device keeps only the board on the schematic. Here is U2:

KY-038 Advance board in Proteus: the blue sound sensor U2 SOUNDADVTEP with the SOUND window in the quiet room, the PWR LED lit, the OPEN PANEL button and the ADVANCE badge
Figure: U2 (Advance) in the quiet room, with OPEN PANEL and the ADVANCE badge.

At Run, the "TEP Sound Sensor Advance" pop-up opens with the look of our TEP Serial Monitor and no Windows title bar: move it by its header, resize or minimise it, and reopen it with OPEN PANEL. The header shows the state (for example U2 · MUSIC 80 dB), the Sound and Test tabs and the palette (theme), gear (Settings) and ? (Help) icons; chips below it show the peak-to-peak, D0, the pot and the claps heard.

The Sound Page

Sound page of the TEP Sound Sensor Advance pop-up in Proteus: the live scene with a loudspeaker and the A0 scope, SET THE SOUND with Music selected and the level slider at 80 dB, WHAT THE BOARD DOES (MEASURED), the 20 s chart and the pin / event log
Figure: The Sound page at MUSIC: base 80 dB, "MUSIC now 79 dB", peak-to-peak 18 and "LOW - quiet (0.015 V)".
  • THE SOUND - LIVE: the same scene as the Simple panel, with the 100 ms scope of A0.
  • SET THE SOUND: the four sources, Clap and Double clap with the claps heard (4 of 5 here), a level slider from 40 to 110 dB, the distance and the pot (- 5 / + 5).
  • WHAT THE BOARD DOES (MEASURED): the supply, A0 now (2.595 V, ADC 531), the A0 level (530) and peak-to-peak (18) by the demo's 50 ms formula, D0 (LOW, 0.015 V), the last D0 burst, the LEDs, the VCC/2 reference with the margin (+90 mV) and the trip level of this pot, "D0 pulses from 83.8 dB (steady tone)".
  • LIVE - THE LAST 20 SECONDS: A0's min and max per 100 ms and the D0 pulses.
  • PIN / EVENT LOG (NEWEST FIRST): power, scene changes, D0 bursts, claps and the test tools.

The slider keeps the base level of the source (80 dB) and an amber marker shows the level now: music has a beat, so the line says "MUSIC now 79 dB". The newest D0 burst in the log, "46 pulses in 45.3 ms, A0 down to 2.40 V (ADC 492)", came from the Scope capture tool's 1 kHz tone.

A Double Clap on the Sound Page

Sound page of the Sound Advance pop-up after a double clap: 2 of 2 heard, last D0 burst 9 pulses 18.1 ms min 464, two spikes on the 20 s chart and an event log with two D0 bursts of 9 pulses in 18.1 ms
Figure: DOUBLE CLAP at NEAR in the quiet room: two bursts of 9 pulses in 18.1 ms, 0.35 s apart, A0 down to ADC 464.

In the quiet room, click Double clap: two claps at 100 dB, far above the 83.8 dB this pot needs, and the page shows 2 of 2 heard. Read the event log from the bottom up:

  • 0.000 s: "Run started: QUIET ROOM NEAR, pot 530 (margin +18.5)" and "Power on: VCC 5.00 V - the PWR LED lights".
  • 9.700 s: "DOUBLE CLAP at NEAR: 100 dB at the mic - loud enough to trip D0".
  • 9.702 s and 10.052 s: "D0 burst: 9 pulses in 18.1 ms", with A0 down to 2.26 V and 2.27 V (ADC 464).

So a clap is not one D0 pulse but 9 pulses in 18.1 ms, and the two bursts are exactly 0.350 s apart; the 20-second chart shows them as two spikes with two D0 marks. On U4, the demo sketch printed the same two claps (see the results below).

The Test Page: Four Measured Sound Test Tools

The four tools on the Test tab take the scene over while they run and put it back when they end. They never talk to your Arduino: they play a sound and measure the A0 and D0 node voltages at every simulation timepoint, so every number is what the circuit really did. Each tool ends with a bold "Good for:" line. All results come from U2 with the demo sketch running.

1. Level Sweep: How Big Is the Swing, and When Does D0 Trip?

Sound sensor level sweep in Proteus: a 1 kHz test tone from 40 to 100 dB, D0 starts to pulse at 85 dB (pred. 83.8), the swing doubles every 6.0 dB, a log chart with the red trip line and a table of A0 min / max, P-P, expected and D0
Figure: Level sweep, 13 points in 1.6 s: D0 from 85 dB (predicted 83.8), 6.0 dB per doubling, P-P 241 at 100 dB.

Good for: "seeing how big A0's swing is for a given loudness, and how loud a sound must be to trip D0." Choose 13 points (40 to 100 dB in 5 dB steps) or 21 points (60 to 100 dB in 2 dB steps). The tool plays a 1 kHz test tone and holds each level for 110 ms:

Level sweep (U2, pot 530, NEAR; D0 pulses counted in 100 ms)
dBA0 min - max (V)P-PExp.D0 (pulses / duty)
402.585 - 2.59630LOW
452.585 - 2.59630LOW
502.584 - 2.59731LOW
552.583 - 2.59831LOW
602.580 - 2.60152LOW
652.576 - 2.60564LOW
702.568 - 2.61398LOW
752.553 - 2.6281513LOW
802.528 - 2.6532524LOW
852.482 - 2.6994543100 / 17 %
902.401 - 2.7807776100 / 34 %
952.258 - 2.924137135100 / 41 %
1002.000 - 3.181241240100 / 45 %

From 70 dB up, the measured swing (P-P) follows the expected one (Exp.) within two counts and grows 1.78 times every 5 dB: 6.0 dB per doubling. The softest tones are lost in the noise (P-P 3); the silent noise floor is P-P 1. D0 stays LOW up to 80 dB (a peak of about 12 counts, less than the 18.5-count margin) and starts at 85 dB, with 100 pulses in 100 ms: once per cycle of the tone. The prediction, 83.8 dB, lies between the two test points. The duty grows from 17 to 45 % as the dips get deeper. On the log chart, orange dots mean D0 pulses, and the red line is the trip level.

2. D0 Threshold: Setting the Pot

Sound sensor D0 threshold test in Proteus for five pots 515 to 560: D0 pulses from 67, 77, 84, 89 and 93 dB against the predicted 69.3 to 92.1 dB, a duty curve per pot, 1.00 pulse per cycle and pulse widths of 272 to 350 us
Figure: D0 threshold, 5 pot settings in 4.7 s: pot 530 trips at 84 dB (predicted 83.8), one pulse per cycle 6 dB above, 338 us / 34 %.

Good for: "setting the pot - how loud a sound must be before D0 reacts, and what D0 looks like when it does." Choose the pot as it is now (530) or five pots from 515 to 560. For each pot, the tone rises 1 dB every 60 ms from 6 dB below the predicted trip level until D0 pulses; then the tool plays it 6 dB louder:

D0 threshold (U2, five pots, NEAR)
PotQuiet A0MarginPulses fromPredicted+6 dBWidthDuty
5152.517 V+17 mV67 dB69.3 dB1.00 / cycle272 us27 %
5202.542 V+42 mV77 dB77.0 dB1.00 / cycle333 us33 %
5302.590 V+90 mV84 dB83.8 dB1.00 / cycle338 us34 %
5452.663 V+163 mV89 dB88.9 dB1.00 / cycle335 us33 %
5602.736 V+236 mV93 dB92.1 dB1.00 / cycle350 us35 %

Every pot trips within 2.3 dB of the prediction, 100 + 20 log(margin / 120) dB. Pot 515 is only 17 mV above VCC/2 and trips at 67 dB; pot 560 sits 236 mV above it and needs 93 dB. Six dB above the trip level, D0 pulses exactly once per cycle, 272 to 350 us wide, and the chart shows the duty climbing with the level. This is the classic advice "turn the pot until the D0 LED just goes off in a quiet room" in numbers.

3. Clap Timing: Debounce and Double Claps

Sound sensor clap timing test in Proteus: three single claps at NEAR, MID and FAR, the A0 chart with the VCC/2 line and D0 marks, longest D0 burst 18.1 ms, ignore D0 for at least 30 ms, and the demo would count 2 claps, 1 toggle
Figure: Clap timing, 3 single claps: NEAR 9 pulses in 18.1 ms (sketch P-P 138), MID 3 pulses in 4.4 ms (P-P 55), FAR not heard.

Good for: "choosing the debounce and the double-clap window of a clap switch, from the real D0 burst." Choose 3 single or 3 double claps, at Near / Mid / Far or as set. Each step starts from 0.3 s of quiet, and every SPICE reading is kept: here 7184 readings in 2.1 s.

Clap timing (U2, single claps at Near / Mid / Far)
StepDistanceFirst pulsePulsesBurstA0 minSketch P-P
1NEAR1.71 ms918.1 ms2.27 V138
2MID3.11 ms34.4 ms2.46 V55
3FARNot heard (no D0 pulse)----

A clap is an 80 ms burst, but D0 pulses only during its loud start: 9 pulses in 18.1 ms at NEAR, 3 in 4.4 ms at MID, and none at FAR, where A0 never reaches VCC/2. "Sketch P-P" is what the demo prints: the max - min of analogRead over the 40 ms after the first pulse.

The cards turn this into settings: with a longest burst of 18.1 ms, ignore D0 for at least 30 ms after a clap (the demo uses 200 ms). And "The demo would count 2 claps, 1 toggle": the NEAR and MID claps came about 0.7 s apart, inside the demo's 0.8 s window, so its rule takes them as a double clap.

4. Scope Capture: The Real Waveform

Sound sensor scope capture in Proteus: a 1 kHz tone at 90 dB triggered by D0 rising, the A0 sine with the VCC/2 line and D0 HIGH marks under its dips, 999.8 Hz, P-P 0.377 V = ADC 77 and 20 D0 pulses at 34 %
Figure: Scope capture, tone 1 kHz at 90 dB, 20 ms: 999.8 Hz, P-P 0.377 V = ADC 77, 20 pulses 331 to 345 us wide, 938 samples.

Good for: "looking at the real waveform - the tone's frequency, a clap's burst, and D0's pulses under it." Pick the sound (as set, the 1 kHz tone, the alarm or a clap), a 10, 20 or 50 ms window and the trigger (D0 rising or now); 20 % of the capture lies before the trigger.

Here, the 1 kHz tone at 90 dB: A0 swings from 2.401 to 2.779 V, a peak-to-peak of 0.377 V = ADC 77, the same 77 counts as the level sweep at 90 dB. From A0's 20 rising crossings of its mean, the tool finds 999.8 Hz. Under every dip below the red VCC/2 line sits one D0 pulse: 20 pulses, 331 to 345 us wide, a duty of 34 %. The 938 samples are Proteus's own timepoints, 2 us apart here, much finer than the 80 us our model asks for.

Settings and Help

The gear opens Settings: the theme (TEP Dark or Light), the text size, open the panel at Run, and Reset to the default size, saved for your Windows user.

Settings page of the TEP Sound Sensor Advance pop-up: theme TEP Dark or Light, text size Small, Normal or Large, open the panel at Run on or off, and reset the window size
Figure: Settings, saved under HKCU\Software\TheEngineeringProjects\TEP Sound Sensor Advance.

The ? icon opens Help & Support: eight cards with their links (bug reports and ideas on the sound sensor board of our forum, this article, updates, donate, our website and the forum) and Copy diagnostics for a bug report. A links.ini next to the DLL can change the links.

Help and Support page of the TEP Sound Sensor Advance: eight cards with their links to the sound sensor forum board, this article, the donate page, the website and the forum, and the Copy diagnostics button
Figure: Help & Support: every card shows its link; TEP Sound Sensor Advance v3.0, build 2026-10-09, TEPSOUND.DLL.

KY-038 Clap Switch with Arduino in Proteus

Open Sound-Sensor-ArduinoUnoV3.pdsprj from the Proteus Simulation folder, with Clap_Switch.hex and both DLLs beside it. Its two Arduino UNOs from our Arduino Library for Proteus V3.0 run the same HEX file:

  • NODE 1 - SIMPLE: UNO 1 (ARD1) with the Sound Simple (U1), the green lamp LED D1 on D13 and Serial Monitor U3.
  • NODE 2 - ADVANCE: UNO 2 (ARD2) with the Sound Advance (U2), the green lamp LED D2 on D13 and Serial Monitor U4.
Sound sensor Arduino Proteus circuit running: two Arduino UNOs with KY-038 sound sensors, the Simple scene panel in the quiet room, the green lamp LEDs D1 off and D2 lit, and two TEP Serial Monitors
Figure: The whole circuit running, without the pop-up window: both sensors in the quiet room, lamp D1 off and lamp D2 on.

Wiring

Demo wiring (the same on both UNOs)
FromToWhy
Sensor + / G+5V terminal / groundPower (the PWR LED lights)
Sensor A0Arduino A0The microphone signal for analogRead()
Sensor D0Arduino D2The comparator output for digitalRead()
Green LED + 220 ΩD13 to GNDThe lamp that the claps switch (the UNO's own D13 LED follows it)
Serial Monitor RXD / TXD / GNDArduino D1 / D0 / groundShows what the Arduino prints, sends what you type
Serial Monitor RXD2Not connectedThe sensor has no serial line to watch

A0 is the first of the UNO's six analog inputs (see our Introduction to Arduino UNO). The sketch polls D0 with digitalRead(); for an interrupt, use attachInterrupt() on D2 (INT0), because Proteus 8.5's ATmega328P does not run pin-change interrupts.

The Arduino Code

The sketch, Clap_Switch.ino, needs no extra library. First the pins and the four settings the commands can change:

const int SOUND_A0 = A0;      // analog output of the sensor (the microphone signal)
const int SOUND_D0 = 2;       // digital output of the sensor (HIGH pulses = loud sound)
const int LAMP_LED = 13;      // the lamp (the UNO's on-board LED)

unsigned long IGNORE_MS = 200;        // after a clap, ignore D0 for this long (one clap = many pulses)
unsigned long DOUBLE_CLAP_MS = 800;   // the second clap must come within this time
unsigned long REPORT_MS = 2000;       // print the sound level every 2 s (EVERY ms; 0 = off)
int clapsToSwitch = 2;                // CLAPS 1 or 2

The heart of the sketch reads A0 for a number of milliseconds and returns the peak-to-peak swing; the mean comes back in average:

int peakToPeak(unsigned long ms, long &average) {
  int lowest = 1023, highest = 0;
  long sum = 0;
  int count = 0;
  unsigned long start = millis();
  while (millis() - start < ms) {
    int value = analogRead(SOUND_A0);
    if (value < lowest) lowest = value;
    if (value > highest) highest = value;
    sum += value;
    count++;
  }
  average = count > 0 ? sum / count : 0;
  return highest - lowest;
}

setup() prints three start lines:

void setup() {
  Serial.begin(9600);
  pinMode(SOUND_D0, INPUT);
  pinMode(LAMP_LED, OUTPUT);
  digitalWrite(LAMP_LED, LOW);
  Serial.println("Clap Switch - KY-038 Sound Sensor - The Engineering Projects");
  Serial.println("Clap twice to switch the lamp (D13) ON / OFF");
  Serial.println("Type HELP for the commands.");
}

In loop(), a clap is the first HIGH on D0 after the ignore time. The sketch measures it over the next 40 ms and checks whether it is the second clap of a pair:

  // 1. A clap: the first HIGH pulse on D0 after the ignore time
  if (digitalRead(SOUND_D0) == HIGH && now - lastClapTime > IGNORE_MS) {
    long average;
    int level = peakToPeak(40, average);     // how loud was it?
    bool second = (claps == 1 && now - lastClapTime <= DOUBLE_CLAP_MS);
    lastClapTime = now;
    clapCount++;

    Serial.print("Clap!  A0 peak-to-peak: ");
    Serial.print(level);

A second clap within the window toggles the lamp, and the sketch prints "(2 of 2)" with the new lamp state:

    } else if (second) {
      claps = 0;
      setLamp(!lampOn);                       // two claps: toggle the lamp

Otherwise, it is the first clap of a pair:

    } else {
      claps = 1;
      Serial.print("   (1 of 2) - clap again within ");
      printSeconds(DOUBLE_CLAP_MS);
      Serial.println(" s");
    }

If the window runs out, the pair starts again:

  // 2. Only one clap and the time is up: start again
  if (claps == 1 && millis() - lastClapTime > DOUBLE_CLAP_MS) {
    claps = 0;
    Serial.println("Only one clap - lamp unchanged");
  }

The claps and the LAMP command switch the lamp through one function:

void setLamp(bool on) {
  lampOn = on;
  digitalWrite(LAMP_LED, lampOn ? HIGH : LOW);
}

Every 2 s, and only then, the sketch prints the level line by itself:

  // 3. Every 2 s (EVERY ms): the quiet level and the sound level - the only line printed by itself
  if (REPORT_MS > 0 && millis() - lastReport >= REPORT_MS) {
    lastReport = millis();
    printLevel();
  }

It shows the mean A0 level and the peak-to-peak over 50 ms, D0 and the lamp:

void printLevel() {
  long average;
  int level = peakToPeak(50, average);
  Serial.print("A0 level: ");
  Serial.print(average);
  Serial.print("   sound peak-to-peak: ");
  Serial.print(level);
  Serial.print("   D0: ");
  Serial.print(digitalRead(SOUND_D0) == HIGH ? "HIGH" : "LOW");
  Serial.print("   lamp: ");
  Serial.println(lampOn ? "ON" : "OFF");
}

The commands are read one line at a time, in upper case. IGNORE accepts 10 to 2000 ms:

  } else if (strncmp(cmd, "IGNORE", 6) == 0 && numberAfter(cmd + 6) >= 10 && numberAfter(cmd + 6) <= 2000) {
    IGNORE_MS = numberAfter(cmd + 6);
    Serial.print("After a clap D0 is ignored for ");
    Serial.print(IGNORE_MS);
    Serial.println(" ms");

CLAPS 1 makes it a single-clap switch:

  } else if (strcmp(cmd, "CLAPS 1") == 0 || strcmp(cmd, "CLAPS 2") == 0) {
    clapsToSwitch = cmd[6] - '0';
    claps = 0;
    Serial.println(clapsToSwitch == 1 ? "One clap switches the lamp" : "Two claps switch the lamp");

HELP prints two short lines, so they fit the Serial Monitor's Simple interface:

void printHelp() {
  Serial.println("Commands: HELP, STATUS, READ, EVERY ms (0 = off),");
  Serial.println("  IGNORE ms, GAP ms, CLAPS 1 or 2, LAMP ON, LAMP OFF");
}

The settings live in RAM, so a new Run brings back the defaults. To change the sketch, export a new HEX file and load it into both UNOs; see How to get the HEX file from Arduino. New to Arduino? Start with our Arduino Tutorial for Beginners.

Serial Monitor Commands

Commands of the demo sketch, upper or lower case (quick buttons: U3 STATUS, READ, EVERY 0, EVERY 2000; U4 STATUS, CLAPS 1, CLAPS 2, HELP)
CommandWhat it does
HELPThe command list (two lines)
STATUSThe lamp, the claps so far and the settings below (two lines)
READOne level line now
EVERY msThe level line every ms milliseconds; EVERY 0 = no level lines, READ still works (default 2000)
IGNORE msAfter a clap, ignore D0 for this long, 10 to 2000 ms (default 200)
GAP msThe second clap must come within this time, 100 to 5000 ms (default 800)
CLAPS 1 / 2One clap or two claps switch the lamp (default 2)
LAMP ON / OFFSwitch the lamp by hand

How to Run the Demo

  1. Press Run. Both Serial Monitors (9600 baud) and U2's pop-up open, and the PWR LEDs light.
  2. On U1's panel, click CLAP: the hands clap, the rings turn red, the D0 and CLAP LEDs flash, and U3 prints "(1 of 2)", then "Only one clap - lamp unchanged". Click DOUBLE CLAP: lamp D1 lights.
  3. Try MID 25 cm (claps are still heard) and FAR 60 cm (nothing is printed until you click POT - three times). Then try MUSIC, TALKING and ALARM.
  4. Use the quick buttons: STATUS, READ and EVERY 0 on U3; CLAPS 1 on U4.
  5. On U2, try the Sound page and the four tools; close its window and reopen it with OPEN PANEL.

Sound Sensor Proteus Simulation Results

The Serial Monitor in Its Simple Interface

Both monitors in this demo run in the Simple interface of our TEP Serial Monitor, as in every monitor screenshot here. Only the log, the send box and one slim toolbar row stay visible (baud rate, follow, pause, search and clear), so the window stays small.

  • Switch to it: right-click the log and choose Simple interface, or press Ctrl+Shift+U.
  • Back to the full view: click the corner button (the expand arrows at the top right), press Esc or Ctrl+Shift+U, or right-click and choose Full interface.
  • Your choice is remembered.

Type commands such as STATUS in the send box and press Enter; the quick buttons come back with the full view. No line the sketch prints is longer than 80 characters, so every line fits. Read all about the monitor in TEP Serial Monitor Advance for Proteus.

Start-Up

After Run, U3 prints the three start lines and then, every 2 s, the level line:

Sound sensor Arduino Proteus start-up in the TEP Serial Monitor Simple interface: Clap Switch - KY-038 Sound Sensor - The Engineering Projects, Clap twice to switch the lamp (D13) ON / OFF, Type HELP for the commands. and A0 level: 530 sound peak-to-peak: 1 D0: LOW lamp: OFF
Figure: U3 in the Simple interface at 9600 baud: the three start lines and the first level line.
Clap Switch - KY-038 Sound Sensor - The Engineering Projects
Clap twice to switch the lamp (D13) ON / OFF
Type HELP for the commands.
A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF

In the quiet room, the mean A0 level is 530, the pot's quiet level, and the peak-to-peak is 1: the model's 1-count noise floor. A real board shows a few counts of noise too.

A Double Clap Switches the Lamp

TEP Serial Monitor Simple interface in Proteus on U4: the start lines, four level lines with lamp OFF, then Clap! A0 peak-to-peak: 134 (1 of 2) - clap again within 0.8 s, Clap! A0 peak-to-peak: 135 (2 of 2) and a level line with lamp ON
Figure: U4 after a DOUBLE CLAP on U2: peak-to-peak 134 and 135, and the lamp is ON.

Every 2 s a level line, then the double clap from U2's Sound page:

A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF
A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF
A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF
A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF
Clap!  A0 peak-to-peak: 134   (1 of 2) - clap again within 0.8 s

The next line is the second clap, 0.35 s later: "Clap! A0 peak-to-peak: 135 (2 of 2)", followed by the sketch's arrow and lamp ON. Lamp D2 lights, and the next level line ends with "lamp: ON". These are the two bursts of 9 pulses from the Sound page's event log above.

The Alarm Switches the Lamp Too

TEP Serial Monitor Simple interface in Proteus on U3: alarm beeps counted as claps with peak-to-peak 76 and 77 that switch the lamp OFF, ON and OFF, Only one clap - lamp unchanged, then a double clap with 133 and 134 that switches the lamp ON
Figure: U3: the ALARM beeps count as claps (P-P 76 / 77) and toggle the lamp; then a real double clap (133 / 134) switches it ON.

This screenshot shows a real-world gotcha. With ALARM at NEAR on U1, each beep prints "Clap!" with a peak-to-peak of 76 or 77, the alarm's swing at 90 dB, and every second beep toggles the lamp:

U3, lines 129 to 141 (U1 at ALARM, then QUIET ROOM and DOUBLE CLAP)
LinesPeak-to-peakWhat the sketch did
129, 13077, 76A pair: lamp OFF
131, 13277, 76A pair: lamp ON
133, 13477, 77A pair: lamp OFF
13577The first of a pair; no second beep followed
139, 140133, 134A double clap: lamp ON

Lines 135 to 139 in full:

Clap!  A0 peak-to-peak: 77   (1 of 2) - clap again within 0.8 s
A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF
Only one clap - lamp unchanged
A0 level: 530   sound peak-to-peak: 1   D0: LOW   lamp: OFF
Clap!  A0 peak-to-peak: 133   (1 of 2) - clap again within 0.8 s

After line 135 the alarm stopped, so the 0.8 s window ran out: "Only one clap - lamp unchanged". Then a DOUBLE CLAP printed 133 and 134 and switched the lamp ON. A real clap switch does the same next to an alarm clock: move the clock further away or turn the pot up.

Troubleshooting

  • A clap prints nothing: it is too quiet for the pot (FAR at the default pot). Click POT - or move to NEAR; the banner and the event log say "too quiet for the pot".
  • The D0 LED is always on, "POT TOO FAR - D0 STUCK HIGH": click POT + until the D0 LED goes off.
  • One clap counts twice: raise IGNORE (the Clap timing tool shows the burst length).
  • The alarm switches the lamp: each beep trips D0, as on a real clap switch; move it to MID or click POT +.
  • The simulation runs slower while a sound plays (about 0.4 x real time with the pop-up open): a sound needs time steps of 50 to 200 us, a quiet room only 1 ms.
  • The Test tab says "Cannot start: ...": the reason is in the message (no power, or another test runs).
  • The part is not simulated, no panel or no monitor window: TEPSOUND.DLL or TEPSERIALMON.DLL is missing from MODELS and the project folder; click OPEN MONITOR on a closed monitor.
  • The Advance pop-up does not appear: click OPEN PANEL, or turn "Open the panel at Run" back on.
  • A pin-change interrupt on D0 never fires: Proteus 8.5's ATmega328P does not run them; use digitalRead() or attachInterrupt() on D2 (INT0).

Things to Know Before Using a Real KY-038 Module

The demo sketch uses only the Arduino core, so it runs on a real UNO and KY-038, too. Keep in mind:

  • Supply: 3.3 to 5 V. A0 and the comparator's VCC/2 reference both follow VCC.
  • A0 is the raw microphone signal: a quiet room reads about 500 to 530, a clap close by swings it by tens to more than 100 counts, speech by a few. Read it as a peak-to-peak over about 50 ms.
  • D0 pulses at the sound's frequency (no hysteresis): take the first pulse and ignore D0 for 100 to 200 ms, otherwise one clap counts many times.
  • The D0 LED always on means the pot is turned too far: turn it back until the LED just goes off in a quiet room.
  • Check your D0 polarity: a few boards and tutorials have D0 the other way round, and the 3-pin "sound detection module" (OUT, GND, VCC) is active LOW. Watch your D0 LED.
  • KY-037: the same board with a bigger microphone.
  • Levels: the dB values in our scene are typical sound levels. The real board has no dB scale, so find your thresholds with your own sounds.
  • Current: the real board draws a few mA.

Limitations of the Simulation

  • Modelled: A0 as the microphone node (4.7 k output resistance, following VCC), D0 as the LM393's open collector with a 10 k pull-up (no hysteresis), the PWR LED's load, the supply, the sounds as sums of tones, the distance, the pot and a 1-count noise floor.
  • Not modelled: the microphone's frequency response and direction, echoes, two sounds mixed with real phase, the LM393's own microsecond delays, the KY-037 and the 3-pin modules.
  • The model is our own implementation, written from the module's published descriptions. The HEX file contains the Arduino AVR core 1.8.6, which includes LGPL-licensed code; its source code is on GitHub.
  • Tested in Proteus 8.5 in October 2026 (the demo, both panels, the four tools and every screenshot here) and with 1,375 automatic PC checks, all passed, 32 of them with the real demo sketch on two UNOs. Proteus 7 is not supported.

Want more boards with an LM393 comparator? See our Flame Sensor Library for Proteus V2.0 and our LDR Sensor Library for Proteus. To switch a lamp on motion instead, take our PIR Sensor Library for Proteus V4.0.

So, that was all about the Sound Sensor Library for Proteus V3.0. I hope the live sound scene, the measured A0 wave, the pulsing D0 and the four test tools make the KY-038 much easier to understand, so your clap switch works the first time you wire a real module. If you use the Sound Sensor Library for Proteus in a project, please share your feedback in the comments or in our forum, and if you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation