Hi! I'm using analogWrite() function in my program to play my buzzer at different frequencies (I purposely not using tone() and other functions; I want to play using analogWrite() only if possible).
But the problem is when I use analogWrite(), the buzzer does not stop and keeps on playing the sound (infact, it just plays the last frequency and gets stuck to it). Can someone please tell me why am I facing this problem and what is the solution for this?
I tried analogWrite(buzz_pin, 0) right after the myDelay() function but still it does not stop the sound. It continuously gives the sound output.
I thought maybe with analogWrite(), I can control the frequency and thus output sounds of desired frequency.
And also, tone() was not working on my board.
(PS: I am using atmega32-a via arduino IDE. All the other arduino functions work well on testing except for tone() )
You have an "active" buzzer that buzzes at a fixed frequency whenever power is applied, you want a "piezo" buzzer that just makes 1 click when power is applied, then you can use the Tone function to make music.
440 clicks per second makes a raspy but tolerable "A".
I thought maybe with analogWrite(), I can control the frequency
analogWrite produces a fixed frequency, but a variable duty cycle (ratio of "on" time to "off"time).
You're clearly not using a Uno, so the limitation of not having PWM on pins 3 and 11 available when using tone doesn't apply, but you didn't say which board you are using.
I am using atmega32a. I have made the corresponding pins_arduino.h and did the changes in boards.txt. All the ports are working fine via arduino. I tried using tone() with "pitches.h" file but didn't get any output.