Lab: Using a Transistor to Control High Current Loads with an Arduino

Introduction

In this tutorial, you’ll learn how to control a high-current DC load such as a DC motor or an incandescent light from a microcontroller. Microcontrollers can only output a very small amount of current from their output pins. These pins are meant to send control signals, not to act as power supplies. The most common way to control another direct current device from a microcontroller is to use a transistor. Transistors allow you to control the flow of a high-current circuit from a low-current source.

What You’ll Need to Know

To get the most out of this Lab you should be familiar with the following concepts beforehand. If you’re not, review the links below:

  • Safety Warning: This tutorial shows you how to control high-current loads. This comes with a higher danger of injury from electricity than the earlier tutorials. Please be careful and double-check your wiring before plugging anything in, and never change your wiring while your circuit is powered.

Things You’ll Need

Figures 1-10 below are the parts you’ll need for this exercise. Click on any image for a larger view.

Photo of an Arduino Nano 33 IoT module. The USB connector is at the top of the image, and the physical pins are numbered in a U-shape from top left to bottom left, then from bottom right to top right.
Figure 1. Microcontroller. Shown here is an Arduino Nano 33 IoT
Photo of flexible jumper wires
Figure 2. Jumper wires.  You can also use pre-cut solid-core jumper wires.
Photo of a solderless breadboard
Figure 3. A solderless breadboard
Photo of a DC Gearmotor
Figure 4. DC Gearmotor. Any DC motor in the 3-15V DC range will work in with this circuit, though 4-6V is an ideal range.
Photo of two potentiometers
Figure 5. Potentiometer
A DC power jack. It pairs with a plug with a 2.1mm inside diameter, 5.5mm outside diameter plug, and has screw terminals on the back so that you can attach wires to it.
Figure 6. A DC Power Jack. This will provide the motor power input.
DC Power Supply. Shown here is a +12 Volt 1 Amp Center Positive DC power supply with a 2.1mm male jack. This size fits the Arduino Uno's female jack.
Figure 7. DC Power Supply to match your motor. If your motor is a 4-6V motor, you should use a 4-6V DC power supply.
Diodes. Shown here are 1N400x power diodes. The body of the component is black, and the end is silver. The silver end indicates the cathode end of the diode.
Figure 8. Diodes. Shown here are 1N400x power diodes.
TIP120 transistor. The transistor here has the same physical package as the voltage regulators shown above. It has three legs and a tab at the top with a hole in it. The tab is the back of the component. If you hold the component with the tab at the top and the bulging side of the component facing you, the legs will be arranged, from left to right, base, collector, emitter. The only way to know the difference between two components of the same package is to read the label on the package, unfortunately. This one is labeled TIP120.
Figure 9. TIP120 transistor or FQP30N06L N-channel MOSFET
Small Incandescent lamp bulb and socket
Figure 10. Small Incandescent lamp bulb and socket

Prepare the breadboard

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V or 3.3V (depending on your model) and any of the ground connections, as shown in Figures 11 and 12.

An Arduino Uno on the right connected to a solderless breadboard, left. The Uno's 5V output hole is connected to the red column of holes on the far right side of the breadboard. The Uno's ground hole is connected to the blue column on the right of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.

Figure 11. An Arduino Uno on the right connected to a solderless breadboard, left. The Uno’s 5V output hole is connected to the red column of holes on the far right side of the breadboard. The Uno’s ground hole is connected to the blue column on the right of the board. The red and blue columns on the right of the breadboard are connected to the red and blue columns on the left side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.

Arduino Nano on a breadboard.

Figure 12. An Arduino Nano mounted on a solderless breadboard. The Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.

The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano’s GND pin (physical pin 14) is connected to the left side black column. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.

Images made with Fritzing

Add a potentiometer

Connect a potentiometer to analog in pin 0 of the module as shown in Figure 12 through Figure 14:

Schematic view of a potentiometer. First leg of the potentiometer is connected to +5 volts. The second leg connected to analog in 0 of the Arduino. The third leg is connected to ground.
Figure 12. Schematic view of a potentiometer connected to analog in 0 of the Arduino
Breadboard view of a potentiometer. First leg of the potentiometer is connected to +5 volts. The second leg connected to analog in 0 of the Arduino. The third leg is connected to ground.
Figure 13. Breadboard view of a potentiometer connected to analog in 0 of an Arduino Uno. The potentiometer is mounted in three rows of the left center section of the breadboard. The two outside pins of the potentiometer are connected to the voltage and ground bus rows, respectively. The center pin is connected to analog in 0 of the Uno.
Breadboard view of a potentiometer connected to analog in 0 of an Arduino Nano.
Figure 14. Breadboard view of a potentiometer connected to analog in 0 of an Arduino Nano. The potentiometer is mounted in three rows of the left center section of the breadboard below the Nano. The two outside pins of the potentiometer are connected to the voltage and ground bus rows, respectively. The center pin is connected to analog in 0 (physical pin 4) of the Nano.

