Yes, use a timer, and APIs to set/ change its period.
Also if using voice coil speaker, use clamp diodes on pin, or
better yet use a single transistor buffer with a clamp on speaker.
If you don't you will get transients on pin and blow up the part.
If you use latter, transistor, limit its base current with an R, same
for collector in series with speaker.