Is it safe to apply a small voltage to a GPIO pin in output mode?

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 ?

• Would you short an output pin to ground and drive it high? Use a buffer transistor and don't expect miralcles of high fidelity! – Andy aka Apr 2 '17 at 19:56
• 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. – uint128_t Apr 2 '17 at 20:07
• @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. – fholly Apr 2 '17 at 20:55
• @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. – TonyM Apr 2 '17 at 21:19
• I'm assuming op is using the series capacitor as a dc bias blocking cap for the Audio signal. – Passerby Apr 2 '17 at 21:31

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.

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.

• 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. – jonk Apr 3 '17 at 6:00
• @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. – TonyM Apr 3 '17 at 9:55
• 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. – jonk Apr 3 '17 at 17:30
• @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 :-) – TonyM Apr 3 '17 at 18:57
• 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. – jonk Apr 3 '17 at 19:19

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:

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.

• 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. – jonk Apr 3 '17 at 6:04
• @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. – glen_geek Apr 3 '17 at 18:01
• 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. – jonk Apr 3 '17 at 18:08