Connect a Transistor to the Microcontroller

The transistor allows you to control a circuit that’s carrying higher current and voltage from the microcontroller. It acts as an electronic switch. The one you’re using for this lab is an NPN-type transistor called a TIP120. See Figure 15 and Figure 16 for the pinout drawing and schematic symbol of the transistor. The datasheet for it can be found here. It’s designed for switching high-current loads. It has three connections, the base, the collector, and the emitter. The base is connected to the microcontroller’s output through a resistor. The high-current load (i.e. the motor or light) is attached to its power source, and then to the collector of the transistor. The emitter of the transistor is connected to ground.

Here’s the main operating principle of using a transistor as a switch: When a small voltage and current is applied between the base and the emitter (at ground), the transistor allows a larger current to flow between the collector and emitter.

The TIP120’s base turns on at about 2.0V, so it works well with either a 5V microcontroller like the Uno, or a 3.3V microcontroller like the Nano 33 IoT or the MKR series.

Pinout drawing of a TIP-120 transistor. It is facing forward with the heat sink tab at the top and the bulging side of the component facing you. From left to right the legs are labelled 1. base, 2. collector, 3. emitter.
Figure 15. Pinout drawing of a TIP-120 transistor. From left to right the legs are labelled 1. base, 2. collector, 3. emitter.
The schematic symbol of an NPN transistor where B is the base, C is the collector, and E is the emitter.
Figure 16. The schematic symbol of an NPN transistor. B is the base, C is the collector, and E is the emitter.

Figures 17 through 19 show how to connect the transistor.

Schematic view of a potentiometer and transistor connected to an Arduino.
Figure 17. Schematic view of a potentiometer and transistor connected to an Arduino. First leg of the potentiometer is connected to +5 volts. The second leg connected to analog in 0 of the Arduino. The third leg is connected to ground. The base of the transistor is connected to digital pin 9 of the Arduino through a 1-kilohm resistor. The collector is connected to ground.
Breadboard view of a potentiometer and transistor connected to an Arduino.
Figure 18. Breadboard view of a potentiometer and transistor connected to an Arduino. First leg of the potentiometer is connected to +5 volts. The second leg connected to analog in 0 of the Arduino. The third leg is connected to ground. The base of the transistor is connected to digital pin 9 of the Arduino through a 1-kilohm resistor. The collector is connected to ground.
Breadboard view of a potentiometer and transistor connected to an Arduino Nano.
Figure 19. Breadboard view of a potentiometer and transistor connected to an Arduino Nano. First leg of the potentiometer is connected to +3.3 volts. The second leg connected to analog in 0 of the Nano. The third leg is connected to ground. The base of the transistor is connected to digital pin 9 of the Nano. The collector is connected to ground.

Connect a Motor and Power Supply

Attach a DC motor to the collector of the transistor as shown in Figures 20 through 22. Most motors will require more current than the microcontroller can supply, so you will need to add a separate power supply as well. If your motor runs on around 9V, you could use a 9V battery. A 5V motor might run on 4 AA batteries (6V). A 12V battery may need a 12V DC wall adapter, or a 12V battery. The ground of the motor power supply should connect to the ground of the microcontroller, on the breadboard.

Add a 1N400x power diode in parallel with the collector and emitter of the transistor, pointing away from ground. The diode protects the transistor from back voltage generated when the motor shuts off, or if the motor is turned in the reverse direction. Used this way, the diode is called a snubber diode.

Schematic view of a potentiometer connected to analog in 0 of the Arduino.
Figure 20. Schematic view of a potentiometer connected to analog in 0 of the Arduino. A transistor is connected to Digital Pin 9. A DC motor connects to the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the DC motor. The negative wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A 1N400x diode’s cathode is connected to the collector, and its anode is connected to ground.
Breadboard view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack.
Figure 21. Breadboard view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack. A transistor is connected to Digital Pin 9. A DC motor connects to the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the DC motor. The negative wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A 1N400x diode’s cathode is connected to the collector, and its anode is connected to ground.
Breadboard view of an Arduino Nano connected to a potentiometer, a transistor, a DC motor, and a DC jack.
Figure 22. Breadboard view of an Arduino Nano connected to a potentiometer, a transistor, a DC motor, and a DC jack. A transistor is connected to Digital Pin 9 through a 1-kilohm resistor. A DC motor connects to the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the DC motor. The negative wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A 1N400x diode’s cathode is connected to the collector, and its anode is connected to ground.

Be sure to add the diode to your circuit correctly. The silver band on the diode denotes the cathode which is the tip of the arrow in the schematic, like so in Figure 23:

