Switching: from Relays to Bipolar Junction Transistors

How many remote controls do you have in your home? Don’t you wish all these things were better integrated somehow, or that you could add remote control functionality to a random device? It’s a common starting point for a project, and a good learning experience for beginners.

A common solution we’ve seen applied is to connect a relay in parallel to all the buttons we want to press. When the relay is triggered, for example by your choice of microcontroller, it gets treated as a button press. While it does work, relays are not really the ideal solution for the very low current loads that we’re dealing with in these situations.

As it turns out, there are a few simple ways to solve this problem. In this article, we’re going to focus on using common bipolar junction transistors instead of relays to replace physical switches. In short, how to add transistors to existing electronics to control them in new ways.

Relays

Relays are still a good solution in some situations. When you’re dealing with high power, or when you need something exactly electrically identical to a closed switch, relays are a reasonable choice. Ever wonder why your USB digital storage oscilloscope makes clicking noises? Those are relays switching between the different modes of the scope – this preserves signal integrity.

On the other hand, relays are typically large, expensive, slow, require significant power, add a large coil of wire to the circuit (this can introduce a lot of noise), and have mechanical parts that are prone to failure. Solid state relays do exist and address some of these issues, but are not particularly cheap.

When a current is supplied to the coil of wire, the relay closes, allowing current to pass. In reality a transistor is generally added to the circuit to provide enough power to control the relay, while a diode protects the transistor from the reverse voltage spike caused by cutting power to the relay (the energy stored in the magnetic field of the coil has to go somewhere!). If the relay is attached parallel to a switch in some external device (such as a remote control), closing the relay is like closing the switch.

 

Bipolar Junction Transistors (BJT)

BJTs are a very common type of transistor. Their function is to amplify current. If a quick overview of how they work would be useful before continuing, this short guide may help.

A small current between the base and emitter controls a much larger current between the collector and emitter. If the collector and emitter are placed in parallel to an open switch, then the state of that switch can be easily controlled by a microcontroller output pin.

Transistors want current control, and your microcontroller GPIOs attempt to put out a given voltage. You need a voltage-to-current transformer, otherwise known as a resistor. (See Ohm’s law.) The exact value will vary, but 10 kΩ is a good starting point for replacing pushbuttons. A simple test circuit (using a switch to provide the current needed instead of a microcontroller) may look something like this:

 

If you were to take a multimeter and measure the resistance across the transistor from the collector to the emitter, it would drop from open-circuit to near zero when a current is provided between the base and emitter. In reality, there’s a voltage drop between the collector and emitter (about 0.3 volts, but it depends on the transistor) when the BJT is ‘switched on’ but for our purposes it is sufficient to think of it as going from infinite resistance to zero.

A reasonable choice of BJT is the venerable 2N2222. It’s available worldwide, is very inexpensive, reasonably fast, and can dissipate up to 500 milliwatts of power which is more than sufficient in this case. Most NPN-type BJTs will work fine though.

Specific Use Case

Consider the following alarmingly pink battery-powered external speaker/radio. It once had an infrared remote, now lost. It’s not a high quality device, but could be marginally useful again if there was a convenient way of turning it on:

Our first step is to disassemble it, and determine which pins of the ‘on’ switch need to be connected to activate the device:

When the device is powered and the switch set to ‘on’, we see 5 V relative to ground on two pins. When the device is powered off, only one of those pins is at 5 V. We start by soldering wires to those pins, applying power to the device, and crossing the wires. The device turned on, and by default switched to Aux mode. How convenient!

Now that we’ve isolated which pins of the switch need to be connected, we can breadboard a quick circuit with out BJT based switch controller and test it out.

 

 

When the right lead of the resistor is connected to power, the resistance across the collector/emitter drops to zero when measured by a multimeter. If it drops, but not quite to zero, you may have your transistor in backwards.

Since this worked, it’s time to solder it to perfboard and connect it to a microcontroller, in this case an ESP8266 mini-D1 board. Instead of connecting the transistor base to power, we will connect it to one of the mini-D1 output pins (we chose D0).

 

In this case, the firmware causes the ESP8266 to raise pin D0 high when it receives a certain UDP network packet from an Android smartphone application. If we wanted to control all features of the speakers, we would have to repeat this process for each switch. It’s admittedly not as elegant a solution as reverse engineering the IR protocol, but it’s versatile, fast enough to be an afternoon project without needing a ton of experience, and it works quite well.

Congratulations – you now have the most basic IoT device: an Internet connected switch. Before you go wild, you may want to consider adding some security depending what you’re connecting.

Also note this approach has some shortcomings. Not all switches function by simple electrical contact: some relatively common switches rely on a change in capacitance. Also in high noise environments, it’s possible for a BJT to unexpectedly trigger, although this can be minimized by placing the base resistor as close as reasonably possible to the transistor base, and if necessary by adding a small grounded metal enclosure (a Faraday cage) around the BJT and nearby traces. Finally, when a BJT fails due to overheating, they often create a short. In other words, be mindful of the consequences of your control system failing.


Filed under: Featured, hardware, how-to

from Hackaday http://ift.tt/2xd8qgp
via IFTTT