Frequency sweep

I am trying to do a continuous sweep . each sweep will be completed within few hundred milliseconds. it will go through square waves between 500-5000Hz.
image
something like this
I thought of using PWM instead of tone library because I thought I can more efficiently control the sweep nature.
after the sweep is completed I need to wait around a hundred ms to measure the resonant freq.