# Change the frequency of a buzzer

How to change the frequency of buzzer?

1 Like

Thanks for the question.

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.

5 Likes

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.

6 Likes

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.

How to change the frequency of buzzer to a varying freequency (by using some functions)? Is it possible?

Thank you. It helped.

Absolutely, the values ranging from 0-255 would change the frequency of the Buzzer.

I have a small doubt in it…
Is it not necessary that we use the function digitalWrite function for digital pins alone (from 0 to 4) and the analogWrite function for analog pins alone(A0).
If this isn’t the fact, then why ‘A0’ name stands out from all other pins ‘0,1,2,3,4’ name if both these functionalities comply as GPIO pins…
I want to know why it is differentiated!!!
Hope my query isn’t silly and my doubt is cleared:grinning:

1 Like

Hii @tvmukhil
One can always use analog pins for digital writings. Functions such as digitalRead() and digitalWrite() work on all the pins. However analogRead() is applicable only to analog pins. But the analogWrite() function apart from being applicable only to analog pins, they also are applicable for digital PWM(Pulse Width Modulation) capable pins. And all the 5 digital pins in Bolt Wifi Module are capable of PWM.

Hope this information helps!

4 Likes