Controlling a device using an ESP 32

I want to use one of the output pins of an ESP 32 to control a device that requires 12 v and uses about 70 ma. I do not want to use a mechanical relay. The ESP puts 3.3 v on the pin and seems to be regulated to 20 ma.

This device outputs an audio signal to an 8 ohm speaker. Best case would be to control the audio circuit by making and breaking the connection in one of the speaker leads but I don’t know how to measure the voltage and current of this circuit.

I’ve already made a couple of false starts, rather unsuccessfully, so I would really appreciate ant help I can get.

The lowest cost and highest fidelity method electronics engineers have used to switch speaker leads on/off for a century now is with an electro-mechanical relay.

AFAIK, even 100 years later, the major practical downside to electro-mechanical relays for speaker switching is the limited lifespan (~100,000 operations). So if you need to switch on/off once per minute (1440 times per day) it may only last ~70 days. Usually speakers only need to switch on/off a few times per day, so a relay can provide decades of zero maintenance high fidelity switching.

What specific reason do you have for not wanting to use a relay for the solution?

How much extra parts cost can you tolerate to avoid a relay in the design?

Can you switch the power to the amplifier, or a low level audio signal, instead of the speaker connection?

PS - I love designing one off custom audio accessories and have done it as a hobby for over 50 years. Can’t remember how many speaker switching circuits I’ve designed in that time for myself, friends, and family.

1 Like

Thank you for your reply.

There are two reasons for avoiding a mechanical relay. I can switch the power to the sound device as it only needs about 70 ma but there is a slight delay when it powers up. Wouldn’t be the end of the world, and is probably better as the device wouldn’t be running all the time with no load on the speaker connections. The other reason is the clicking of the relay. It bothers me.

Then again, if the cost is noticeable, I am willing to use one. With 50 years of experience, I am more than willing to accept your recommendations.

The sound device is a small, low power output device, monaural that plays a loop of the required sound.

This whole mess is to be used on a model railroad layout, as a grade crossing signal. I developed a mechanical system, using 12v relays, that works, but… There are several problems with it. The sound of the relays, and the fact that the LED lights go on and off instantaneously, which is not like the real crossing signals. Got lost in YouTube and came across a video showing the wonders of the ESP 32 microprocessor and thought “I can do that!”. I wrote a sketch that controls the lights exactly as I want them to work. Then I discovered that the ESP 32 doesn’t have the sound capabilities I need without a bunch of other stuff. So I decided to continue to use use the existing sound module. And here I am.

Thanks for the added details.

Ah the click, or sometimes much louder clunk, of most electro-mechanical relays. That is definitely something to be avoided, or at least very much minimized, for a model railroad layout.

I’m going to assume that the audio output power of the amplifier is 2 Watts or less.

Given the low power of the audio, need to switch audio, and usefulness of small size, let me introduce you to the miniature reed relay, they are nearly silent.

Since you have 12VDC available I’ll use that for the relay coil because the 10V or lower must operate spec allows lots of voltage drop so nearly any NPN transistor will work for the 3.3V to 12V level convertor.

I’m choosing this one because it’s low cost, can switch 10 Watts maximum, is a brand I’ve used for 40 years (Hamlin was bought by Littlefuse), and comes in an easy to use DIP package.

Here’s a schematic and parts list:

Quan Ref Des Name Value Manufacturer Mfg Part # Digi-Key Part # Description
1 D1 Diode 1N914 onsemi 1N914 1N914FS-ND DIODE GEN PURP 100V 200MA DO35
1 R1 Resistor 10k Stackpole CF18JT10K0 CF18JT10K0CT-ND RES 10K OHM 5% 1/8W AXIAL
1 Q1 NPN Transistor 2N3904 onsemi 2N3904BU 2N3904FS-ND TRANS NPN 40V 0.2A TO92-3
1 RY1 RELAY, SPST Reed Relay Littelfuse Inc. HE721A1200 HE102-ND RELAY REED SPST 500MA 12V

I made the project public so you can copy it and/or add the parts to a cart/list if you want.


  • Any general purpose small signal transistor should work
  • Any small signal diode should work
  • I haven’t done the full math but I believe any resistor value from 1K to 75K should work
  • For $0.47US more, you can get the relay version with the snubber diode built-in. The built-in diode would improve the snubbing performance.

Thank you so much for your reply!
You are correct that the audio output is < 2 w.
Looking at the package of the relay, I’m willing to be I won’t be able to hear it operate, which makes it acceptable.
Looking at your schematic, am I correct that the 3.3 v supply ground and the 12 V ground can be a common connection? I am referring to the ground symbol at the bottom of the transistor.
And, I can use this relay in series with the speaker?

Which relay contains the snubber diode>? The HE721A1200 or the HE721A1210?

Again, thank you!

Just out of curiosity, would it be possible to replace the diode and relay from your circuit, and add the sound device in their place? The sound module uses, measured by my multimeter, 68ma when connected to 12 v.

Reed switches are the traditional switches used for windows with burglar alarms. You usually have to get you rear right next to the switch to hear the very tiny click when it turns on.

Yes, it must be a common ground.

Yes, you need the relay’s switch contacts connected in series with the speaker’s positive wire.



Possibly, the transistor choice becomes a little more critical and the resistor value gets very critical at the higher current level.

There are other details about the sound module that will be needed to examined to be sure.

What sound module is it?


Here’s the site for the sound module.

I think I’ll just stick with the schematic you sent. :slight_smile:

Going to place an order tonight!


1 Like

Well, the 2N3904EC-ND transistor is 0 stock! :frowning:
So, after looking at the specs sheets, I chose 2N4401-ND.
I placed the order. Did I do right?

1 Like

I had 2N3904BU (2N3904FS-ND) in the list, there are over 13,000 of those in stock.

2N4401 has half the gain but it should work just fine.

1 Like

Very nice sound modules for train models.

They have a switch input to give a single play.
Connect the relay contacts to the switch input, program the ESP32 to act like a human pressing the button, and you’ll be all set.

  • When you want to play the sound once, you turn on the relay for a fraction of second then turn the relay off.
  • If you leave the relay on, the sound loop will be continuous.

Wish I’d seen the module before, I’m pretty sure a single resistor and a 4N25 optocoupler would also work for activating the switch input (it wouldn’t work for switching the power or speaker).

Well I finally got time enough to get back to this project. And It works just like you said it would! I’m glad I used a relay as I ended up needing to use it as the speaker connection. Using it to power the sound module resulted in an excessive delay as the module powered up. So I just powered the module and used the relay to connect and disconnect one lead of the speaker.

Now, I just need to figure how to replace the solderless board with something more robust. :slight_smile:

When I want to transfer a circuit from a solderless board, if space in the final device is not an issue, I use a pre-made PCB that exactly duplicates the solderless boards function. Then I literally transfer the parts and jumper wires over and solder them in place.

These are my favorite brand, the ones with PTH (plated through hole) in the name are the solder boards.

Dig-Key doesn’t have it in stock, but if you do a lot of this the super bundle pack is a good value.

If I’m going to do that, perhaps I should include a power supply so I can use just a single 12v input instead of both 12v and 3.3v. Is that a big project?

The circuit I drew only connects to a12V power supply, the 3.3V shown is meant to be provided by the ESP32 pin.

So I’m assuming you want a 3.3V supply to power your ESP32 micro. I’ve never used an ESP32 micro, so I don’t know how difficult it would be to build a power supply for it.