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

A short solderless breadboard with two rows of holes along each side. There are no components mounted on the board. The board is oriented sideways so that the long rows of holes are on the top and bottom of the image.
A short solderless breadboard.
Three short pieces of hookup wire: one is clad in red insulation, one in blue, and one in black. All three have exposed ends approximately 5mm long.
Three short pieces of hookup wire
An Arduino Uno. The USB connector is facing to the left, so that the digital pins are on the top of the image, and the analog pins are on the bottom.
An Arduino Uno.
Potentiometer. The one shown here has three legs spaced 0.1 inches apart and can be therefore mounted on a solderless breadboard.
Potentiometer
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.
Diodes. Shown here are 1N400x power diodes.
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.
DC Power Supply
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.
TIP120 transistor
DC geared motor, hobby size. The motor shaft has a round blue hub attached.
DC Motor. Pictured motor also has a round plate attached to the shaft
Small Incandescent lamp bulb and socket
Small Incandescent lamp bulb and socket

Connect the Breadboard

Connect the breadboard to the Arduino, running 5V and ground to the side rails

An Arduino Uno on the left connected to a solderless breadboard, right. The Uno's 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno's ground hole is connected to the blue column on the left 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.
An Arduino Uno on the left connected to a solderless breadboard, right.

Add a potentiometer

Connect a potentiometer to analog in pin 0 of the module:

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.
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.
Breadboard view of a potentiometer connected to analog in 0 of an Arduino

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. 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. 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.

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.
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.
The schematic symbol of an NPN transistor. B is the base, C is the collector, and E is the emitter.

 

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).
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).
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).

Connect the base to an output pin of the microcontroller, and the emitter to ground like so:

 

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. The collector is connected to ground.
Schematic view of a potentiometer connected to analog in 0 of the Arduino. A transistor is connected to Digital Pin 9
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. The collector is connected to ground.
Breadboard view of a potentiometer connected to analog in 0 of an Arduino. A transistor is connected to Digital Pin 9

Safety Warning: You can generally connect the base to a microcontroller’s pin directly without a current limiting resistor because the current from the pin is low enough. But it’s necessary if you’re controlling a transistor circuit without a microcontroller.

Connect a motor and power supply

Attach a DC motor to the collector of the transistor. Most motors will require more amperage 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. a 12V battery may need a 12V wall wart, or a 12V battery. The ground of the motor power supply should connect to the ground of the microcontroller, on the breadboard.

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 red wire to the first wire of the DC motor. The other wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor.
Schematic view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack
Breadboard 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 red wire to the first wire of the DC motor. The other wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor.
Breadboard view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack.

 

Next, add a diode in parallel with the collector and emitter of the transistor, pointing away from ground. The diode to protects the transistor from back voltage generated when the motor shuts off, or if the motor is turned in the reverse direction.

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 red wire to the first wire of the DC motor. The other wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A Power Diode with the Cathode connected to the collector of the transistor and anode connected to ground
Schematic view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack, and protection diode
Breadboard 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 red wire to the first wire of the DC motor. The other wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A Power Diode with the Cathode connected to the collector of the transistor and anode connected to ground
Breadboard view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack, and protection Diode

The circuit with protection diode across the transistor,The TIP120 can be replaced with a MOSFET if you prefer.

You may also find that adding a diode across the motor helps with back voltage protection as well, particularly when you’re running multiple transistor-motor circuits. If you plan to add a diode across the motor, here’s the circuit:

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 red wire to the first wire of the DC motor. The other wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A Power Diode with the Cathode connected to the collector of the transistor and anode connected to ground. A second power diode connects to the leads of the DC motor with the Cathode connected to the lead also connects to + voltage of the DC jack. The anode connects simultaneously to the collector of the transistor and the other lead of the DC motor.
Schematic view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack, a protection diode, and a second diode across the motor
Breadboard 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 red wire to the first wire of the DC motor. The other wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A Power Diode with the Cathode connected to the collector of the transistor and anode connected to ground. A second power diode connects to the leads of the DC motor with the Cathode connected to the lead also connects to + voltage of the DC jack. The anode connects simultaneously to the collector of the transistor and the other lead of the DC motor.
Breadboard view of an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack, a protection diode, and a second diode across the motor

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:

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
Schematic representation and physical representation of a diode.

This circuit assumes you’re using a 12V motor. If your motor requires a different voltage, make sure to use a power supply that’s appropriate. Connect the ground of the motor’s supply to the ground of your microcontroller circuit, though, or the circuit won’t work properly.

Connect a lamp instead

You could also attach a lamp using a transistor. Like the motor, the lamp circuit below assumes a 12V lamp. 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 connected to analog in 0 of the Arduino. A transistor is connected to Digital Pin 9. An incandescent lamp connects to the transistor and a DC jack. The DC jack connects its red wire to the first wire of the incandescent lamp. The other wire of the DC jack connects to ground. The second wire of the incandescent lamp connects to the collector of the transistor. A Power Diode with the Cathode connected to the collector of the transistor and anode connected to ground
Schematic view of an Arduino connected to a potentiometer, a transistor, an incandescent lamp, and a DC jack, and protection Diode

 

Breadboard view of a potentiometer connected to analog in 0 of the Arduino. A transistor is connected to Digital Pin 9. An incandescent lamp connects to the transistor and a DC jack. The DC jack connects its red wire to the first wire of the incandescent lamp. The other wire of the DC jack connects to ground. The second wire of the incandescent lamp connects to the collector of the transistor. A Power Diode with the Cathode connected to the collector of the transistor and anode connected to ground
Breadboard view of an Arduino connected to a potentiometer, a transistor, an incandescent lamp, and a DC jack, and protection Diode

 

Program the microcontroller

Write a quick program to test the circuit. 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 24, 2018 by Tom Igoe