Piezoelectric crystal generates electrical signals when a time varying pressure / force is applied. You need to give a time varying force for getting a time varying signals and perhaps vice versa.
Hope you can get something useful from the Command Snippets. The important thing is devide a period into 16 to 20 steps. The period should be 1/f, where fmeans the frequency you needed, In my codes, it stands for the resonant frequency of a ultrasonic transducer.