Transistor Switches in IoT Prototyping
Benefits & Usage Examples
Table of Contents
Introduction to transistors
One of the first non-trivial issues that a newcomer into IoT hardware design probably has to deal with, is the use of transistor switches. The purpose of this post is to highlight the multi-fold benefits of using transistor switches in an IoT prototype circuit and demonstrate how to use them through a simple but practical example application. We will focus on two popular micro-controller families, Pycom and Arduino MKR boards. These modules combine the benefits of well-known ESP-32 and Arduino modules and embedded radio modems for WiFi, LoRaWAN and NB-IoT connectivity, offering an all-in-one solution from sensing to cloud.
Before delving into the details of putting transistor switches in IoT prototypes, a non-electronics expert may wonder what is a transistor, what types are out there, and what exactly means using a transistor as a switch. There are some excellent introductory and not-so introductory tutorials out there for one to become a “master” of transistor, such as https://learn.sparkfun.com/tutorials/transistors/all and https://learn.adafruit.com/transistors-101, so here we just keep it to “high-level”.
Transistors are in essence, electronic elements with 3 terminals, the Base, Collector, Emitter for the first type, called BJTs, and Gate, Source, Drain for the second type, called FETs. They are used for either amplifying or switching signals. By controlling the voltage or the current of one pair of terminals we can control the current flow in another pair, so in practice “open” or “close” a circuit (in other words “switch”), and not only that, but also we can provide for larger current at the output (in other words “amplify”). How do BJT and FET differ? In many ways: BJTs are current-controlled while FETs are voltage-controlled. FETs have significantly higher impedance, hence they practically draw negligible current. The above features combined with their smallest manufacturing size makes FETs far more popular, though they are more expensive. In the remainder of the blog post we will assume the switching operation of the transistors, which will be explained through an easy example later on.
Usage scenarios
So, you just got your IoT modules and some sensors and you are eager to prepare a first working prototype. For many applications you might not need a transistor at all and you are ready to go. But, in practice there are many cases and applications where you need a driving circuit based on a transistor switch. In particular:
- You have placed a sensing circuit at one or more points of your board and you need to avoid energy consumption during idle or deep sleep periods. (always keep in mind that low power consumption is of utmost importance for IoT devices). One well-known example is the simple resistor divider which is added at points where the voltage level (e.g. for a battery) needs to be measured. The voltage divider is connected to a GPIO pin of the micro-controller. If no special care is taken, this will result in continuous current flow, thus high power consumption, although you probably need to activate the circuit a few times per hour or even once per day.
- You have a sensor that draws more current than a GPIO pin can sink. Typical GPIO pins can draw up to a few mA: for example Arduino MKR pins draw up to 7 mA, whereas Pycom boards up to 12 mA and recommended not to exceed 6 mA (https://media.digikey.com/pdf/Data%20Sheets/Arduino%20PDFs/ABX00012_Web.pdf, https://docs.pycom.io/gitbook/assets/specsheets/Pycom_002_Specsheets_WiPy3.0_v2.pdf). Then there is the regulated 3.3V Output pin (VCC for MKRs and 3V3 for Pycom) that can draw up to a few hundreds of mA, but this pin is constantly “on” even at deep sleep states, and you can’t switch it off. In this case, you can use a transistor switch to drive the current from the regulated Output pin using an additional low-sink current GPIO pin as the controller. With this setup you won’t waste energy during inactive states.
- You have a sensor that needs short excitation pulses (e.g. on the order of 10 msec) with high current sinks (e.g 10-20 mA) or else it may be destroyed. The popular high-accuracy EC5 soil sensor represents such an example (https://www.metergroup.com/environment/products/ec-5-soil-moisture-sensor/).
In a nutshell, transistor switches can be really handy when you need to save energy or when standard GPIO pins can not sustain the current draw requirements.
Example application
But, how should you build such a circuit? With a simple search on the Internet and on electronics textbooks you might find that there are basically two transistor types (BJTs and FETs) and two switching types (High-Side and Low-Side). In this post we will first go with a BJT transistor connected as low-side, and then show the high-side equivalent implementation. The figure below presents an example based on:
- Pycom’s WiPy 3.0 microcontroller (https://docs.pycom.io/gitbook/assets/specsheets/Pycom_002_Specsheets_WiPy3.0_v2.pdf)
- DHT22 / AM2302 sensor (https://cdn-learn.adafruit.com/downloads/pdf/dht.pdf)
- 2N2222 NPN switching transistor (https://en.wikipedia.org/wiki/2N2222)
2N2222 is a very common NPN transistor, coming in various packaging forms, including the cheap TO-92, which can be easily found in your local electronics/makers store or eBay. The transistor is driven by GPIO pin P8 through a 1k base resistor, and we have also added a 300 Ohm pull-down resistor. In essence, with low-side setups we “switch” the ground. In particular:
- When P8 is set to LOW, the transistor acts as an open-circuit (cut-off) and no current flows into the sensor. GND of DHT22 is practically floating, so the sensor’s circuit is practically “open”.
- When P10 is set to HIGH, the transistor opens, collector’s (C) and emitter’s (E) pins are “short-circuited”, hence current starts to flow, thus the GND pin of the DHT22 sensor is set to 0V and the sensor’s circuit is “closing”.
So, to take a measurement the following 3 steps are required:
- Set P8 pin to HIGH
- Use a software library to read temperature/humidity values from P10 pin (we use an open-source library for that: https://github.com/JurassicPork/DHT_PyCom/tree/pulses_get).
- Set P8 to LOW
Then you can safely put the WiPy to deep sleep, without worrying about any current draw from the DHT sensor, or practically any other sensor/circuit connected this way.
High-side implementation
For high-side implementation you can use the equally popular 2N2907 PNP transistor, with the Emitter connected directly to the 3V3 pin. The GND pin of the DHT22 sensor is connected to the common ground and the VCC pin to the Collector, so in this implementation the supply pin is actually switched. Differently from the low-side implementation, the transistor now opens with a digital LOW and acts as an open-circuit with a digital HIGH.
For Arduino MKR boards the story is the same. You may use any of the digital pins to control the transistor switch and read data from the sensor and the regulated VCC pin for constant power supply of 3.3V/600 mA max.
FETs as transistor switches with Pycom and Arduino MKR modules
For completeness, we present the corresponding circuit diagrams for the Pycom and Arduino MKR modules using two FET models that can be easily found on local electronics store (BS170 and B250P). The high-side and low-side configurations using FETs instead of BJTs are straightforward. Notice also the different pin labeling (D0, D1) used in Arduinos.