I'm new here and I'm more or less new to electronics and to Arduino specifically. I'm mostly playing around with connecting different components and trying to get stuff working.
For another of my little sandbox projects, I decided to use an ancient piezo speaker that I had cannibalized from some old device a long time ago (probably a playing postcard or something but I don't remember anymore).
When connecting directly to an Arduino's pin (both to a Yun and a cheap Nano clone) and e.g. using the toneMelody example, I'm able to play some tones on the little speaker.
And here's the thing - I read in a few places, both here and around the net, that you shouldn't connect a speaker directly to the microcontroller since you can damage its pins by drawing too much current. I have no idea whether that's also true about the piezo speaker but just to try things out, I decided to try connecting it through an transistor.
I'm connecting the base to the pin, the emitter to ground and the collector to the speaker (the other connector from the speaker is connected to +5V). And then, what I get is almost a silence - the tones play all right - but the output is very silent.
Interestingly, when I connect a LED that way, it lights up normally (when outputting HIGH to the pin), just as it does when connected directly (of course, it's connected the other way round with regards to polarity in the latter case).
I'm probably doing something stupid but if anyone could point me in the general direction so I could learn why the signal, when put through a transistor is much more silent than the direct one - especially in the face of the fact that people suggested connecting speakers through a transistor to get a louder signal - I'd be thoroughly grateful.
The maximum current from an I/O pin is 40mA. From [u]Ohm's Law[/u], 5V / 0.04A = 125 Ohms. That's the minimum resistance (or impedance) so you can't use a regular 8-Ohm speaker.
A Piezo transducer is effectively a (small) capacitor, and it's generally OK. If you are not sure, put a 125 Ohm (or slightly greater) resistor in series.
The impedance ([u]capacitive reactance[/u]) of a capacitor depends on the capacitance (Farads or microfarads) and the frequency. I don't know what the effective capacitance of a typical piezo is, but it should be OK at audio frequencies.
I'm connecting the base to the pin, the emitter to ground and the collector to the speaker (the other connector from the speaker is connected to +5V). And then, what I get is almost a silence - the tones play all right - but the output is very silent.
That should "work" with an NPN transistor, but you need a current limiting resistor in series with the transistor base. Again 125 Ohms would be the minimum, but 1K should be fine.
What's the frequency? With a typical piezo, you should get a fairly loud signal at around 2kHz. As you go lower, the piezo looses efficiency and if you go too high your ear looses sensitivity or you can go beyond the human hearing range and you won't hear anything.
Still, the question that bugs me remains and it was the reason for coming here in the first place:
Why is it that the sound is much more silent (almost inaudible - I had to listen very closely to even notice it's producing any sound) when the speaker is driven through a transistor as compared to being driven directly (not very loud but clearly audible)?
You might have a defective transistor, a PNP transistor, or you've misidentified the base, collector, and emitter.
Or, maybe the voltage is being pulled-down by the lack of a current limiting resistor. i.e., Excess current into the transistor base but less current through (and less voltage applied) to the piezo. But, if that were the problem your Arduino would probably crash.
Sadly transistors never followed common design. Some have E-B-C when you're looking at the flat side, leads down. Some are other way around. And some can even be B-C-E or whatever.
I do have a printed list of a few common transistors I have with pinouts so I can make sure I have it done right when I am using something other than the common 2N3904 or 2N3906
Well, I used an NPN transistor - a BC547B and I had consulted its datasheet before connecting.
And anyway, as indicated above, when I connect an LED through it, it's powered correctly when I output HIGH and powered down when I output LOW, so my naive reasoning suggests, the transistor is doing its job and the I have the leads connected in a correct way.
I was wondering - could that e.g. have anything to do with a response time maybe? ...
Since the piezo device is a capacitor, if you connect it between the collector and Vcc with no other connection to the collector (such as a pull-up resistor of 1k), then no current can flow.
Paul__B:
Since the piezo device is a capacitor, if you connect it between the collector and Vcc with no other connection to the collector (such as a pull-up resistor of 1k), then no current can flow.
I think this is your problem. You could try putting an appropriately sized resistor in parallel with the piezo speaker to provide current bias to the transistor. There's probably a way to optimize it, but Paul's suggestion of 1k sounds reasonable enough.
That said, I don't think you need an external driver for this sort of device.
Here is a trick to basically double the drive to the Piezo speaker - this is a diagram of a demo board used for PIC processors, but if you look at the piezo drive circuit in the left middle of the schematic, you can see how they do it - basically, the transistor inverts the signal, so when drive on one side of the piezo is high, the other side is low and when the drive goes low, the other side goes high. Works surprisingly well (you can also use an h-bridge etc to achieve the same result).