1
\$\begingroup\$

I want to drive a speaker, in series with a 10uF electrolytic capacitor, from a microcontroller and was wondering what will happen when the GPIO pin goes low, given that the capacitor will try to discharge. Is it safe ? Or do I need to create an alternative path to ground with a resistor ?

\$\endgroup\$
  • \$\begingroup\$ Would you short an output pin to ground and drive it high? Use a buffer transistor and don't expect miralcles of high fidelity! \$\endgroup\$ – Andy aka Apr 2 '17 at 19:56
  • \$\begingroup\$ Why the 10 uF capacitor? If you want a low pass filter, then you need a resistor as well as a capacitor (for a classic RC LPF). Otherwise, this seem to by an XY problem. \$\endgroup\$ – uint128_t Apr 2 '17 at 20:07
  • 1
    \$\begingroup\$ @Andyaka I'm not building some high fidelity or any kind of device, just trying to familiarize myself with MCU programming and wanted to produce some tones. \$\endgroup\$ – fholly Apr 2 '17 at 20:55
  • \$\begingroup\$ @Andyaka, don't use a buffer transistor as it won't give miracles of push-pull. You need two transistors for that. If you're going to criticise, don't look silly doing it. Besides that, experimentation and learning is to be guided and encouraged. \$\endgroup\$ – TonyM Apr 2 '17 at 21:19
  • 2
    \$\begingroup\$ I'm assuming op is using the series capacitor as a dc bias blocking cap for the Audio signal. \$\endgroup\$ – Passerby Apr 2 '17 at 21:31
2
\$\begingroup\$

No it is not safe. The MCU is going to malfunction at the long run.

As an example, for AVR microcontrollers sink current must not be bigger than 20ma. So you have to make sure that the discharging current will not go higher.

I would recommend to connect the speaker through a driver which could be a transistor or an amplifier.

\$\endgroup\$
0
\$\begingroup\$

With the MCU I/O pin configured as an output, it will use a push-pull ('totem pole') driver. This consists of a FET from the chip's Vdd supply to the I/O pin and another FET from the I/O pin to the chip's GND supply.

When the pin is driving high, it sources (outputs) a current to Vdd and when driving low, it sinks (inputs) a current to GND. These currents should be stated in the datasheet, usually as IoH(max) and IoL(max) respectively. If the load exceeds these specified currents, the FETs will limit the current flow and dissipate power based on the voltage drop across the particular FET and the current drawn. If maintained for long enough, this could damage the MCU.

However, with a switching action your audio experiment/application will require, potential damage is unlikely to be a problem. You may not get the behaviour you hope for but it will be fine to experiment with.

\$\endgroup\$
  • \$\begingroup\$ I've done the same thing -- hooking up an I/O pin, via an external resistor to increase the series resistance a bit followed by a cap, to a small speaker. No problems. However. I did measure spike voltages at the ext. resistor/cap node a few volts below ground and above my rail due to the speaker parasitics (which are rather complex and frequency-varying.) Without the external series resistor, the I/O pin would have been directly exposed and the protection diodes active. They have limits (under 2 mA, usually, these days.) At least an added external R would help limit that diode current. \$\endgroup\$ – jonk Apr 3 '17 at 6:00
  • \$\begingroup\$ @jonk, interesting stuff and the road I'd sort of hoped the OP will go up as their circuit develops. I'd imagined their circuit in a month won't look much like today's but they'd have got lots of learning out of it, with some reassurance along the way. Thanks. \$\endgroup\$ – TonyM Apr 3 '17 at 9:55
  • \$\begingroup\$ For hobbyists, I'm less worried about over-taxing an I/O pin. (Obviously, it is important to worry about total port current limits and heating.) Alone, all I've seen is that they fail to meet their voltage specs when driving hard (and heat up a tiny spot in the IC.) The designers know they have to dedicate LOTS of aluminum resource to high-current I/O pins. So there is a lot of margin. What worries me are the tiny protection diode traces that also connect to the I/O pin that become active when pin voltages go outside the rails. And possible latch-up. Adding external R helps address it. \$\endgroup\$ – jonk Apr 3 '17 at 17:30
  • \$\begingroup\$ @jonk, good observations, I agree. At the same time, they are so much tougher than the microcontrollers I started out using - the 8048's and 8051's. I looked up my NXP LPC1114 I/O output current and it's 45 mA. 74HC/HCT pin diodes are rated for 20 mA continuous. These things can carry stuff. And I wouldn't be surprised if the OP plays out making I/O noises like this and adds in a power op-amp to the speaker, or uses 5 mW hundreds-ohms headphones from the I/O pin. If the I/O goes, they'll try something else. I hope they get your good experiences from this :-) \$\endgroup\$ – TonyM Apr 3 '17 at 18:57
  • 1
    \$\begingroup\$ The LM386 would be a cheap option. One could just duplicate it's output section with a few BJTs. Two I/O pins could drive two of glen's BJT-pair outputs with the speaker in between for more delivered speaker power, if needed (180 degrees out of phase, of course.) Basically, just driving the speaker with an H-bridge. In fact, could just use H bridge parts like the UC2950, I suppose. Or just get a TDA8551 which is a fancy BTL design, arranged for 5 V at 1 W output, and includes a volume control (I use them.) There's even class-D brds available these days for almost nothing. \$\endgroup\$ – jonk Apr 3 '17 at 19:19
0
\$\begingroup\$

Majid_L makes sense - a buffer stage will stress your microcontroller's I/O pin less. An 8-ohm speaker will ask for current above permissible limits at times (for moments after switching high or switching low) when driven directly. A buffer can be simple, like this:

schematic

simulate this circuit – Schematic created using CircuitLab With the transistor buffer, the I/O pin will see about 800 ohm load, instead of 8 ohms (assuming transistor current gain is 100). The transistor type is not critical, but the bottom one must be PNP, top one NPN.
C1 is added because lots of pulse current is pulled from the 5V supply by the transistors. You don't want these pulses to reduce the microcontroller's Vdd, else erratic operation (or brownout) may occur.

\$\endgroup\$
  • \$\begingroup\$ I think the 10\$\mu\$F cap that the OP mentioned is a bit small for that kind of load. Given the recommendation to beef it up with the BJTs, may as well then recommend an increased capacitance while at it. \$\endgroup\$ – jonk Apr 3 '17 at 6:04
  • \$\begingroup\$ @jonk That's a good point - for an audio application, capacitors of 100uf would provide telephone-type frequency response. But for tone frequencies above 2 kHz, 10uf is OK. \$\endgroup\$ – glen_geek Apr 3 '17 at 18:01
  • \$\begingroup\$ Well, \$\tau=10\:\mu\textrm{F}\cdot 8\:\Omega= 80\:\mu\textrm{s}\$. Even 1\$\tau\$ means about 63% droop across the cap towards the new equilibrium state, already. That's all that was sticking in my mind here. So yeah, frequency matters. 2 kHz is at least several \$\tau\$, with only 10\$\mu\$F. That still worries me. \$\endgroup\$ – jonk Apr 3 '17 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.