<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/116599/how-to-change-pwm-period-on-runtime-in-ncs</link><description>Hi, 
 I&amp;#39;m trying to play some sounds on a piezo with the help of the pwm. I tried to set the period with pwm_set_dt but only the first call is successful and every subsequent call raised an error. 
 Here is my test code: 
 
 The results looked like this</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 05 Jun 2025 08:07:19 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/116599/how-to-change-pwm-period-on-runtime-in-ncs" /><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/538164?ContentTypeID=1</link><pubDate>Thu, 05 Jun 2025 08:07:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7bf333c0-0c0d-4f27-a83b-6fa68bb89b20</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;Thank you very much. Finally, after several months, there is a solution to the problem.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/536876?ContentTypeID=1</link><pubDate>Mon, 26 May 2025 07:36:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cd2d80cf-e555-4155-a6db-d9a12d4c8a9d</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;The Zephyr PWM API is a bit limited. I suggest using nrfx driver directly&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/blob/master/nrfx/samples/src/nrfx_pwm/common_mode/main.c"&gt;https://github.com/zephyrproject-rtos/hal_nordic/blob/master/nrfx/samples/src/nrfx_pwm/common_mode/main.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/531337?ContentTypeID=1</link><pubDate>Thu, 10 Apr 2025 11:33:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:49ec7274-8ccb-4a6b-aae0-a8de2f7b96c8</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;Is there anything else I can test? Can you provide an example where you change the frequency of two PWM channels?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/527201?ContentTypeID=1</link><pubDate>Thu, 13 Mar 2025 14:02:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b732822c-8a28-4bf2-8207-a7f697d0ddd3</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;It&amp;#39;s defined like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;pwmpiezo0 {
		compatible = &amp;quot;pwm-leds&amp;quot;;
		status = &amp;quot;okay&amp;quot;;
	
		piezo_pwm_pos: piezo_pwm_pos {
				status = &amp;quot;okay&amp;quot;;
				pwms = &amp;lt;&amp;amp;pwm1 0 PWM_HZ(7500) PWM_POLARITY_NORMAL&amp;gt;;
				label = &amp;quot;Piezo positive&amp;quot;;
		};
	
		piezo_pwm_neg: piezo_pwm_neg {
				status = &amp;quot;okay&amp;quot;;
				pwms = &amp;lt;&amp;amp;pwm1 1 PWM_HZ(7500) PWM_POLARITY_INVERTED&amp;gt;;
				label = &amp;quot;Piezo negative&amp;quot;;
		};
	};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;One of the pins is already inverted. I then call&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;ret = pwm_set_dt(&amp;amp;piezo_pwm_pos, PWM_HZ(m_piezo_params.frequency),
					PWM_HZ(m_piezo_params.frequency) / 2);

ret = pwm_set_dt(&amp;amp;piezo_pwm_neg, PWM_HZ(m_piezo_params.frequency),
				   PWM_HZ(m_piezo_params.frequency) / 2);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;within a timeout handler every x ms. If m_piezo_params.frequency stays the same, the function calls return 0. But if I change the frequency it breaks after the first timeout.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/527135?ContentTypeID=1</link><pubDate>Thu, 13 Mar 2025 11:32:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ce7c692d-059c-4f0c-b121-40f7112cbe01</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Ah, right!&lt;/p&gt;
&lt;p&gt;How do you define these channels?&lt;/p&gt;
&lt;p&gt;If you do it like this in the dts:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.7.99-ncs2/boards/nordic/nrf52dk/nrf52dk_nrf52832.dts#L48-L53"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v3.7.99-ncs2/boards/nordic/nrf52dk/nrf52dk_nrf52832.dts#L48-L53&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;you can have e.g.&amp;nbsp;pwm_led_0 with&amp;nbsp;PWM_POLARITY_INVERTED,&lt;/p&gt;
&lt;p&gt;and define a&amp;nbsp;&lt;span&gt;pwm_led_1 with&amp;nbsp;PWM_POLARITY_NORMAL&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/526985?ContentTypeID=1</link><pubDate>Wed, 12 Mar 2025 14:18:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bfa4d1e0-863a-4e11-9da3-e7f6ebc42c0f</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;you already suggested this, but it also results in &amp;quot;Incompatible period.&amp;quot; and the functions return -EINVAL after the first time. Is it a problem, that I call these functions from a timeout handler?&lt;/p&gt;
&lt;p&gt;In the nrf5 SDK there was&amp;nbsp;NRF_DRV_PWM_EVT_FINISHED. In this event handler I could change the frequency of the pwm and&amp;nbsp;it&amp;nbsp;results&amp;nbsp;in a nice alarm sound.&amp;nbsp;Why is this not&amp;nbsp;possible with the nrf connect SDK.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/526952?ContentTypeID=1</link><pubDate>Wed, 12 Mar 2025 13:40:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:92004622-37b8-4601-8489-5b4d96e60188</guid><dc:creator>Sigurd</dc:creator><description>&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
Try using the&amp;nbsp;&amp;nbsp;pwm_set_cycles() function. And use&amp;nbsp;PWM_POLARITY_NORMAL on one channel, and&amp;nbsp;&amp;nbsp;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;PWM_POLARITY_INVERTED on the other. Snippet:&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="ui-code" data-mode="text"&gt;ret = pwm_set_cycles(pwm_led0-&amp;gt;dev, pwm_led0-&amp;gt;channel, period_cycles, period_cycles / 2,
	PWM_POLARITY_INVERTED);

