<?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 with a piezo buzzer</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/1964/pwm-with-a-piezo-buzzer</link><description>I&amp;#39;m trying to get some sound out of a piezo buzzer using the PWM library here: 
 github.com/.../nrf51-pwm-library 
 My buzzer has a resonant frequency of 2,730Hz. The buzzer doesn&amp;#39;t have its own driver, I need to generate the wave for it. What changes</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 27 May 2015 18:18:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/1964/pwm-with-a-piezo-buzzer" /><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8457?ContentTypeID=1</link><pubDate>Wed, 27 May 2015 18:18:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8324ec71-9613-4f9f-b3dd-3d03d50c322c</guid><dc:creator>Honey</dc:creator><description>&lt;p&gt;hi
please check this code.i have problem in buzzer while using softdevie....&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/ble_5F00_app_5F00_uart_5F00_pwm.zip"&gt;ble_app_uart_pwm.zip&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8456?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 17:32:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7bba03fc-9305-4c72-b637-753ac9e97519</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;It seems we have our pins 1 and 2 round the wrong way on Q3! So indeed, a hardware issue. Thanks again for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8455?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 14:57:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ae3684bc-85f9-4f29-86a0-a8440c76e994</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;No worries. Hopefully that will turn out to be your issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8454?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 14:41:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8b8a54bf-77bd-42e8-ac57-e1eb979c09bb</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;Thanks awfully for your help on this. Q5 has changed since that schematic was printed and is now one that switches at a lower voltage, but I think your point remains valid. Must admit I am quickly out of my depth on the hardware here and will ask my hardware engineer to take a look today. Thanks again.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8453?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 14:07:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65bd6171-4736-471f-80d8-ce3c49c1ec1d</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;I&amp;#39;m looking at your circuit. Could you explain how Q5 works?
If Q5 is turned on wouldn&amp;#39;t you be shorting pin 1 and 2 on your buzzer? So you basically are grounding out the circuit and also draining your battery. I could be looking at the circuit wrong but that seems to be a valid area to consider.&lt;/p&gt;
&lt;p&gt;If that is the case I would remove Q5 and just ground pin 2 to ground of the buzzer. Seems to be a circuit to stop leakage from the battery when the buzzer is not in use. I would also place a cap in parallel to R4 (100pf or similar)&lt;/p&gt;
&lt;p&gt;Hardware issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8452?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 13:56:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5e39a477-9751-462d-ab36-f91ad624a75f</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;Eliot,&lt;/p&gt;
&lt;p&gt;Loaded your code on a pca10001 with no softdevice using sdk 5.1.0.36092.&lt;/p&gt;
&lt;p&gt;I placed your code inside of the uart application and just included nrf_pwm.c to the lib folder and copied your main over.&lt;/p&gt;
&lt;p&gt;I first verified that your source would generate 2.730kHz signal and it did without any modification.&lt;/p&gt;
&lt;p&gt;Then I added the while loop section and the duty cycle adjusted according to the formula and sin table.&lt;/p&gt;
&lt;p&gt;I am attaching the screen shots.&lt;/p&gt;
&lt;p&gt;tek00002 =&amp;gt; manually setting duty
tek00003 =&amp;gt; using sin table formula
tek00004 =&amp;gt; using sin table formula&lt;/p&gt;
&lt;p&gt;pics 00003 &amp;amp; 00004 show the duty cycle varying.&lt;/p&gt;
&lt;p&gt;So your code appears to be running fine. Have you or can you try on an evaluation board. That would rule out the software and point to an hardware issue.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/tek00002.png" alt="tek00002.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/tek00003.png" alt="tek00003.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/tek00004.png" alt="tek00004.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8451?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 13:18:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:489a1c52-7857-4351-b0eb-2e038dbea4b4</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;Here&amp;#39;s the source I&amp;#39;m using, not much different from nrf51-pwm-library out of the box, and a schematic for my buzzer circuit. That line marked &amp;#39;BuzzerOn&amp;#39; is going to pin 14 on the Nordic. I&amp;#39;m driving the buzzer off a lithium ion battery, rather than the Nordic pin itself, hence the transistors here.&lt;/p&gt;
&lt;p&gt;Have just checked my battery and it&amp;#39;s flat (2.04V)! Let me charge it and see if that helps. Will post again with multimeter output once that&amp;#39;s done.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/main_5F00_sin.c"&gt;main_sin.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/nrf_5F00_pwm.h"&gt;nrf_pwm.h&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/nrf_5F00_pwm.c"&gt;nrf_pwm.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/screenshot_5F00_schematic_5F00_buzzer.png" alt="screenshot_schematic_buzzer.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8450?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 12:28:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4465ad7-c30c-4552-87b7-16c892b48361</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;Are you able to update your source code?
Then I can run it and let you know my results.&lt;/p&gt;
&lt;p&gt;If not I understand.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8449?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 10:08:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec3a2ea8-a1d2-44e4-84a6-31eb87cae3b2</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;Thanks. USE_WITH_SOFTDEVICE is definitely 0 and I&amp;#39;ve tested with the nrf_pwm_set_value(0, 1465) hard coded. No joy.&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t yet have a scope but will see what I can see with a multimeter.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8448?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 09:53:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bf3c3a76-2e11-4b10-b66d-5da9182f5b96</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;Your frequency was taken into account in the formula TORBJØRN provided above ...&lt;/p&gt;
&lt;p&gt;pwm_max_value = 16000000/ (2 * 2^0* 2730) = 2930
2930 is not in hertz. it is the max pwm value, the max counter value for the timer compare.&lt;/p&gt;
&lt;p&gt;So between 0-2930 with a prescaler of 0 the pwm frequency will be 2730 and a variable duty cycle depending of the value to set with the nrf_pwm_set_value function.&lt;/p&gt;
&lt;p&gt;nrf_pwm_set_value(0,1465) gives a 2730 Hz signal with 50% duty
nrf_pwm_set_value(0,500) gives a 2730 Hz signal with ~6% duty&lt;/p&gt;
&lt;p&gt;If you added the code given by TORBJØRN you should be generating the correct frequency.&lt;/p&gt;
&lt;p&gt;have you attempted to manually set the duty cycle?
nrf_pwm_set_value(0, 1465); Try to manually set it instead of using the sin table.&lt;/p&gt;
&lt;p&gt;This will help you debug.&lt;/p&gt;
&lt;p&gt;Do you have any means off verifying the frequency being generated on the pin? oscope? dmm? Make sure you are getting a signal first.&lt;/p&gt;
&lt;p&gt;If you aren&amp;#39;t using the softdevice make sure  USE_WITH_SOFTDEVICE is set to 0. By default in the pwm.h I believe it is 1. This will definitely cause an app failure.&lt;/p&gt;
&lt;p&gt;Good Luck.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8447?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 09:31:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f913ba2d-79ae-4438-b22c-aa51c9a391b4</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;I see the confusion: my buzzer requires 2,730Hz, not 2,930Hz. Half of that is 1,365. Do you mean I need to scale up the values in the sine table so that they peak at 1,365, like this?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;	uint32_t value = (float)sin_table[counter]/255.0f * 1365.0f;
    nrf_pwm_set_value(0, value);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Running that produces no sound yet. I&amp;#39;m sure my pwm_config is wrong. Currently using this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nrf_pwm_config_t pwm_config =
	{.num_channels  = 1,             // Was 3
	.gpio_num       = {14},          // Was 8,9,10
	.ppi_channel    = {0,1,2,3,4,5},
	.gpiote_channel = {2,3,0},
	.mode           = PWM_MODE_BUZZER_2730HZ};
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8446?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2014 07:56:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0dca661d-7eb6-4336-a748-3a60642f5d08</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;Eliot,&lt;/p&gt;
&lt;p&gt;By duty cycle of 50% he means the following ...&lt;/p&gt;
&lt;p&gt;nrf_pwm_set_value(0, sin_table[counter]); where sin_table[counter] value what be 50% of the pwm max value. If pwm max = 2930 then half of that (50%) would give 1465.&lt;/p&gt;
&lt;p&gt;Hope that helps.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8445?ContentTypeID=1</link><pubDate>Fri, 28 Mar 2014 19:33:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:251eafc7-9880-46ae-a304-d7476f8a7e01</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;Sorry, I&amp;#39;m not following entirely. I&amp;#39;ve modified nrf_pwm_init() to use your extra case, thanks for that. But don&amp;#39;t understand what you mean by &amp;quot;for 50% duty cycle use 1465&amp;quot;. What should the pwm_config look like?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nrf_pwm_config_t pwm_config =
	{.num_channels  = 3,
	.gpio_num       = {8,9,10},
	.ppi_channel    = {0,1,2,3,4,5},
	.gpiote_channel = {2,3,0},
	.mode           = PWM_MODE_BUZZER_2730HZ};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My buzzer has one pin on the Nordic&amp;#39;s pin 14 and the other on ground, effectively.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8444?ContentTypeID=1</link><pubDate>Fri, 28 Mar 2014 17:13:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fb43dc01-0e81-4c92-9886-69810e4c0385</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Eliot&lt;/p&gt;
