Go Down

Topic: Pinging (literally) (Read 1 time) previous topic - next topic


Yesterday, I made a cool thing that could be the beginning of a synth.  It works with one piezo (or speaker thru a transistor) and doesn't just beep, but makes a pinging noise, sort of like a plucking noise.  I'll post the code soon.  (I'm typing on my iPod right now).


Here's the code:
Code: [Select]
const int CC = 956;
const int BB = 1014;
const int AA = 1136;
const int GG = 1275;
const int FF = 1432;
const int EE = 1519;
const int DD = 1700;
const int cc = 1915;
int tones[] = {
 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
int tones2[]  = {
 956, 1014, 1136, 1275, 1432, 1519, 1700, 1915 };

float vols[] = {

int speakerPin = 9;

void setup()  {
 pinMode(9, OUTPUT);

void loop()  {
 for(int x = 0; x < 8; x++)  {
   for(int y = 0; y < 36; y++)  {
     playTone(tones[x], vols[y] / 10);
   digitalWrite(9, LOW);
 for(int x = 0; x < 8; x++)  {
   for(int y = 0; y < 36; y++)  {
     playTone(tones2[x], vols[y] / 10);
   digitalWrite(9, LOW);

void playTone(int tone, int volume) {
 for (long i = 0; i < 10000; i += tone * 2) {
   int num = map(volume, 0, 255, 0, tone);
   digitalWrite(speakerPin, HIGH);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone - num);



That is really cool! Just tried it and it is pretty wild. It is a lot more pleasant than a normal piezo buzz.


Aug 23, 2009, 11:34 pm Last Edit: Aug 23, 2009, 11:35 pm by amacmullen14 Reason: 1

I'm ordering an ultrasonic range sensor, and I hope to make a theremin-like-thing!
(Ping the sensor to make a ping!)

By the way, did you use a speaker?

Currently, I have a speaker wired like this:

Digital pin 9 to base of transistor
Collector to 5v
Emitter to speaker
Speaker to ground

I also have a diode parallel to the speaker, with the anode to the base of the transistor, for extra safety.  Is this necessary?
I put it there since the speaker is essentially a coil of wire.


Aug 24, 2009, 12:00 am Last Edit: Aug 24, 2009, 12:01 am by dh1ao Reason: 1
is it something like this?

The diode makes sense, sure. But I would be concerned about the power dissipation of the transistor if you switch it on for more than a millisecond or so. After the first change in potential the speaker acts like a very low ohm wire rising the current through transistor and speaker. You will see which one will give signs of smoke first  :o


Oh.  But in this application of the speaker, could anything bad happen?


Hi again

you wrote

I also have a diode parallel to the speaker

That is what I tried to sketch

and than you wrote
with the anode to the base of the transistor

??? Than the diode is not in parallel to the speaker.

You should limit the current through the collector with a resistor with at least 100 ohms 250 mW


Aug 24, 2009, 02:37 am Last Edit: Aug 24, 2009, 03:58 am by amacmullen14 Reason: 1
Oh! Sorry.  I meant the emitter

And I didn't really understand what you said.  I'm no electronics expert.  If this circuit is bad, can you show me a better one?


Aug 24, 2009, 07:24 pm Last Edit: Aug 24, 2009, 07:24 pm by dh1ao Reason: 1
Hi sciguy

just a rough sketch

I guess you want some sound out of the speaker so DC current isn't necessary? Therefore the capacitor. If the sound is to low you could resize R2 to 220 ohms or 100 ohms. Sure there much better circuits, my one maybe used as a guide  :o



Aug 24, 2009, 11:56 pm Last Edit: Aug 24, 2009, 11:59 pm by amacmullen14 Reason: 1
OK, thanks.
Couple questions:

Is the thing under the 5v is the output pin?
Do I need to use an external battery?  (it is a small speaker, maybe 2" diameter)


Hi sciguy

so sorry, ehm as you see (and surely you guessed it by my bad english) I'm from europe ;D
The symbol you asked for here is a symbol for common ground.

I used an external power supply for the sketch. I'm pretty sure you can use the Arduino 5V. The current drawn from it will be far below 500 mA.
Not sure how USB supply acts to sudden impulses when the capacitor is charged/decharged. Worth a try, your PC will take no harm.

As mentioned there's surely much better circuitry...


Go Up