PWM Features

Hi,
I need someone to help me understand to get a buzzer working with PWM. Am I right to assume that analogWrite(pin,127) would give me a 50% duty cycle? i have been speaking to my manager about this, and he says I will need to add a off time for this to give a 50% duty cycle.

Example 1
Off time to create a 50% duty cycle ;

analogWrite(pin,127);
delay(30);
analogWrite(pin,0);
delay(30);

Example 2
Simply just the one line of code;
analogWrite(pin,127);

Can anyone confirm which is correct.

Thank you.

Am I right to assume that analogWrite(pin,127) would give me a 50% duty cycle?

Yes

The clue is in the name
Pulse
Width
Modulation

The analogWrite() parameter determines the width of the pulse

analogWrite(0) = 0% pulse width, ie always off
analogWrite(255) = 100% pulse width, ie always on

Any value between 0 and 255 will give a proportional pulse width

Your example 1 code would not turn off the PWM signal during the delay(), it will just delay the change between 0% duty cycle and 50% duty cycle

Have you got access to an oscilloscope ?

Are you trying to control the volume of a buzzer? What kind of buzzer is it?

Verify your 'pin' supports PWM on your Arduino platform. Reference:

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

UKHeliBob:
Yes

The clue is in the name
Pulse
Width
Modulation

The analogWrite() parameter determines the width of the pulse

analogWrite(0) = 0% pulse width, ie always off
analogWrite(255) = 100% pulse width, ie always on

Any value between 0 and 255 will give a proportional pulse width

Your example 1 code would not turn off the PWM signal during the delay(), it will just delay the change between 0% duty cycle and 50% duty cycle

Have you got access to an oscilloscope ?

I am confused how i would get a 50% duty cycle then. Dont you need it to be on and off, as shown in a square wave.
as i am working from home i do not have an oscilloscope . I had tried to get a virtual/software version of one but had no luck.

With example 1, you'll probably hear the buzzer beeping at 16.7Hz at 50% volume.
For example 2, buzzer remains ON at 50% volume.

Example 1
Off time to create a 50% duty cycle ;

analogWrite(pin,127);   ← buzzer turns ON at 50% volume 
delay(30);              ← buzzer remains ON at 50% volume for 30ms
analogWrite(pin,0);     ← buzzer turns OFF
delay(30);              ← buzzer remains OFF for 30ms

Example 2
Simply just the one line of code;
analogWrite(pin,127);   ← buzzer turns ON at 50% volume

Please post a link to the buzzer.

There are many types: some buzz simply when powered, others require PWM of a certain frequency.

I am confused how i would get a 50% duty cycle then

analogWrite(pin,127);

i have been speaking to my manager about this, and he says I will need to add a off time for this to give a 50% duty cycle.

Well, technically, 127/255 = 49.8% duty cycle
Practically, you won’t be able to hear the difference of 0.2%.

Is the requirement really to get a pure square wave (PWM at 50.0%)?
If so, then you’ll need to use a library or do some low level programming.

UKHeliBob:

analogWrite(pin,127);

So if i write that, in an Oscilloscope. In one Cycle/Period the buzzer will be half off and half on?
Too many people have given different opinions, i just need to know if I need to turn it off myself in the code like what i did in example 1.

jacklythgoee:
Too many people have given different opinions,

…working on too little information.

See reply#6

TheMemberFormerlyKnownAsAWOL:
If your buzzer...working on too little information.

See reply#6

Not too sure what it is exactly. The ones i am using are passive buzzers i am pretty sure but one of the buzzers i have is from an elego starter kit. i then have a KY-006 Passive Piezo buzzer alarm modul.

jacklythgoee:
So if i write that, in an Oscilloscope. In one Cycle/Period the buzzer will be half off and half on?
Too many people have given different opinions, i just need to know if I need to turn it off myself in the code like what i did in example 1.

If you use that then the buzzer will be on for half of the PWM cycle and off for half of the PWM cycle

Depending on which Arduino and which pin you use the frequency will be different

See analogWrite() - Arduino Reference

However, the effect on the buzzer will depend on the type of buzzer. If it is a passive buzzer then it will output the appropriate PWM frequency. If it an active buzzer then it will output its native frequency for half the time at the PWM frequency

What type of buzzer is it exactly ? Active or passive ?

UKHeliBob:
If you use that then the buzzer will be on for half of the PWM cycle and off for half of the PWM cycle

Depending on which Arduino and which pin you use the frequency will be different

See analogWrite() - Arduino Reference

However, the effect on the buzzer will depend on the type of buzzer. If it is a passive buzzer then it will output the appropriate PWM frequency. If it an active buzzer then it will output its native frequency for half the time at the PWM frequency

What type of buzzer is it exactly ? Active or passive ?

passive im pretty sure

jacklythgoee:
passive im pretty sure

What happens if you put 5V across the buzzer ?

If it clicks then it is passive
If it buzzes then it is active

UKHeliBob:
What happens if you put 5V across the buzzer ?

If it clicks then it is passive
If it buzzes then it is active

Yes it is a passive buzzer

jacklythgoee:
Yes it is a passive buzzer

Then given a PWM signal it will output the PWM frequency at the on/off ratio specified by the analogWrite() parameter. Why not try it for yourself with a simple sketch that does nothing but output a PWM signal to the buzzer, which is really a speaker ? Sweep through the analogWrite() values using a for loop to hear the effect of changing it but put a delay() of say 100 after each analogWrite() so that you can hear the changing tones

UKHeliBob:
Then given a PWM signal it will output the PWM frequency at the on/off ratio specified by the analogWrite() parameter. Why not try it for yourself with a simple sketch that does nothing but output a PWM signal to the buzzer, which is really a speaker ? Sweep through the analogWrite() values using a for loop to hear the effect of changing it but put a delay() of say 100 after each analogWrite() so that you can hear the changing tones

So you're saying i create a for loop that increase up in increments of 5 for example. from 0-255?

jacklythgoee:
So you’re saying i create a for loop that increase up in increments of 5 for example. from 0-255?

Yes. Choose any increment you like and experiment

You don’t even need to use a for loop if you let the loop() function do the looping for you and let the byte value overflow back to zero after 255

I would use the tone() function to activate the buzzer. Select the optimal frequency.