<?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>PWM register-based LED dimming not updating duty cycle dynamically on nRF5340</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/124786/pwm-register-based-led-dimming-not-updating-duty-cycle-dynamically-on-nrf5340</link><description>Hi, I’m trying to generate a PWM signal directly using the nRF5340’s PWM peripheral registers (not using Zephyr’s pwm_dt_spec or driver API). 
 The LED turns ON correctly, so the PWM peripheral is initialized and running, but the duty cycle doesn’t change</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 Oct 2025 09:31:06 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/124786/pwm-register-based-led-dimming-not-updating-duty-cycle-dynamically-on-nrf5340" /><item><title>RE: PWM register-based LED dimming not updating duty cycle dynamically on nRF5340</title><link>https://devzone.nordicsemi.com/thread/551096?ContentTypeID=1</link><pubDate>Fri, 10 Oct 2025 09:31:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:938ced30-c299-44aa-8606-c9e5782835c6</guid><dc:creator>Edvin</dc:creator><description>[quote user="arun98475"]Does this seem like a good way to handle real-time PCM playback using PWM on nRF5340?[/quote]
&lt;p&gt;Yes, this is reasonable.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I am not aware of any good samples in NCS using PWM for playing sound. But I remember that there was a sample for the Thingy:52 in the old nRF5 SDK for Thingy.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can find it here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.nordicsemi.com/Products/Development-hardware/Nordic-Thingy-52/Download"&gt;https://www.nordicsemi.com/Products/Development-hardware/Nordic-Thingy-52/Download&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Search for &amp;quot;file with source code&amp;quot; on that page.&lt;/p&gt;
&lt;p&gt;After you download the zip file, look for the file:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;source\drivers\drv_speaker.c -&amp;gt;&amp;nbsp;drv_speaker_flash_pcm_play().&lt;/p&gt;
&lt;p&gt;It uses values from sounds.h, set up in drv_speaker_sample_play() (from drv_speaker.c)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM register-based LED dimming not updating duty cycle dynamically on nRF5340</title><link>https://devzone.nordicsemi.com/thread/550912?ContentTypeID=1</link><pubDate>Wed, 08 Oct 2025 13:30:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4fc15697-f015-4c4c-95f8-c51c04cd9813</guid><dc:creator>arun98475</dc:creator><description>&lt;p data-start="317" data-end="491"&gt;My goal is to play &lt;strong data-start="336" data-end="365"&gt;PCM audio (48 kHz, int16)&lt;/strong&gt; coming from a PC,&amp;nbsp; Down sampled to 8k uint8_t and play through a GPIO pin using PWM, and also mix it with other generated waveforms (like sine or custom tones).&lt;/p&gt;
&lt;p data-start="493" data-end="514"&gt;Here&amp;rsquo;s the concept:&lt;/p&gt;
&lt;ul data-start="515" data-end="914"&gt;
&lt;li data-start="515" data-end="564"&gt;
&lt;p data-start="517" data-end="564"&gt;I will use the &lt;code data-start="531" data-end="541"&gt;nrfx_pwm&lt;/code&gt; driver in loop mode.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-start="565" data-end="636"&gt;
&lt;p data-start="567" data-end="636"&gt;I maintain a &lt;strong data-start="580" data-end="595"&gt;ring buffer&lt;/strong&gt; that continuously stores new PCM data.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-start="637" data-end="798"&gt;
&lt;p data-start="639" data-end="798"&gt;The PWM sequence (&lt;code data-start="657" data-end="677"&gt;nrf_pwm_sequence_t&lt;/code&gt;) will point to this buffer, and I&amp;rsquo;ll use &lt;code data-start="719" data-end="747"&gt;nrfx_pwm_sequence_update()&lt;/code&gt; periodically to refresh it with the latest data.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-start="799" data-end="914"&gt;
&lt;p data-start="801" data-end="914"&gt;This should allow &lt;strong data-start="819" data-end="850"&gt;asynchronous audio playback&lt;/strong&gt;, avoiding glitches caused by blocking or synchronous updates.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I believe this approach will give smooth asynchronous playback and help eliminate current glitches.&lt;br data-start="2841" data-end="2844" /&gt; Does this seem like a good way to handle real-time PCM playback using PWM on nRF5340?&lt;br data-start="2929" data-end="2932" /&gt; Any suggestions or potential pitfalls I should be aware&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM register-based LED dimming not updating duty cycle dynamically on nRF5340</title><link>https://devzone.nordicsemi.com/thread/550780?ContentTypeID=1</link><pubDate>Tue, 07 Oct 2025 08:51:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6a6b2c73-e4a2-4421-92e0-8fadff082438</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;It is not closed. It is open. But it did have the status &amp;quot;Verified Answer&amp;quot; at some point:&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/pastedimage1759817838452v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;So if you press &amp;quot;Verify answer&amp;quot; on one of the replies in the ticket, it will get this state. But if you then later reply in it, it will go back to open.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, the reason you want to use the registers directly is that you want to have them running synchronously, correct?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;While it is technically possible to run the PWM registers directly, as it is (almost) done in the nRF5 SDK&amp;#39;s SDK\examples\peripheral\pwm_driver for the nRF52 series, it is a bit more complicated here, since there is a different method of settings GPIOs as output pins in NCS, requiring you to involve devicetree (an .overlay file).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Attached is a modified version of:&lt;/p&gt;
&lt;p&gt;NCS\modules\hal\nordic\nrfx\samples\src\nrfx_pwm\common_mode&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/hello_5F00_world_5F00_nrfx_5F00_pwm.zip"&gt;devzone.nordicsemi.com/.../hello_5F00_world_5F00_nrfx_5F00_pwm.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is a quite simple app controlling the 4 LEDs as PWM pins, but it doesn&amp;#39;t need to be the LEDs, just replace the pin numbers.&lt;/p&gt;
&lt;p&gt;However, when the nrfx pwm driver is finished handling the PWM sequence, it will release the pins, leaving them as disconnected input.&lt;/p&gt;
&lt;p&gt;If you rather want to control the pins at a fixed PWM signal &amp;quot;forever&amp;quot;, but also with the possibility to turn them always on/always off, I suggest you look into the blinky_pwm implementation from NCS\zephyr\samples\basic\blinky_pwm.&lt;/p&gt;
&lt;p&gt;Attached here is another simplified version, using the pins that you wanted, P0.25 and P0.26.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/hello_5F00_world_5F00_pwm.zip"&gt;devzone.nordicsemi.com/.../hello_5F00_world_5F00_pwm.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that if you use this approach (the overlay files and zephyr pwm library), the pins will be set as a static output 0 or 1 if you set the duty cycle to 100% or 0% of the PWM period.&lt;/p&gt;
&lt;p&gt;Also note that the pins are selected in the nrf5340dk_nrf5340_cpuapp.overlay file. It should be quite easy to change them (pin number, polarity, etc).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM register-based LED dimming not updating duty cycle dynamically on nRF5340</title><link>https://devzone.nordicsemi.com/thread/550737?ContentTypeID=1</link><pubDate>Mon, 06 Oct 2025 16:14:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3153a2c9-e41f-4999-95b9-dbe4e0c84e18</guid><dc:creator>arun98475</dc:creator><description>&lt;p data-start="72" data-end="79"&gt;&lt;strong data-start="72" data-end="79"&gt;Hi,&lt;/strong&gt;&lt;/p&gt;
&lt;p data-start="81" data-end="168"&gt;Actually, I don&amp;rsquo;t have any overlay file. The only setting I have in my&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code data-start="152" data-end="162"&gt;prj.conf&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;is:&lt;/p&gt;
&lt;div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary"&gt;
&lt;div class="sticky top-9"&gt;
&lt;div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"&gt;
&lt;div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="overflow-y-auto p-4" dir="ltr"&gt;&lt;code class="whitespace-pre!"&gt;&lt;span&gt;&lt;span class="hljs-attr"&gt;CONFIG_NRFX_PWM0&lt;/span&gt;=y&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-start="197" data-end="262"&gt;The code snippet I shared is from a small test program I created.&lt;/p&gt;
&lt;p data-start="264" data-end="529"&gt;The reason I&amp;rsquo;m doing this is that in my main project, I need to update four GPIO pins simultaneously. Currently, I&amp;rsquo;m toggling them using a timer running at 8000 Hz. This works fine for a single GPIO pin, but when I add multiple pins, it starts producing glitches.&lt;/p&gt;
&lt;p data-start="531" data-end="640"&gt;To improve performance and eliminate those glitches, I&amp;rsquo;m exploring the use of PWM and direct register writes.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM register-based LED dimming not updating duty cycle dynamically on nRF5340</title><link>https://devzone.nordicsemi.com/thread/550715?ContentTypeID=1</link><pubDate>Mon, 06 Oct 2025 13:12:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05ab6402-fef4-41c2-bc82-040f1793bc36</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I suspect your LEDs are on because the pins are disconnected. At least unless you did something in your devicetree/overlay files?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Can you please zip and upload your entire application?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Is there a particular reason why you want to use the PWM registers directly like this, instead of using the PWM drivers?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What is your end goal for the PWM? What are you controlling?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>