Page 1 of 1
					
				Buzzer tones.
				Posted: Mon Oct 27, 2025 8:43 pm
				by Billduck1302
				I would like to play several notes with the buzzer component. For example, Close Encounters is 393,516,476,361,393 Hz. Sort of!
 I see that the buzzer frequency is a property of the component. 
So my question is "How can I change the frequency when running a program". The frequency is not available as a variable.  
I'm sure it is in there, but I can not find it.
This is a great tone generator, that I can use to play the tune. <
https://www.szynalski.com/tone-generator/?
 
			
					
				Re: Buzzer tones.
				Posted: Tue Oct 28, 2025 10:54 am
				by mnfisher
				The buzzer component only turns a pin on (or off) to play a tone - which is a feature of the buzzer hardware....  The frequency value is only for simulation.
It might be possible to alter the tone using PWM either with the buzzer device or by outputting directly (via a transistor or amplifier) to a speaker...
There was a couple of example of this in the forums.
Martin
			 
			
					
				Re: Buzzer tones.
				Posted: Tue Oct 28, 2025 5:14 pm
				by medelec35
				Hi.
The PWM code within UNO is currently not accurate.
I have created a project to give you great accuracy for 4 PWM pins.
you  can use that to generate required tones.
This is a screen shot of the notes I added within Main.
			
		
				
			 
- PWM notes.jpg (27.44 KiB) Viewed 166 times
 
This is how you set pwm to a specified pin frequency and duty.
			
		
				
			 
- Uno Accurate PWM.png (27.67 KiB) Viewed 166 times
  
			
					
				Re: Buzzer tones.
				Posted: Tue Oct 28, 2025 11:18 pm
				by Billduck1302
				It works, thank you. 
How would I go about giving the tone a duration?
Also, Is the PWM C code a component? Obviously I don't get it.
			 
			
					
				Re: Buzzer tones.
				Posted: Wed Oct 29, 2025 8:10 am
				by medelec35
				You're welcome.
When you use the PWM function the note will remain on.
you then can use a delay for the duration then to  silence you can use a duty of 0 as that will just keep the pin low.
Alternatively after the duration of the first note, just add another PWM call function to set the PWM to the next frequency (note).
I could add another parameter for duration, but wanted to keep the call function a simple as possible.
No the PWM Flowchart is not a component, it's just a PWM generating project that makes PWM on the UNO much better than it currently is. 
It is just a test project.
			 
			
					
				Re: Buzzer tones.
				Posted: Wed Oct 29, 2025 5:11 pm
				by Billduck1302
				Thanks again. I will try that.
Oddly enough, with the attached program, when I add 2 Buzzer components,
 with different frequencies, and test it, I get the same frequency sound.
			 
			
					
				Re: Buzzer tones.
				Posted: Wed Oct 29, 2025 8:52 pm
				by medelec35
				You can't use the Buzzer component as  mnfisher has stated for your application.
you can use my project file to do what you are after.
For example continuously changing from one tone to another, the attached will do that for you.
I have made it as simple as I can.
			 
			
					
				Re: Buzzer tones.
				Posted: Thu Oct 30, 2025 8:13 am
				by medelec35
				Hello.
The buzzer component does not rapidly switch on and off at the set frequency during the on period to generate a tone for a speaker or piezo.
The frequency is for simulation only.
Martin stated that:
mnfisher wrote: ↑Tue Oct 28, 2025 10:54 am
The buzzer component only turns a pin on (or off) to play a tone - which is a feature of the buzzer hardware....  The frequency value is only for simulation.
 
All the buzzer does when on  is set a  pin high (polarity Active High)  or low (Active low), then the pin is reverted to the non active state.
In effect it's like the LED component with a delay and sound simulation.
That means you will need to connect a fixed frequency buzzer i.e. it buzzes when you connect a steady DC supply to it.
This is not your intention, so you will need to go down the PWM route for a loudspeaker and the appropriate buffer component, if the current draw exceeds maximum current draw of the microcontroller.