{"id":5040,"date":"2022-10-03T19:58:33","date_gmt":"2022-10-03T17:58:33","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=5040"},"modified":"2024-02-17T09:50:02","modified_gmt":"2024-02-17T08:50:02","slug":"arduino-stm32-audio-pwm-output","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/","title":{"rendered":"Arduino STM32 &#8211; Fast PWM Audio Output"},"content":{"rendered":"<p>I was measuring the speed of the <strong>analogWrite()<\/strong> on an <strong>STM32 Black Pill<\/strong> on some PWM pins and I was getting a result of around <strong>48697 samples<\/strong> per second. This is enough for one line of hifi data: but I wanted to provide at least 4 channels!<\/p>\n<p>This is not good enough: so I generated some new code with 4 PWM output pins using the <strong>STMCubeIDE<\/strong> and measured how many duty cycle updates we can do on these 4 channel: I was getting <strong>more the 1.2 millions<\/strong>!<\/p>\n<p>This seems to be a much better option. Next I tried to do the same in the <strong>STM32 Arduino Timer API<\/strong> and integrated this into my PWMAudioStream version for the STM32: I was using the <strong>TIM2<\/strong> to generate the PWM and <strong>TIM3<\/strong> for driving the sample rate.<\/p>\n<p>Finally, here is an example sketch that tests with 2 output channels:<\/p>\n<pre><code>#include \"AudioTools.h\"\n\nint channels = 2;\nuint16_t sample_rate=44200;\nSineWaveGenerator&lt;int16_t&gt; sineWave(32000); \/\/ subclass of SoundGenerator with max amplitude of 32000\nGeneratedSoundStream&lt;int16_t&gt; sound(sineWave);  \/\/ Stream generated from sine wave\nPWMAudioStream pwm; \/\/ Analog output simulated by PWM                 \nStreamCopy copier(pwm, sound); \/\/ copy in to out\n\nvoid setup() {\n  Serial.begin(115200);\n  AudioLogger::instance().begin(Serial, AudioLogger::Warning);  \n\n  \/\/ setup sine wave\n  sineWave.begin(channels, sample_rate, N_B4);\n\n  \/\/ setup PWM output\n  auto config = pwm.defaultConfig();\n  config.sample_rate = sample_rate;\n  config.channels = channels;  \n  pwm.begin(config);\n}\n\nvoid loop(){\n  copier.copy();\n}\n<\/code><\/pre>\n<p>The output goes to the pins PA0, PA1 and to PA2 and PA3 if we would use all 4 channels.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was measuring the speed of the analogWrite() on an STM32 Black Pill on some PWM pins and I was getting a result of around 48697 samples per second. This is enough for one line of hifi data: but I wanted to provide at least 4 channels! This is not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4980,"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":[45],"class_list":["post-5040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound","tag-stm32"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Arduino STM32 - Fast PWM Audio Output - 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\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Arduino STM32 - Fast PWM Audio Output - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"I was measuring the speed of the analogWrite() on an STM32 Black Pill on some PWM pins and I was getting a result of around 48697 samples per second. This is enough for one line of hifi data: but I wanted to provide at least 4 channels! This is not [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-03T17:58:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-17T08:50:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/09\/stm32.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\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\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Arduino STM32 &#8211; Fast PWM Audio Output\",\"datePublished\":\"2022-10-03T17:58:33+00:00\",\"dateModified\":\"2024-02-17T08:50:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/\"},\"wordCount\":166,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/stm32.jpeg\",\"keywords\":[\"STM32\"],\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/\",\"name\":\"Arduino STM32 - Fast PWM Audio Output - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/stm32.jpeg\",\"datePublished\":\"2022-10-03T17:58:33+00:00\",\"dateModified\":\"2024-02-17T08:50:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/stm32.jpeg\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/stm32.jpeg\",\"width\":500,\"height\":500},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2022\\\/10\\\/03\\\/arduino-stm32-audio-pwm-output\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Arduino STM32 &#8211; Fast PWM Audio Output\"}]},{\"@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":"Arduino STM32 - Fast PWM Audio Output - 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\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/","og_locale":"en_US","og_type":"article","og_title":"Arduino STM32 - Fast PWM Audio Output - Phil Schatzmann","og_description":"I was measuring the speed of the analogWrite() on an STM32 Black Pill on some PWM pins and I was getting a result of around 48697 samples per second. This is enough for one line of hifi data: but I wanted to provide at least 4 channels! This is not [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/","og_site_name":"Phil Schatzmann","article_published_time":"2022-10-03T17:58:33+00:00","article_modified_time":"2024-02-17T08:50:02+00:00","og_image":[{"width":500,"height":500,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/09\/stm32.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\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Arduino STM32 &#8211; Fast PWM Audio Output","datePublished":"2022-10-03T17:58:33+00:00","dateModified":"2024-02-17T08:50:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/"},"wordCount":166,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/09\/stm32.jpeg","keywords":["STM32"],"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/","url":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/","name":"Arduino STM32 - Fast PWM Audio Output - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/09\/stm32.jpeg","datePublished":"2022-10-03T17:58:33+00:00","dateModified":"2024-02-17T08:50:02+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/09\/stm32.jpeg","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/09\/stm32.jpeg","width":500,"height":500},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2022\/10\/03\/arduino-stm32-audio-pwm-output\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Arduino STM32 &#8211; Fast PWM Audio Output"}]},{"@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\/5040","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=5040"}],"version-history":[{"count":15,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5040\/revisions"}],"predecessor-version":[{"id":5055,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5040\/revisions\/5055"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/4980"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=5040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=5040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=5040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}