{"id":52362,"date":"2026-11-01T10:51:01","date_gmt":"2026-11-01T10:51:01","guid":{"rendered":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/"},"modified":"2026-11-01T10:51:01","modified_gmt":"2026-11-01T10:51:01","slug":"sound-sensor","status":"publish","type":"siratek_tutorial","link":"https:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/","title":{"rendered":"Reacting to Sound"},"content":{"rendered":"<body><p><\/p>\n<h2>What it does<\/h2>\n<p>An electret microphone, an amplifier and a comparator on one small board. It gives you a digital pin that goes active when a sound is loud enough, and an analog pin carrying the amplified microphone signal.<\/p>\n<p>This is the sensor most likely to disappoint, and it is worth saying why before you wire it up. It does not measure loudness, it cannot tell you what a sound was, and the obvious way to use the analog pin gives you a number that bounces around meaninglessly. All of those are fixable or avoidable, and this page is mostly about how.<\/p>\n<h2>Before you begin<\/h2>\n<h3>Required parts and tools<\/h3>\n<ul>\n<li>An Arduino Uno or compatible board.<\/li>\n<li>One microphone sound sensor module. This is Siratek product 2892, supplied as the KY-038 or the similar KY-030.<\/li>\n<li>Four jumper wires.<\/li>\n<\/ul>\n<h3>Check the pin order on your own board<\/h3>\n<p>This matters more here than on any other module in this batch. The Siratek board\u2019s four-pin header is labelled, in order:<\/p>\n<p><strong><code>AO<\/code>  <code>G<\/code>  <code>+<\/code>  <code>DO<\/code><\/strong><\/p>\n<p>That is <strong>not<\/strong> the <code>AO DO GND VCC<\/code> order used by the gas and soil modules. Power and ground sit in the middle, with the two outputs on the outside.<\/p>\n<p>If you wire this from memory after doing the MQ-2 tutorial, you will put 5 V onto the digital output pin. Read the silkscreen, every time, on every module. The same four signals appear in at least three different orders across cheap sensor boards.<\/p>\n<h3>What is on the board<\/h3>\n<ul>\n<li>The silver cylinder is an <strong>electret microphone<\/strong>.<\/li>\n<li>The blue multi-turn trimmer sets the comparator threshold. Multi-turn means it takes many turns to cross its range, so a quarter turn does almost nothing. Keep going.<\/li>\n<li>An LM393 comparator.<\/li>\n<li>Two LEDs: one for power, one that follows <code>DO<\/code>.<\/li>\n<\/ul>\n<h3>The analog pin does not give you loudness<\/h3>\n<p>This is the misunderstanding worth clearing up first.<\/p>\n<p>Sound is a pressure wave that swings above and below the resting air pressure, thousands of times a second. The microphone turns that into a voltage that swings above and below a midpoint, and that is what reaches <code>AO<\/code>.<\/p>\n<p>So a single <code>analogRead()<\/code> catches the waveform at one arbitrary instant. In silence it returns the midpoint, somewhere around 512. In a loud room it returns a number that could be anywhere between the extremes, including the midpoint. Print those readings and you get noise in both senses.<\/p>\n<p><strong>Loudness is the size of the swing, not the height of one sample.<\/strong> To get it you must sample over a window of time and measure the spread:<\/p>\n<pre><code>loudness  ~  (highest sample in the window) - (lowest sample in the window)<\/code><\/pre>\n<p>A window of about 50 milliseconds is a reasonable compromise: long enough to catch a full cycle of anything audible, short enough to feel responsive. That is what the second example does, and it is the difference between a sensor that seems broken and one that works.<\/p>\n<h3>What it cannot do<\/h3>\n<ul>\n<li><strong>It is not a sound level meter.<\/strong> The number is uncalibrated and depends on the trimmer, the module and the distance. It cannot be converted into decibels in any meaningful way.<\/li>\n<li><strong>It cannot recognise sounds.<\/strong> There is no frequency analysis here. A clap, a door and a shout are all just a swing.<\/li>\n<li><strong>It is not very sensitive.<\/strong> These modules hear a clap across a room and a conversation only up close. If you want to detect speech at a distance, this is not the part.<\/li>\n<\/ul>\n<p>What it is good for: clap switches, knock detection, a noise-activated light, a visual level display. Those are all about <em>something happened<\/em>, which is exactly what it is good at.<\/p>\n<h2>Pin reference<\/h2>\n<table>\n<thead>\n<tr>\n<th>Pin or label<\/th>\n<th>Connect to<\/th>\n<th>Purpose<\/th>\n<th>Notes<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>AO<\/code><\/td>\n<td>Uno <code>A0<\/code><\/td>\n<td>Amplified microphone signal<\/td>\n<td>Swings around a midpoint<\/td>\n<\/tr>\n<tr>\n<td><code>G<\/code><\/td>\n<td>Uno <code>GND<\/code><\/td>\n<td>Ground<\/td>\n<td>Second from the <code>AO<\/code> end<\/td>\n<\/tr>\n<tr>\n<td><code>+<\/code><\/td>\n<td>Uno <code>5V<\/code><\/td>\n<td>Power<\/td>\n<td>Third from the <code>AO<\/code> end<\/td>\n<\/tr>\n<tr>\n<td><code>DO<\/code><\/td>\n<td>Uno <code>D2<\/code><\/td>\n<td>Threshold output<\/td>\n<td>Set by the trimmer<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<figure class=\"siratek-tutorial-figure\"><img decoding=\"async\" src=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/sound-sensor-wiring.svg\" alt=\"A KY-038 sound sensor module showing the AO, G, plus and DO header order, with AO to A0 and DO to pin 2\" loading=\"lazy\"><figcaption>Power and ground sit in the middle on this board, not at the end. Read the silkscreen.<\/figcaption><\/figure>\n<h2>Wiring steps<\/h2>\n<ol>\n<li>Unplug the USB cable.<\/li>\n<li>Read the four labels on your board and confirm the order before connecting anything.<\/li>\n<li><code>G<\/code> to <code>GND<\/code>.<\/li>\n<li><code>+<\/code> to <code>5V<\/code>.<\/li>\n<li><code>AO<\/code> to <code>A0<\/code>.<\/li>\n<li><code>DO<\/code> to <code>D2<\/code>.<\/li>\n<li>Plug the USB cable back in. The power LED should light.<\/li>\n<\/ol>\n<h2>Software and libraries<\/h2>\n<p>No library is required.<\/p>\n<h2>Minimal working example<\/h2>\n<p>Start by seeing the problem, so the fix makes sense.<\/p>\n<pre><code>\/\/ Siratek: the naive reading, which is deliberately unhelpful.\n\nconst int soundPin = A0;\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(\"Siratek: watch this jump around even in a quiet room.\");\n}\n\nvoid loop() {\n  Serial.println(analogRead(soundPin));\n  delay(50);\n}<\/code><\/pre>\n<p>Run it in silence and the numbers wander. Clap and nothing clearly changes. This is the sketch most people write first, and the reason they conclude the sensor is faulty.<\/p>\n<h3>Measuring the swing instead<\/h3>\n<pre><code>\/\/ Siratek: loudness as the peak-to-peak swing over a 50 ms window.\n\nconst int soundPin = A0;\nconst unsigned long windowMs = 50;\n\nint measureLoudness() {\n  unsigned long start = millis();\n  int high = 0;\n  int low = 1023;\n\n  while (millis() - start &lt; windowMs) {\n    int sample = analogRead(soundPin);\n    if (sample &gt; high) {\n      high = sample;\n    }\n    if (sample &lt; low) {\n      low = sample;\n    }\n  }\n\n  return high - low;\n}\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(\"Siratek: be quiet, then clap, and write both numbers down.\");\n}\n\nvoid loop() {\n  int loudness = measureLoudness();\n\n  Serial.print(\"loudness = \");\n  Serial.println(loudness);\n}<\/code><\/pre>\n<p>Now the quiet reading is small and steady, and a clap produces an obvious spike. Those two numbers are your floor and your ceiling, and every threshold should sit between them, measured in the room where the project will live.<\/p>\n<p>Note there is no <code>delay()<\/code>. The window itself takes 50 milliseconds, which is the pacing.<\/p>\n<h3>A clap switch<\/h3>\n<pre><code>\/\/ Siratek: toggling an output on a loud sound, ignoring echoes.\n\nconst int soundPin = A0;\nconst int lampPin = 8;\n\n\/\/ Measure your own with the sketch above.\nconst int clapAbove = 120;\n\n\/\/ A clap echoes. Ignore anything for a moment afterwards.\nconst unsigned long ignoreFor = 400;\n\nunsigned long lastClap = 0;\nbool lampOn = false;\n\nint measureLoudness() {\n  unsigned long start = millis();\n  int high = 0;\n  int low = 1023;\n\n  while (millis() - start &lt; 50) {\n    int sample = analogRead(soundPin);\n    if (sample &gt; high) {\n      high = sample;\n    }\n    if (sample &lt; low) {\n      low = sample;\n    }\n  }\n\n  return high - low;\n}\n\nvoid setup() {\n  pinMode(lampPin, OUTPUT);\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  int loudness = measureLoudness();\n\n  if (loudness &gt; clapAbove &amp;&amp; millis() - lastClap &gt; ignoreFor) {\n    lastClap = millis();\n    lampOn = !lampOn;\n    digitalWrite(lampPin, lampOn);\n    Serial.println(lampOn ? \"on\" : \"off\");\n  }\n}<\/code><\/pre>\n<p>The 400 millisecond blanking period is what makes this usable. A clap in a hard-walled room produces echoes loud enough to cross the threshold again, and without it one clap toggles the lamp twice and leaves it where it started. It is the same thought as debouncing a button, for the same reason.<\/p>\n<h3>Using the digital pin instead<\/h3>\n<p>If <code>A0<\/code> is needed elsewhere, <code>DO<\/code> will do the job, with the trimmer doing what your threshold did:<\/p>\n<ol>\n<li>In a quiet room, turn the trimmer until the <code>DO<\/code> LED just goes out.<\/li>\n<li>Back it off slightly.<\/li>\n<li>Clap, and check it lights.<\/li>\n<\/ol>\n<p>It is a multi-turn trimmer, so give it several full turns before deciding nothing is happening.<\/p>\n<h2>Expected result<\/h2>\n<p>With the window sketch, a quiet room gives a small steady number and a clap gives a clear spike, several times larger.<\/p>\n<p>The absolute values depend on the trimmer and the module. What matters is the ratio: if a clap does not produce a number several times the quiet reading, turn the sensitivity up and try again.<\/p>\n<h2>Troubleshooting<\/h2>\n<table>\n<thead>\n<tr>\n<th>Symptom<\/th>\n<th>Likely cause<\/th>\n<th>Check or fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Analog reading jumps about in silence<\/td>\n<td>Reading the waveform, not the level<\/td>\n<td>Measure peak-to-peak over a window<\/td>\n<\/tr>\n<tr>\n<td>No difference between quiet and loud<\/td>\n<td>Single sample again<\/td>\n<td>Same fix<\/td>\n<\/tr>\n<tr>\n<td>Nothing works, module warm<\/td>\n<td>Pin order assumed from another board<\/td>\n<td>This one is <code>AO G + DO<\/code><\/td>\n<\/tr>\n<tr>\n<td>Quiet reading is not near the middle<\/td>\n<td>Normal; the midpoint varies<\/td>\n<td>Use the swing, not the absolute value<\/td>\n<\/tr>\n<tr>\n<td>Trimmer seems to do nothing<\/td>\n<td>It is multi-turn<\/td>\n<td>Keep turning, several full turns<\/td>\n<\/tr>\n<tr>\n<td><code>DO<\/code> always active<\/td>\n<td>Threshold too sensitive<\/td>\n<td>Adjust in a quiet room until the LED goes out<\/td>\n<\/tr>\n<tr>\n<td><code>DO<\/code> never active<\/td>\n<td>Threshold backed off too far<\/td>\n<td>Turn back, then test with a clap<\/td>\n<\/tr>\n<tr>\n<td>One clap toggles twice<\/td>\n<td>Echoes crossing the threshold again<\/td>\n<td>Add a blanking period<\/td>\n<\/tr>\n<tr>\n<td>Cannot hear speech across a room<\/td>\n<td>These modules are not sensitive<\/td>\n<td>Use it for loud, close sounds<\/td>\n<\/tr>\n<tr>\n<td>Readings change when a motor runs<\/td>\n<td>Electrical noise on the supply<\/td>\n<td>Separate supplies, grounds joined<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Next steps<\/h2>\n<ul>\n<li><strong>Easier first:<\/strong> <em>Digital and Analog, and When Each Applies<\/em>, for why one sample is not a level.<\/li>\n<li><strong>Related products:<\/strong> the passive buzzer, to make sound as well as hear it.<\/li>\n<li><strong>Next project:<\/strong> a bar display of the current level on an OLED, updated several times a second.<\/li>\n<\/ul>\n<h2>Sources and attribution<\/h2>\n<p>All text, wiring guidance and code in this tutorial are original Siratek work.<\/p>\n<ul>\n<li>The <code>AO G + DO<\/code> header order, the electret microphone, the multi-turn trimmer, the LM393 and the two indicator LEDs were read from the Siratek product photographs for item 2892.<\/li>\n<\/ul>\n<p>The peak-to-peak window technique described here follows from how a microphone signal behaves rather than from any particular source. Threshold values are presented as something to measure in the room where the project will be used. This module is explicitly described as uncalibrated and unsuitable for sound level measurement. No third-party tutorial text, diagrams, photographs or code has been reproduced here.<\/p>\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>An electret microphone, an amplifier and a comparator on one small board. It gives you a digital pin that goes active when a sound is loud enough, and an analog pin carrying the amplified microphone signal.<\/p>\n","protected":false},"featured_media":0,"template":"","siratek_tutorial_level":[],"class_list":["post-52362","siratek_tutorial","type-siratek_tutorial","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Reacting to Sound - siratek<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reacting to Sound - siratek\" \/>\n<meta property=\"og:description\" content=\"An electret microphone, an amplifier and a comparator on one small board. It gives you a digital pin that goes active when a sound is loud enough, and an analog pin carrying the amplified microphone signal.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/\" \/>\n<meta property=\"og:site_name\" content=\"siratek\" \/>\n<meta property=\"og:image\" content=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/sound-sensor-wiring.svg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/\",\"url\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/\",\"name\":\"Reacting to Sound - siratek\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sound-sensor-wiring.svg\",\"datePublished\":\"2026-11-01T10:51:01+00:00\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/#primaryimage\",\"url\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sound-sensor-wiring.svg\",\"contentUrl\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/sound-sensor-wiring.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/sound-sensor\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/siratek.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Reacting to Sound\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\",\"url\":\"https:\\\/\\\/siratek.net\\\/\",\"name\":\"siratek\",\"description\":\"Your Best Electronic Retail Shop\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/siratek.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reacting to Sound - siratek","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/","og_locale":"en_US","og_type":"article","og_title":"Reacting to Sound - siratek","og_description":"An electret microphone, an amplifier and a comparator on one small board. It gives you a digital pin that goes active when a sound is loud enough, and an analog pin carrying the amplified microphone signal.","og_url":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/","og_site_name":"siratek","og_image":[{"url":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/sound-sensor-wiring.svg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/","url":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/","name":"Reacting to Sound - siratek","isPartOf":{"@id":"https:\/\/siratek.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/#primaryimage"},"image":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/#primaryimage"},"thumbnailUrl":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/sound-sensor-wiring.svg","datePublished":"2026-11-01T10:51:01+00:00","breadcrumb":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/#primaryimage","url":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/sound-sensor-wiring.svg","contentUrl":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/sound-sensor-wiring.svg"},{"@type":"BreadcrumbList","@id":"http:\/\/siratek.net\/index.php\/tutorials\/sound-sensor\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siratek.net\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"https:\/\/siratek.net\/index.php\/tutorials\/"},{"@type":"ListItem","position":3,"name":"Reacting to Sound"}]},{"@type":"WebSite","@id":"https:\/\/siratek.net\/#website","url":"https:\/\/siratek.net\/","name":"siratek","description":"Your Best Electronic Retail Shop","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siratek.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial"}],"about":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/types\/siratek_tutorial"}],"version-history":[{"count":0,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52362\/revisions"}],"wp:attachment":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/media?parent=52362"}],"wp:term":[{"taxonomy":"siratek_tutorial_level","embeddable":true,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial_level?post=52362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}