ret = pwm_set_cycles(pwm_led1-&amp;gt;dev, pwm_led1-&amp;gt;channel, period_cycles, period_cycles / 2,
		PWM_POLARITY_NORMAL);&lt;/pre&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/526913?ContentTypeID=1</link><pubDate>Wed, 12 Mar 2025 12:28:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:06a5b03e-aa17-4dc4-9039-6033e3612341</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;in your case you just changed the period of one pin. If I do&amp;nbsp;it also for only one channel everything works. I want to set two pins exactly inverted with changing frequency over time as seen in the image below.&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/8623.Screenshot.png" /&gt;&lt;/p&gt;
&lt;p&gt;But I only can&amp;nbsp;change the frequency for one channel or set it once for two channels. It&amp;#39;s not possible to change the frequency while the pwm is active. I could change the frequency if I disable the pwm for both channels before setting the new frequency, but this results in an&amp;nbsp;interrupted output sound on the piezo.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;How can I invert channel 1 in relation to channel 0?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Christian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/513398?ContentTypeID=1</link><pubDate>Wed, 04 Dec 2024 11:29:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:04e689fb-1706-4bec-b89d-190400fff8e0</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I tried repdicing the issues, but it works fine here.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1733311760008v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Here is the project I used for testing:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/pwm_5F00_test.zip"&gt;devzone.nordicsemi.com/.../pwm_5F00_test.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Tested with NCS 2.8.0 and nRF52832DK(build target nrf52dk/nrf52832)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/512878?ContentTypeID=1</link><pubDate>Mon, 02 Dec 2024 07:06:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86229934-93ee-42f8-b443-4815bf2ff01d</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;unfortunatelly this doesn&amp;#39;t work either. If I set the period with this function I will just get some &amp;quot;clicking&amp;quot; sounds on the piezo. Is there a similar function like in the nrf5 SDK where I can start the PWM for n repeats and then it will automatically stop?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Christian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/512232?ContentTypeID=1</link><pubDate>Tue, 26 Nov 2024 23:21:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0816cedb-39f7-455c-a507-5dd33a22882a</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Try&amp;nbsp;using&amp;nbsp;pwm_set_cycles()&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.nordicsemi.com/bundle/zephyr-apis-latest/page/group_pwm_interface.html#gaff280789f7b45fdefc354b3f841fe3ef"&gt;https://docs.nordicsemi.com/bundle/zephyr-apis-latest/page/group_pwm_interface.html#gaff280789f7b45fdefc354b3f841fe3ef&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/511812?ContentTypeID=1</link><pubDate>Mon, 25 Nov 2024 07:19:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f59f56f-bf73-4c17-98e2-a52746afe763</guid><dc:creator>ilockit_CFR</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;at the moment I&amp;#39;m using SDK5 and&amp;nbsp;can easily change the frequency within an event handler, which is called when the PWM is finished. How is this possible in Nordic Connect SDK?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Christian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to change pwm period on runtime in ncs?</title><link>https://devzone.nordicsemi.com/thread/511577?ContentTypeID=1</link><pubDate>Thu, 21 Nov 2024 22:42:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:21e1c88f-050c-4a5a-87b0-15b559def84c</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I see you are getting this in the log: &amp;quot;Incompatible period&amp;quot;. Looking at the source code where this is printed,&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.7.99-ncs1/drivers/pwm/pwm_nrfx.c#L94,"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v3.7.99-ncs1/drivers/pwm/pwm_nrfx.c#L94,&lt;/a&gt;&amp;nbsp;there is a comment that explains this error:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;/* If any other channel is driven by the PWM peripheral, the period&lt;br /&gt; * that is currently set cannot be changed, as this would influence&lt;br /&gt; * the output for that channel.&lt;br /&gt; */&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>