Change the frequency of a buzzer

How to change the frequency of buzzer?

Thanks for the question.
Hope this answer helps :slight_smile: :

The possible commands are below,

  • digitalWrite
  • analogWrite

pin - pin number where you want to send the command. It could be

  • A0, 0, 1, 2, 3, 4

value - The state of the pin. It could be

  • HIGH - For digital pins
  • LOW - For digital pins
  • 0-255 - For analog pin

Here is the full code for the change the frequency of a buzzer given below :-



Here in the button “ON” ,
analogWrite(0, 250) i.e. 0 is the pin number and 250 is the value.
So, you can give any value in between 0-255 in place of “250”.
And in the button “OFF”
analogWrite(0, 0) i.e. the pin number is 0 and the value also must be 0 so when you click on the “OFF” button there should not be any sound from the Buzzer.

So, I hope it will help you. And never hesitate to ask anything when you counter any problem.
Good luck and best wishes to you for the rest of the course as well.

Sorry , did’t give the code .The full code can be seen in the code of project 3 you just have to change
digital to analog.

should we have to change the circuit connections?

No, the circuit remains the same. (remove the resistor if you have connected it)
If you want to change the output, give different values for the analog write function.

Like, below
analogwrite(0,x) where x can vary from 0-255. then you will be able to hear the different type of sounds
You can make different buttons for different values of the analog function.