{"id":6292,"date":"2024-08-06T11:27:17","date_gmt":"2024-08-06T09:27:17","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=6292"},"modified":"2024-07-30T21:57:50","modified_gmt":"2024-07-30T19:57:50","slug":"testing-the-audio-pwm-output-on-a-stm32h7","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/","title":{"rendered":"Testing the Audio PWM output on a STM32H7"},"content":{"rendered":"<p>In <a href=\"https:\/\/www.pschatzmann.ch\/home\/2024\/07\/30\/analog-audio-output-on-an-stm32h7\/\">my last blog<\/a>, I described the <strong>analog audio output<\/strong> using the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\">Arduino Audio Tools library<\/a>: The <strong>STM32H743VIT6<\/strong> microcontroller is quite powerful but we only have <strong>max 2 DAC pins<\/strong> available!<\/p>\n<p>This restriction falls when we use <strong>PWM<\/strong>: We use a PWM frequency above the hearing range (30khz) and modulate the PWM Duty Cycle with the audio signal.<\/p>\n<p>Compared to the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/examples\/examples-stream\/streams-generator-pwm\/streams-generator-pwm.ino\">demo pwm sine example<\/a>, I increased the <strong>sample rate<\/strong> to 44100 and the <strong>channels<\/strong> to 2 and I defined the <strong>two output pins<\/strong>.<\/p>\n<pre><code>#include \"AudioTools.h\"\n\nAudioInfo info(44100, 2, 16);\nSineWaveGenerator&lt;int16_t&gt; sineWave(32000); \nGeneratedSoundStream&lt;int16_t&gt; sound(sineWave);  \nPWMAudioOutput pwm;                  \nStreamCopy copier(pwm, sound);    \nint pins[] = {PA1, PA5};\n\n\nvoid setup() {\n  Serial.begin(115200);\n  AudioLogger::instance().begin(Serial, AudioLogger::Warning);  \n\n  \/\/ setup sine wave\n  sineWave.begin(info, N_B4);\n\n  \/\/ setup PWM output\n  auto config = pwm.defaultConfig();\n  config.copyFrom(info);\n  config.setPins(pins);\n  pwm.begin(config);\n}\n\nvoid loop(){\n  copier.copy();\n}\n<\/code><\/pre>\n<p>Here is result from an oszilloscope:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm-300x154.png\" alt=\"\" width=\"600\"  class=\"alignnone size-medium wp-image-6293\" srcset=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm-300x154.png 300w, https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm-768x395.png 768w, https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png 1020w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>It seems that the output signal between the different channels are <strong>not quite in synch<\/strong> but apart from this, it seems to work perfectly.<\/p>\n<p>To turn this into a proper sine wave on the oscilloscope, we would need to add a <a href=\"https:\/\/www.electronics-tutorials.ws\/filter\/filter_2.html\">low pass filter<\/a>. But you can connect some earphones directly, because your ears will act as low pass filter since humans can only hear up to 17&#8217;000hz and this decreases when we get older.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last blog, I described the analog audio output using the Arduino Audio Tools library: The STM32H743VIT6 microcontroller is quite powerful but we only have max 2 DAC pins available! This restriction falls when we use PWM: We use a PWM frequency above the hearing range (30khz) and modulate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6293,"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":[47],"class_list":["post-6292","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound","tag-stm32h7"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Testing the Audio PWM output on a STM32H7 - 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\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Testing the Audio PWM output on a STM32H7 - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"In my last blog, I described the analog audio output using the Arduino Audio Tools library: The STM32H743VIT6 microcontroller is quite powerful but we only have max 2 DAC pins available! This restriction falls when we use PWM: We use a PWM frequency above the hearing range (30khz) and modulate [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-06T09:27:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"524\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Testing the Audio PWM output on a STM32H7\",\"datePublished\":\"2024-08-06T09:27:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/\"},\"wordCount\":173,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/pwm.png\",\"keywords\":[\"STM32H7\"],\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/\",\"name\":\"Testing the Audio PWM output on a STM32H7 - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/pwm.png\",\"datePublished\":\"2024-08-06T09:27:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/pwm.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/pwm.png\",\"width\":1020,\"height\":524},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2024\\\/08\\\/06\\\/testing-the-audio-pwm-output-on-a-stm32h7\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Testing the Audio PWM output on a STM32H7\"}]},{\"@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":"Testing the Audio PWM output on a STM32H7 - 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\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/","og_locale":"en_US","og_type":"article","og_title":"Testing the Audio PWM output on a STM32H7 - Phil Schatzmann","og_description":"In my last blog, I described the analog audio output using the Arduino Audio Tools library: The STM32H743VIT6 microcontroller is quite powerful but we only have max 2 DAC pins available! This restriction falls when we use PWM: We use a PWM frequency above the hearing range (30khz) and modulate [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/","og_site_name":"Phil Schatzmann","article_published_time":"2024-08-06T09:27:17+00:00","og_image":[{"width":1020,"height":524,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png","type":"image\/png"}],"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\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Testing the Audio PWM output on a STM32H7","datePublished":"2024-08-06T09:27:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/"},"wordCount":173,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png","keywords":["STM32H7"],"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/","url":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/","name":"Testing the Audio PWM output on a STM32H7 - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png","datePublished":"2024-08-06T09:27:17+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2024\/07\/pwm.png","width":1020,"height":524},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2024\/08\/06\/testing-the-audio-pwm-output-on-a-stm32h7\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Testing the Audio PWM output on a STM32H7"}]},{"@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\/6292","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=6292"}],"version-history":[{"count":11,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6292\/revisions"}],"predecessor-version":[{"id":6327,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/6292\/revisions\/6327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/6293"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=6292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=6292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=6292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}