How can I use a transistor (2n2222) to act as a switch with this 12V momentary switch?
A schematic, given what I read above, would be something like the following.







simulate this circuit &ndash; Schematic created using CircuitLab

I've added a resistor in series with the LED because LEDs usually do not directly operate off of \$12\:\text{V}\$ unless they have something built in (resistor, etc) to help with current limiting. It helps me avoid thinking about that problem if it is included. So perhaps I added it more for me, than you. But it's there.

The formula for the output voltage of the LM317 is:

$$V_\text{OUT}=V_\text{REF}\cdot\left(1+\frac{R_2}{R_1}\right)+I_\text{ADJ}\cdot R_2$$

where \$V_\text{REF}=1.25\:\text{V}\pm 50\:\text{mV}\$ and \$I_\text{ADJ}\approx 50\:\mu\text{A}\$. Given your resistor values in your wiring diagram, I compute something close to \$V_\text{OUT}\approx 3.13\:\text{V}\$. Which, I suppose, is possible for the (+) input of your garage door opener.



As you can see, \$SW_1\$ is connected to the "high side" of things. (Okay, so I drew it out that way. But it is also following convention to arrange things this way.) So, at first blush, when talking about using transistors then what you want is called a "high side switch." That's important for transistors because they come in two polarities and one is good as a low side switch and the other polarity is good as a high side switch. So it helps to know which kind of switching you are doing so you can quickly select the right polarity of transistor.

You've provided a lot of information. Perhaps almost too much because now we can see "several different ways" to approach a solution. But all of them are probably more complicated than the one you suggested. So I think it is best to stay with that.

The addition isn't that complicated, as you can see below. The main thing is that the "brick" (black box) needs access to ground (or the (-) side of things) as well as the unswitched \$12\:\text{V}\$ supply rail. It also accepts a ground-referenced signal from the Arduino Nano and activates its SW+ output that bypasses the manual switch when the Arduino Nano orders it to do so.







simulate this circuit