&lt;p&gt;Did you figure this out?&lt;/p&gt;
&lt;p&gt;You can modify the nrf51-pwm-library to run at a base frequency of about 2730 Hz quite easily, you just have to change the max value of the PWM using the following formula:&lt;/p&gt;
&lt;p&gt;pwm_max_value = 16.000.000 / (2 * 2^PRESCALER * FREQUENCY)&lt;/p&gt;
&lt;p&gt;With a prescaler of 0 this gives you a max value of 2930, and I implemented a new PWM mode in nrf_pwm.c to test this out:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    case PWM_MODE_BUZZER_2730HZ:
        PWM_TIMER-&amp;gt;PRESCALER = 0;
        pwm_max_value = 2930;
        break;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now you just need to set the channel according to the duty cycle you want on your buzzer signal, for 50% duty cycle use 1465.&lt;/p&gt;
&lt;p&gt;Best regards
Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8443?ContentTypeID=1</link><pubDate>Tue, 25 Mar 2014 00:09:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8d9d385c-b752-48a9-8d85-f07d3a63c456</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;Thanks Steven. I just tried running this. No sound yet. What would a typical value be for MAX_SAMPLE_LEVELS? How would I aim for 2,730Hz?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8442?ContentTypeID=1</link><pubDate>Mon, 24 Mar 2014 14:55:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6655833f-d05f-426c-bcb1-45f699d97c9f</guid><dc:creator>Steven Wik</dc:creator><description>&lt;p&gt;I should add that you can run other code while the buzzer is running since it will be toggled on the Timer - you may have to be clever about it if you want to change the tone frequency to create a tune, but it&amp;#39;s doable.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM with a piezo buzzer</title><link>https://devzone.nordicsemi.com/thread/8441?ContentTypeID=1</link><pubDate>Mon, 24 Mar 2014 14:52:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d575fb5d-88d7-4a7a-9ce5-63cc898e9b16</guid><dc:creator>Steven Wik</dc:creator><description>&lt;p&gt;Eliot,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not familiar with the code you posted above or the library that it uses, however I have implemented code to control a piezo buzzer in much the same way that you require, which I&amp;#39;ll provide snippets of below.  Some of this code may be in the library you use, again I haven&amp;#39;t looked at it.&lt;/p&gt;
&lt;p&gt;First, you can configure the pin to the buzzer as an output like below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Audio / PWM Pin Setup
  nrf_gpio_cfg_output(PWM_OUTPUT_PIN_NUMBER);		// Connect GPIO input buffers and configure PWM_OUTPUT_PIN_NUMBER as output
  nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW);
  NRF_PPI-&amp;gt;CH[1].EEP = (uint32_t)&amp;amp;NRF_TIMER2-&amp;gt;EVENTS_COMPARE[1];  // Configure PPI channel 1 to toggle PWM_OUTPUT_PIN on every TIMER2 COMPARE[1] match
  NRF_PPI-&amp;gt;CH[1].TEP = (uint32_t)&amp;amp;NRF_GPIOTE-&amp;gt;TASKS_OUT[0];
  NRF_PPI-&amp;gt;CHEN = (PPI_CHEN_CH1_Enabled &amp;lt;&amp;lt; PPI_CHEN_CH1_Pos);  // Enable PPI channels 0-2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you can configure the Timer to control the PWM output as below - this code will cause the PWM pin to toggle whenever the number in CC[1] is hit:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Configure Timer for Buzzer
	NRF_TIMER2-&amp;gt;MODE = TIMER_MODE_MODE_Timer;
  NRF_TIMER2-&amp;gt;PRESCALER = 4; //1us resolution
  NRF_TIMER2-&amp;gt;TASKS_CLEAR = 1;
  NRF_TIMER2-&amp;gt;CC[1] = MAX_SAMPLE_LEVELS;
  NRF_TIMER2-&amp;gt;INTENSET = (TIMER_INTENSET_COMPARE1_Enabled &amp;lt;&amp;lt; TIMER_INTENSET_COMPARE1_Pos);
  NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt on Timer 2
  __enable_irq();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, you can implement a handler for the timer using code like below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void TIMER2_IRQHandler(void)						// used for buzzer
{
  if ((NRF_TIMER2-&amp;gt;EVENTS_COMPARE[1] != 0) &amp;amp;&amp;amp; ((NRF_TIMER2-&amp;gt;INTENSET &amp;amp; TIMER_INTENSET_COMPARE1_Msk) != 0))
  {
    // Sets the next CC1 value
    NRF_TIMER2-&amp;gt;EVENTS_COMPARE[1] = 0;
    NRF_TIMER2-&amp;gt;CC[1] = (NRF_TIMER2-&amp;gt;CC[1] + MAX_SAMPLE_LEVELS);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To start the buzzer, simply start the Timer, as below:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;NRF_TIMER2-&amp;gt;TASKS_START = 1;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Try changing the value of MAX_SAMPLE_LEVELS around to achieve different tones.&lt;/p&gt;
&lt;p&gt;I hope this helps!&lt;/p&gt;
&lt;p&gt;Cheers,
Steve&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>