I desoldered a component, HY-05 Electro-magnetic sound generator, from a broken motherboard. Its DCV is 3~8V, operating frequency 2400 plus or minus 200 Hz. I can get a small metallic sound with the operating voltage probably because the frequency is wrong. How can I adjust the frequency in the power supply of 50Hz? If the question is bizarre, it may be that the component is just broken.
You could hook it to the output line of a PC serial port, set the port for 4800,n,8,1 and transmit 0x55 bytes at it all day. If you draw out the waveform you'll see that back to back 0x55 bytes will result in a square wave at half the bit rate. Using 8,n,1 settings means it takes 10 bits to send a byte, so at 4800bps, that's 480 chars/second, so each 0x55 that you send would get you a hair more than 2msec of 2400Hz square wave.
If it is indeed 'electromagnetic' then it really shouldn't matter about the polarity, but you should use limiting resistors to keep from exceeding the voltage ratings. To keep the peak-to-peak voltage down where it would be with a single-ended 5V square wave, you'd need to trim the +/-12V swings of the serial port down to +/-2.5V .. or if you had an RS-232C port (i.e., already TTL level) you wouldn't have to mess with resistors at all.
Actually, if this part is like a tiny dynamic speaker (which the 40ohms suggests), then you should probably worry more about keeping under the current maximum. It's doubtful that the internal insulation would be remotely close to breakdown at 8V, but it is probable that the fine gauge wire would melt if you push too much current through it. If you have to use resistors, start large and work your way down.