Do you need to control something high-powered from an Arduino pin? Do you need to control something from a sensor that just doesn't give much output? Want to toggle power to a hacked device, replacing or augmenting a physical button? The transistor has got your back. But before we get to hacking, let's clear up a few things:Good Stuff to Know
- BJT vs. (MOS)FET: These are different types of transistors. I'm working with bipolar junction transistors (BJT). FETs (field-effect transistors) usually look a bit beefier, with a built-in heatsink. Each has three pins, but BJTs use the terms "
collector, base, and emitter" for them, while FETs call them "
source, gate, and drain".
- Collector: Similar to the positive leg on an LED, this is where power flows in.
- Base: This is the "trigger" pin coming from your controller, sensor, or whatever.
- Emitter: Like the negative leg on your LED, this is the ground side of your transistor.
Even transistors of similar types may put these legs in a different order, so double-check your part! An easy way is to Google image-search "[part number] pinout".
- Part numbers: Make sure you grab the right transistors! You can Google the datasheet to be safe. Things to watch out for: it must be able to handle the power you're pushing through; double-check which way to hook it up; and it should be the right type (NPN or PNP). Use the first two rows engraved into the transistor. They can be hard to read; sorry.
- NPN vs. PNP: NPN transistors are normally "off" (disconnected), unless you're applying power to the base pin. PNP transistors are normally "on" (allowing current to flow), unless your signal is high.
- Transistor vs. Relay: If you're toggling something that's really power-hungry, consider using a relay instead. They're a bit slower, because they use a magnet to toggle a physical metal switch inside a little box. But that also makes them a bit safer, because of the physical isolation between the leads. And they make a cool little "click" noise. Rad!
We'll just run the Arduino Blink sketch, since that gives a great visualization of what's going on.The Setup
I'm using a DC motor for this example, since they tend to ride the edge between what an Arduino pin can toggle on its own, and needing a little help. As you can see in the video above, it just barely works. DC motors are common in wheeled robots and R/C vehicles. They have two terminals, and one gets connected to power, the other to ground. Based on which way 'round you put the wires, the motor will spin clockwise or counter-clockwise.
Try plugging one wire directly into pin 13, and one directly into ground, to start with. Not much going on there, eh? Little haptic motors (and their twins, cell-phone vibe motors) will run perfectly happily on the small power supply. But kick it up to small-robot standards, and you'll have to prod the motor to get it spinning even when the power's on.
Now, unplug your Arduino and reconfigure the circuit on a breadboard: plug one of the motor's wires into the 5V pin on your Arduino, if there is one; otherwise, skip ahead to the next bit with the external battery. Now, connect the transistor:
- BASE (middle) leg to digital pin 13
- COLLECTOR to the motor's other wire
- EMITTER to a ground pin
Plug in the 'Duino again, and voilà! Your little motor should be going like crazy, then off and on again, just like you'd expect. Make sure your motor is ready to handle that much voltage, and unplug it quickly if you smell anything unusual.External Power
OK, cool! We can power stuff directly from the digital pin, or use that to control 5V from the Arduino. The next step is to add an external source – for example, in case you want to control a light that takes 9V or 12V.
The main thing to note is that you're tying both grounds together: the one from the power source, and the one for the controlling signal, meet to form a "common ground". This allows both power and data signals to flow through the same circuit.
Your motor's chosen "
ground" wire is still tied to the collector; your digital control pin is still tied to the base; and GND is still tied to the emitter. But this time, the motor's designated "
power" wire is sourced from the positive terminal of your 9V source, and the battery's negative terminal is tied to GND as well.
This circuit can also be used to amplify the amount of power to control a heavy-duty servo, or with NeoPixel strips that want to run on their own power source, or anything similar!
Next week, we'll cover how to drive your motor forwards and backwards – it takes a bit of finagling with an H-bridge, but it's not bad!
And in our next transistor tutorial, we'll get to toggle buttons and switches (to automate devices for home automation). We'll also get around to amplifying the signal from a piezo disc – which is useful for making impact-reactive systems like light-up wearables!