Using a Transistor to Control High Current Loads with an Arduino
SUMMER 2018 UPDATE: Upon revisiting this exercise, I was unable to complete it exercise successfully and questioned the accuracy of my circuit design and expected motor behavior from the fall. As an alternative, I followed this Adafruit example, which uses a PN2222 Transistor, and was not only able to "blink" the motor on and off (code) but also control its speed with a potentiometer (code). When adding the pot, connect the wiper to one of the analog pins. In addition, no other power source was used; just the current provided by the Arduino's USB connection to the computer.
Here's Adafruit's breadboard layout for future reference:
FALL 2017: This week I learned how to control a circuit that requires more current and amperage to operate than my microcontroller can supply. In this lab, I still used my microcontrolller, but only to open or close another circuit, in this case a DC toy motor connected to a 9V battery.* So I created two circuits, one operating on 5V and the other operating on 9V. I used the smaller current to control the larger current with a type of electronic switch called a transistor.
For this lab I used a TIP-120 transistor, which has three connections: a base, a collector, and an emitter. By supplying voltage to the base from an Arduino output pin, the current then flows from the collector to the emitter. It’s this channel from the collector to the emitter that is part of the second circuit with the motor and battery.
The TIP-120 is a type of bipolar transistor, known as a NPN transistor. In a PNP transistor, the channel between the collector and the emitter turns off when voltage is supplied to the base. Another type of transistor, field-effect transistors (FETs) work in a similar way except that they require even less current to open or close a channel for current to flow. The three FET connections are called gate (equivalent to bass), drain (equivalent to emitter), and source (equivalent to collector). Just as with the bipolar class of transistors, you'll find N-channel MOSFETs (which operate in similar fashion to the NPN transistors) and P-channel MOSFETs (equivalent to the PNP operation).
Relays are mechanical switches that are also used to control a higher current or voltage circuits. Unlike transistors, they are slower, and because they are mechanical, will not last for as many switching operations. However, transistors only allow you to control the current in one direction. This is not the case for relays and can be used for controlling AC or DC.
*The rated voltage for this Adafruit DC toy motor is 6V meaning that it it's speed will likely top out at 6V. A 9V battery is probably overkill for this test, but I'll more interested in setting up the circuits correctly. (And now I understand why my zoetrope from a few weeks back, which used this exact motor and battery, spun so fast!)
I connected the base of my TIP120 transistor to Digital Pin 9 (I supplied the voltage from my Arduino through here) and the emitter to ground. That was circuit one. For circuit two, one lead of my motor connected to the transistor's collector, the other to the 9V battery which connects to the same ground as circuit one. (Both circuits must have a common ground to work.) Following the lab, I added a diode pointing away from ground and in parallel with the collector and emitter to protect my transistor from any reverse voltage than might occur when my motor shut off (or if my motor reversed direction). I learned in class that the TIP120 has a diode built into its housing, as well, but it doesn't hurt to add an extra one to the overall circuit as I did here.
DC Motor Control Using an H-Bridge
In the above Lab, I merely turned the motor on and off, and it spun in one direction. Had I reversed the voltage (by switching the motor's leads to ground and power), then the motor would have spun in the opposite direction. Should I have a need for this in the future, an easier way to reverse motor direction would be to use a H-bridge circuit. This is really just an array of transistors that lets me control the polarity and amount of voltage to another circuit. I could build this from scratch or use an already manufactured integrated circuit, such as the L293NE H-bridge.
I followed the lab to connect all the components, except for the increased power supply; this motor will run fine on the Arduino's 5 volts. I connected three Arduino pins to the H-bridge: one to enable it and the other two to control its internal switches. These were all declared as outputs in the sketch. I also wired a button as an input.