Schematic representation and physical representation of a diode. The schematic form shows an equilateral triangle with a line bisecting the triangle equally from one point to and through the middle of the opposing flat side. There is also a line perpendicular to the other line that also intersects the triangle at its bisected point. The cathode is represented by the side of the schematic with the line. The drawing of the physical form of the diode looks like a black resistor with only a single grey stripe on one side. The side with the stripe represents the cathode
Figure 23. Schematic representation and physical representation of a diode.
NPN Transistor and N-Channel MOSFET side by side. The physical packages of the transistor and MOSFET are nearly identical. The pin out of the N-channel MOSFET is comparable to the transistor, where G of the MOSFET is the gate (equivalent of base of the transistor), D is the drain (equivalent of the collector) and S is the source (equivalent of the emitter).
Figure 24. NPN Transistor and N-Channel MOSFET side by side with a schematic diagram of the MOSFET. G is the gate (equivalent of base), D is the drain (collector) and S is the source (emitter).
Another version of the schematic symbol of an N-channel MOSFET, where G is the gate (equivalent of base), D is the drain (collector) and S is the source (emitter).
Figure 25. Schematic symbol of an N-channel MOSFET, where G is the gate (equivalent of base), D is the drain (collector) and S is the source (emitter).

The circuit to connect a MOSFET to a microcontroller is very similar to the circuit for a bipolar transistor. For a MOSFET you don’t need a resistor connecting the output pin of the microcontroller and the gate like you do with a bipolar transistor. In fact, you may even need a pulldown resistor to turn the MOSFET off when you take the output pin low.

Connect a Lamp Instead of a Motor

You could also attach a lamp using a transistor. There are many 12V incandescent lamps, designed for use in track lighting, gallery lighting, and so forth. Nowadays, there are many 12V DC LED equivalents of the 12V AC lamps as well.  Here are a few examples:

The lamp circuit in Figures 26 through 28 assumes a 12V lamp. MOSFETs are generally best for switching incandescent and LED lamps, so the circuit below uses a MOSFET. If you’re using a 5V board like the Uno, you can use theIRF520 MOSFET. For the 3.3V boards, the FQP30N06L MOSFET will do well. Change your power supply accordingly if you’re using a different lamp. In the lamp circuit, the protection diode is not needed, since there’s no way for the polarity to get reversed in this circuit.

 

Schematic view of a potentiometer, MOSFET, and lamp connected to an Arduino.
Figure 26. Schematic view of a potentiometer, MOSFET, and lamp connected to an Arduino. The gate of a MOSFET transistor is connected to Digital Pin 9 of the Arduino. A 12V lamp connects to the drain of the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the lamp. The negative wire of the DC jack connects to ground. The second wire of the lamp connects to the drain of the transistor. The source of the transistor connects to ground.

 

Breadboard view of a potentiometer, MOSFET, and lamp connected to an Arduino.
Figure 27. Breadboard view of a potentiometer, MOSFET, and lamp connected to an Arduino. The gate of a MOSFET transistor is connected to Digital Pin 9 of the Arduino. A 12V lamp connects to the drain of the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the lamp. The negative wire of the DC jack connects to ground. The second wire of the lamp connects to the drain of the transistor. The source of the transistor connects to ground.

Breadboard view of a potentiometer, MOSFET, and lamp connected to an Nano.
Figure 28. Breadboard view of a potentiometer, MOSFET, and lamp connected to an Nano. The gate of a MOSFET transistor is connected to Digital Pin 9 of the Nano. A 12V lamp connects to the drain of the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the lamp. The negative wire of the DC jack connects to ground. The second wire of the lamp connects to the drain of the transistor. The source of the transistor connects to ground.

Program the microcontroller

Write a quick program to test the circuit, whether it’s a motor or a lamp. Your program should make the transistor pin an output in the setup method. Then in the loop, it should turn the motor on and off every second, just like the blink sketch does.

const int transistorPin = 9;    // connected to the base of the transistor

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
 }

 void loop() {
   digitalWrite(transistorPin, HIGH);
   delay(1000);
   digitalWrite(transistorPin, LOW);
   delay(1000);
 }

Now that you see it working, try changing the speed of the motor or the intensity of the lamp using the potentiometer.

To do that, read the voltage of the potentiometer using analogRead(). Then map the result to a range from 0 to 255 and save it in a new variable. Use that variable to set the speed of the motor or the brightness of the lamp using analogWrite().

const int transistorPin = 9;    // connected to the base of the transistor

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
 }

 void loop() {
   // read the potentiometer:
   int sensorValue = analogRead(A0);
   // map the sensor value to a range from 0 - 255:
   int outputValue = map(sensorValue, 0, 1023, 0, 255);
   // use that to control the transistor:
   analogWrite(transistorPin, outputValue);
 }

For the motor users: A motor controlled like this can only be turned in one direction. To be able to reverse the direction of the motor, an H-bridge circuit is required. For more on controlling DC motors with H-bridges, see the DC Motor Control lab

Originally written on July 1, 2014 by Matt Richardson
Last modified on August 23, 2019 by Yeseul Song