(Note: The Arduino ground must be in common with your garage door opening system's negative terminal, so make sure that you share the Arduino power supply's ground [or negative side] with the negative side of the garage door system. We don't usually show that connection in a schematic because it is "assumed" that you know it should be there. But if you only wire the I/O pin and do NOT connect up the Arduino ground side also then there will be problems in making this work.)



Let's focus on the transistor switch (that black box above.)

The Arduino Nano is operated off of its own \$5\:\text{V}\$ power supply (after regulation, anyway.) This is distinct from the the supplies used in the garage door opener system. The I/O pin will drive out either \$\approx 5\:\text{V}\$ when "HI" or else \$\approx 0\:\text{V}\$ when "LO" (these values are relative to the (-) side of the Arduino Nano power supply.)

The problem right now is that none of us know how much current is required by the relay, the LM317 and the garage door opener module. (We can guess about the LED, though. There are typical values that are reasonably assumed for it.) But I'm convinced that this is a low-current circuit (for now), so I'll treat it as such and use a simple arrangement.

The first idea here is that you need to switch \$12\:\text{V}\$ on the high side. This would suggest a PNP transistor. But to turn a PNP off you have to make the base voltage very close to the emitter voltage (near a zero volt difference) and this means the base would need to be close to \$12\:\text{V}\$. Sadly, your Arduino Nano cannot achieve that. Not even close. So this means a second BJT is needed and this time, an NPN.

There are two basic approaches at this point. In both, the PNP is operated as a switch device, which means with its collector pulled close to its emitter and that means with a large base current relative to the collector current. However, there are two different ways to go with the NPN -- either as yet another switch device with similar relative ratios for the base vs collector current, or else as an emitter-follower that works as a current sink, instead. The advantage of this latter case is that the required I/O pin current is quite a bit less and that's a good thing. However, it would not work out if you were switching \$5\:\text{V}\$, for example. It's lucky that in this circumstance you are switching a higher voltage. So that's the path I'm going to take, below.

(Note that while I'm taking this "better path" [according to me], that doesn't mean you will often see it done this way. Most people just fall into the well-worn path of operating both BJTs as switches. It's kind of a rut, of sorts, and folks sometimes seem to have a hard time keeping out of such ruts. But I like to push a little off the beaten path, time to time. So that's why I'm going this way.)

Let's look at the new schematic:







simulate this circuit

I've assumed a collector current for \$Q_1\$ of about \$200\:\text{mA}\$ and a lowish \$\beta_\text{SAT}\approx 10\$ to make sure it has enough base current (of about \$20\:\text{mA}\$) which determines the collector current for \$Q_2\$. Assuming an I/O pin output of about \$4.8\:\text{V}\$ and a \$V_\text{BE}\$ drop of about \$850\:\text{mV}\$ for \$Q_2\$, I worked out \$R_4=\frac{4.8\:\text{V}-850\:\text{mV}}{20\:\text{mA}}\approx 200\:\Omega\$. A nearby value of \$220\:\Omega\$ was then selected.

The I/O pin current will be about 100 times smaller, so less than \$200\:\mu\text{A}\$, which is easily handled by any I/O pin I've used in decades. So should be just fine.

Dissipation in either BJT will be no more than about \$150\:\text{mW}\$. So small signal BJTs for both will probably survive fine. (Of course, verify things don't get too hot once you build it. I'm making assumptions here.)

I've provided two alternatives, by the way. The one labeled "MINIMAL" is for the minimalist types who just want to use the least number of parts and are willing to take a few risks if they are small ones. In this case, there's a missing DC path resistor that is used to help tie the PNP base upward to its emitter voltage (\$12\:\text{V}\$.) Without that resistor, it's possible though probably not likely, that the PNP will fail to completely turn off. I haven't tested thousands of incarnations to find out what the odds are, but without the resistor it will probably work okay. But it's not as safe to use as the one labeled "BETTER." If you aren't stuck on using the absolute minimum of parts, I'd add \$R_5\$. Cheap, doesn't take up a lot of room, and helps manage a situation that is otherwise left to chance.

Just make sure you "share" the power supply grounds between your Arduino and your garage door opener circuit (tie them together.) (Of course, do not tie the positive rails to each other!) They just need a common reference point and ground (or minus) serves that role.

That's about it, really. I do apologize for using schematics instead of wiring diagrams, which may be easier for you to follow. If you seriously need a wiring diagram (you cannot make such a diagram out of a schematic concept), then I'll try and fabricate one that makes the wiring parts of it clearer.

P.S. Wiring diagrams are more for builders who don't want to understand how it is that what they are building actually works, but instead just want to go build it and pray that it functions right and that diagram is accurate. Schematics are more for those who want to follow and understand what's going on and aren't nearly as interested in the exact wiring details as they are in the functional blocks and how they work together. Two different purposes.



Other approaches would be to have your Arduino Nano directly perform the relay function and control conductance between \$SW_\text{A}\$ and \$SW_\text{B}\$ of your module. If you do that directly with transistors somehow, you could eliminate the relay. (Usually, that's a gain and not a loss.) However, this still leaves the power switch, \$SW_1\$, as an issue (do you leave it on all the time, or what exactly?) And what about the LED?

You could separate the LED out (get rid of it, since it depends on \$12\:\text{V}\$) and replace that part, using another I/O pin of your Arduino Nano to turn on and off an LED operating from its power supply, for example. Then get rid of the relay and work out a circuit to directly connect \$SW_\text{A}\$ and \$SW_\text{B}\$ only after first powering up a different voltage regulator that accepts an ENABLE pin (yet another I/O pin from your Ardiuno Nano.) So, with three I/O pins from your Arduino Nano (one for an LED light, one to enable a voltage regulator, and one to connect \$SW_\text{A}\$ to \$SW_\text{B}\$, you could get rid of the relay entirely and use transistors for everything.

In such a case, you'd make sure the LED is off and that the connection between \$SW_\text{A}\$ and \$SW_\text{B}\$ is deactivated, then activate the power, wait a decent period of time to let it stabilize, activate the LED to indicate power, then activate the connection between \$SW_\text{A}\$ and \$SW_\text{B}\$ for whatever time you think is needed. Then you'd reverse this process, to return to a quiescent state.

But that's why I said it would be simpler to just go with your own suggested approach. It assumes everything works already, just fine, and adds a simple parallel transistor switch to replace the manual pressing of a button. All you have to do is make sure that you hold the circuit active for the right period of time.