{"id":3055,"date":"2021-04-30T06:08:31","date_gmt":"2021-04-30T04:08:31","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=3055"},"modified":"2021-12-14T15:15:32","modified_gmt":"2021-12-14T14:15:32","slug":"back-to-basics-sampling-audio-with-the-esp32","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/","title":{"rendered":"Back to Basics &#8211; Sampling Audio with the ESP32"},"content":{"rendered":"<p>In this Blog I am giving a little bit of background on how the sampling of analog signals has been implemented in my Arduino <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">audo-tools library<\/a>. My initial approach was based on the <a href=\"https:\/\/www.toptal.com\/embedded\/esp32-audio-sampling\">Blog from Ivan Voras<\/a> using timers and interrupts.<\/p>\n<p>But there is a much better way by using the <strong>extended ESP32 I2S functionality<\/strong>: You can use this to sample <strong>an analog signal<\/strong> (e.g. from a microphone) at <strong>very high speeds<\/strong> and I finally used this approach in my <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/src\/AudioTools\/ADC.h\">ADC class<\/a>.<\/p>\n<p>Here is how you set it up using the ESP32 API:<\/p>\n<pre><code>#include \"esp_a2dp_api.h\"\n#include \"driver\/i2s.h\"\n#include \"freertos\/queue.h\"\n#include \"driver\/adc.h\"\n\n\/\/ public config parameters\nint sample_rate = 44100;\nint dma_buf_count = 10;\nint dma_buf_len = 512;\nbool use_apll = false;\n\nconst i2s_port_t i2s_num = I2S_NUM_0; \/\/ Analog input only supports 0!\n\n\/\/ select the analog pin e.g 32\nadc_unit_t unit = ADC_UNIT_1;\nadc1_channel_t channel = ADC1_GPIO32_CHANNEL;\n\n\/\/ I2S config\ni2s_config_t i2s_config = {\n  .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),\n  .sample_rate = sample_rate,\n  .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,\n  .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,\n  .communication_format = I2S_COMM_FORMAT_I2S_LSB,\n  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,\n  .dma_buf_count = dma_buf_count,\n  .dma_buf_len = dma_buf_len,\n  .use_apll = use_apll,\n  .tx_desc_auto_clear = false,\n  .fixed_mclk = 0};\n\n\/\/ setup config\nif (i2s_driver_install(i2s_num, &amp;i2s_config, 0, NULL)!=ESP_OK){\n    ESP_LOGE(ADC_TAG, \"%s - %s\", __func__, \"i2s_driver_install\");\n}      \n\n\/\/init ADC pad\nif (i2s_set_adc_mode(unit, channel)!=ESP_OK) {\n    ESP_LOGE(ADC_TAG, \"%s - %s\", __func__, \"i2s_set_adc_mode\");\n}\n\n\/\/ enable the ADC\nif (i2s_adc_enable(i2s_num)!=ESP_OK) {\n    ESP_LOGE(ADC_TAG, \"%s - %s\", __func__, \"i2s_adc_enable\");\n}\n\n<\/code><\/pre>\n<p>And finally you can just use i2s_read() in a loop to get the data:<\/p>\n<pre><code>size_t size_read;\ni2s_read(i2s_num, dest, size_bytes, &amp;size_read, ticks_to_wait);\n<\/code><\/pre>\n<p>Pretty simple! &#8211; but it is even simpler with my API:<\/p>\n<pre><code>ADC adc;\nconst int32_t max_buffer_len = 512;\nint16_t buffer[max_buffer_len][2];\n\nauto config = adc.defaultConfig());\n\/\/ default pin is 34 - change it to 32 \nconfig.setPin(32);\nadc.begin(config);\n\n\/\/ and finally read the data \nsize_t len = adc.read(buffer, max_buffer_len); \n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this Blog I am giving a little bit of background on how the sampling of analog signals has been implemented in my Arduino audo-tools library. My initial approach was based on the Blog from Ivan Voras using timers and interrupts. But there is a much better way by using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1307,"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":[20,22],"tags":[30],"class_list":["post-3055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound","tag-back-to-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Back to Basics - Sampling Audio with the ESP32 - 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\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Back to Basics - Sampling Audio with the ESP32 - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"In this Blog I am giving a little bit of background on how the sampling of analog signals has been implemented in my Arduino audo-tools library. My initial approach was based on the Blog from Ivan Voras using timers and interrupts. But there is a much better way by using [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-30T04:08:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-14T14:15:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/03\/esp32.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"266\" \/>\n\t<meta property=\"og:image:height\" content=\"190\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/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\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Back to Basics &#8211; Sampling Audio with the ESP32\",\"datePublished\":\"2021-04-30T04:08:31+00:00\",\"dateModified\":\"2021-12-14T14:15:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/\"},\"wordCount\":131,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/esp32.jpeg\",\"keywords\":[\"Back to Basics\"],\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/\",\"name\":\"Back to Basics - Sampling Audio with the ESP32 - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/esp32.jpeg\",\"datePublished\":\"2021-04-30T04:08:31+00:00\",\"dateModified\":\"2021-12-14T14:15:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/esp32.jpeg\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/esp32.jpeg\",\"width\":266,\"height\":190},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2021\\\/04\\\/30\\\/back-to-basics-sampling-audio-with-the-esp32\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Back to Basics &#8211; Sampling Audio with the ESP32\"}]},{\"@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 - Sampling Audio with the ESP32 - 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\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/","og_locale":"en_US","og_type":"article","og_title":"Back to Basics - Sampling Audio with the ESP32 - Phil Schatzmann","og_description":"In this Blog I am giving a little bit of background on how the sampling of analog signals has been implemented in my Arduino audo-tools library. My initial approach was based on the Blog from Ivan Voras using timers and interrupts. But there is a much better way by using [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/","og_site_name":"Phil Schatzmann","article_published_time":"2021-04-30T04:08:31+00:00","article_modified_time":"2021-12-14T14:15:32+00:00","og_image":[{"width":266,"height":190,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/03\/esp32.jpeg","type":"image\/jpeg"}],"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\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Back to Basics &#8211; Sampling Audio with the ESP32","datePublished":"2021-04-30T04:08:31+00:00","dateModified":"2021-12-14T14:15:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/"},"wordCount":131,"commentCount":6,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/03\/esp32.jpeg","keywords":["Back to Basics"],"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/","url":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/","name":"Back to Basics - Sampling Audio with the ESP32 - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/03\/esp32.jpeg","datePublished":"2021-04-30T04:08:31+00:00","dateModified":"2021-12-14T14:15:32+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/03\/esp32.jpeg","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/03\/esp32.jpeg","width":266,"height":190},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2021\/04\/30\/back-to-basics-sampling-audio-with-the-esp32\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Back to Basics &#8211; Sampling Audio with the ESP32"}]},{"@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\/3055","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=3055"}],"version-history":[{"count":12,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/3055\/revisions"}],"predecessor-version":[{"id":3067,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/3055\/revisions\/3067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/1307"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=3055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=3055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=3055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}