Howto make sound with DC motors
I am just start to play around with the new Arduino Tone library, when I realize that I have no sound transducer laying around here. Than I remember an old hack that someone did with the Asuro robot (many thanks to ArexxHank and stochri). Making sound with the DC motors. So I grab one of my actual robots, the Ardubot and began to port the sound function to my current setup. Mixed together with the examples from the Tone library and here is the result.
How it works:
The trick is quite simple and should be work with every robot that uses a normal H-Bridge as motor driver. PWM is not needed here, just the direction pins to move the motors forward and backward. If you use PWM for your H-bridge, you can additionally control the amplitude of the generated sound. You need to switch the motors between forward and backward with the desired frequency. The motor will not start running, just begin beeping at this frequency. The frequency range is not as wide than a speaker, but it's ok.
Here is the playSound function:
void playSound(uint16_t freq, uint16_t duration_msec, uint8_t amplitude)
period_usec = 1000000L / freq;
duration_usec = 1000 * (uint32_t) duration_msec;
k = duration_usec / period_usec;
motorSpeed (amplitude, amplitude);
for (n = 0; n < k; n++)
motorDirection (DIR_FORWARD, DIR_FORWARD);
motorDirection (DIR_REVERSE, DIR_REVERSE);
motorSpeed (0, 0);
Here is the schematic: