{"id":2402,"date":"2021-01-07T12:20:39","date_gmt":"2021-01-07T11:20:39","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=2402"},"modified":"2021-12-14T15:16:00","modified_gmt":"2021-12-14T14:16:00","slug":"esp32-tone","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/","title":{"rendered":"Back to Basics &#8211; A simple ESP32 sound generator (tone)"},"content":{"rendered":"<p>The first thing I wanted to try out after I have received my <strong>Piezo Buzzer<\/strong> was to generate some simple beeps. In <strong>Standard Arduino<\/strong> there exists the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/advanced-io\/tone\/\">tone()<\/a> function which &#8211; unfortunately &#8211; is not available in the ESP32 Arduino implementation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg\" alt=\"\" width=\"193\" height=\"250\" class=\"alignnone size-full wp-image-2386\" \/><\/p>\n<p>There are libraries to bridge this gap, but I was wondering how this could be implemented using the functionality which is available. I came up with the following alternatives:<\/p>\n<ul>\n<li>Use <strong>I2S together with the built in DAC<\/strong>: This is not simple and in the best case we can generate sounds on max 2 pins.<\/li>\n<li>Use the <strong>built in PWM functionality<\/strong>: The functionality is available to set the frequency and the number of pins are limited by the available timer (2 channels groups * 8 channels each) which gives 16 parallel sounds which is not bad. <\/li>\n<li>Use <strong>digital output<\/strong> together with <strong>a timer<\/strong>: There are 2 groups where each has 2 general purpose timers.<\/li>\n<\/ul>\n<p>Here is an first simple example which uses the last approach. We use a timer which toggels the indicated digital pin on and off in the indicated frequency. This is done in a timer interrupt using an alarm.<\/p>\n<pre><code>const int PIN = 25;\n\nhw_timer_t* timer = NULL;\nbool value = true;\nint frequency = 20; \/\/ 20 to 20000\n\nvoid IRAM_ATTR onTimer() {\n  value = !value;\n  digitalWrite(PIN, value); \n}\n\nvoid setup() {\n    Serial.begin(115200);\n    pinMode(PIN, OUTPUT);    \/\/ sets the digital pin as output\n    setupTimer();\n}\n\nvoid setupTimer() {\n    \/\/ Use 1st timer of 4  - 1 tick take 1\/(80MHZ\/80) = 1us so we set divider 80 and count up \n    timer = timerBegin(0, 80, true);\/\/div 80\n    timerAttachInterrupt(timer, &amp;onTimer, true);\n}\n\nvoid setFrequency(long frequencyHz){\n    timerAlarmDisable(timer);\n    timerAlarmWrite(timer, 1000000l \/ frequencyHz, true);\n    timerAlarmEnable(timer);\n}\n\nvoid tone(long frequencyHz, long durationMs){\n    setFrequency(frequencyHz);\n    delay(durationMs);\n}\n\nvoid loop() {\n    Serial.print(frequency); \/\/ in hz\n    Serial.println(\" hz\");\n    tone(frequency,1000);\n    frequency+=100;\n    if (frequency&gt;=200000){\n      frequency = 20;\n    }\n}\n<\/code><\/pre>\n<p>I decided to use this approach and change it so, that we can drive <strong>any number of pins<\/strong> with <strong>one single timer<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first thing I wanted to try out after I have received my Piezo Buzzer was to generate some simple beeps. In Standard Arduino there exists the tone() function which &#8211; unfortunately &#8211; is not available in the ESP32 Arduino implementation. There are libraries to bridge this gap, but I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[22],"tags":[30],"class_list":["post-2402","post","type-post","status-publish","format-standard","hentry","category-machine-sound","tag-back-to-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Back to Basics - A simple ESP32 sound generator (tone) - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Back to Basics - A simple ESP32 sound generator (tone) - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"The first thing I wanted to try out after I have received my Piezo Buzzer was to generate some simple beeps. In Standard Arduino there exists the tone() function which &#8211; unfortunately &#8211; is not available in the ESP32 Arduino implementation. There are libraries to bridge this gap, but I [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-07T11:20:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-14T14:16:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Back to Basics &#8211; A simple ESP32 sound generator (tone)\",\"datePublished\":\"2021-01-07T11:20:39+00:00\",\"dateModified\":\"2021-12-14T14:16:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/\"},\"wordCount\":218,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/piezo.jpeg\",\"keywords\":[\"Back to Basics\"],\"articleSection\":[\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/\",\"name\":\"Back to Basics - A simple ESP32 sound generator (tone) - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/piezo.jpeg\",\"datePublished\":\"2021-01-07T11:20:39+00:00\",\"dateModified\":\"2021-12-14T14:16:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/piezo.jpeg\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/piezo.jpeg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/01\\\/07\\\/esp32-tone\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Back to Basics &#8211; A simple ESP32 sound generator (tone)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Back to Basics - A simple ESP32 sound generator (tone) - Phil Schatzmann","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":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/","og_locale":"en_US","og_type":"article","og_title":"Back to Basics - A simple ESP32 sound generator (tone) - Phil Schatzmann","og_description":"The first thing I wanted to try out after I have received my Piezo Buzzer was to generate some simple beeps. In Standard Arduino there exists the tone() function which &#8211; unfortunately &#8211; is not available in the ESP32 Arduino implementation. There are libraries to bridge this gap, but I [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/","og_site_name":"Phil Schatzmann","article_published_time":"2021-01-07T11:20:39+00:00","article_modified_time":"2021-12-14T14:16:00+00:00","og_image":[{"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg","type":"","width":"","height":""}],"author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Back to Basics &#8211; A simple ESP32 sound generator (tone)","datePublished":"2021-01-07T11:20:39+00:00","dateModified":"2021-12-14T14:16:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/"},"wordCount":218,"commentCount":5,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg","keywords":["Back to Basics"],"articleSection":["Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/","url":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/","name":"Back to Basics - A simple ESP32 sound generator (tone) - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg","datePublished":"2021-01-07T11:20:39+00:00","dateModified":"2021-12-14T14:16:00+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2021\/01\/piezo.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/01\/07\/esp32-tone\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Back to Basics &#8211; A simple ESP32 sound generator (tone)"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=2402"}],"version-history":[{"count":8,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2402\/revisions"}],"predecessor-version":[{"id":2410,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2402\/revisions\/2410"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=2402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=2402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=2402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}