13 – Transistors




    • Get to know the transistor
    • The first circuit with a transistor
    • First encounter with a Direct Current (DC) motor
    • How to vary the motor speed via PWM


Bill of materials

Arduino Uno Arduino Uno or equivalentWe can use any other Arduino board in this chapter.


Jumper wires

A solderless Breadboard plus some jumper wires.

330 Ohm Resistor

A LED diode and a 330 Ω resistor
2N222 Transistor A 2N2222 transistor. Check that this reference number is silverscreened on the surface, because the temperature sensor is quite similar.
DC fan  A DC Motor



In the previous chapters up to now, we have interacted with the outside world sending signals to the Arduino digital pins. For example, we have set a pin HIGH, trusting that it was enough to control what there was beyond, what until now have been mostly LED diodes.

But the power handling capability of Arduino to provide power to an external device is limited and frequently is not enough to excite an external actuator. That is why we need external devices that receive the signals sent by Arduino and adapt them to the needs of the outside world.

To do that we will have to use devices as transistors, relays, servos and different kinds of little DC motors.

  • The Arduino digital pins can draw a maximum of 40 mA or, what is the same, 5V x 40 mA = 0,2 Watt.
  • This is enough to light up LED diodes or even move a little servo but not to move a little DC motor or an stepper motor.
  • It is important to make sure that everything we connect to the Arduino pins doesn’t exceed the specifications, otherwise it will end up with a burnt plastic smell and a trip to the Arduino shop to buy another one.

During the previous chapters we have being introducing electronic components of different kinds: diodes, LED diodes, resistors, potentiometers … And now we will introduce another one, which is the foundation of all modern Electronics, including Computer Science, the transistor.

Don’t panic! We are aware of the reverential awe that the transistor inspires among Electronics and Arduino rookies, but keep calm, it is much easier than it looks and is very grateful component in almost any project you can imagine.

So please, keep reading. And I trust that you’ll end up getting to like transistors.


The transistor


According to Wikipedia (at least in spanish) the transistor is a semiconductor device used to deliver an output signal in response to an input signal, that can act as amplifier, oscillator, switch or rectifier.

Wow, it doesn’t surprise me the panic! Let’s translate it.

Continuing with the analogy that we saw in chapter 3, between electric current and water flow, the transistor is very similar to a water tap or faucet. Yes, one of these water taps that we turn on and off to take a bath or wash our hands.

A water tap has basically two functions:

  • Allow to turn on or off the water flow (digitally speaking, YES/NO).
  • Control the intensity of that water flow, opening more or less the stream.

Well, basically a transistor is exactly the same built differently, with different materials and a little faster to operate.

A transistor can work in two ways:

  • Turning on or off the current intensity, acting like a switch.
  • Amplifying an input signal (we will soon come back to this)

When a transistor operates in the first manner, acting like a switch, without half-measures, we say that it operates in the cut-off Region (allow the ) or in the saturation region (). And this is the foundation of all modern digital technology: computers, mobiles, game consoles, digital watches… In fact, when a transistor operates in the cut-off region its output is 0 and 1 in the saturation region (or FALSE/TRUE, if you prefer).

A water tap has three parts: input, control and output, and so have transistors, but we call them emitter (E), base (B) and collector (C).

If we turn a tap on and off continuously in both directions, the outflow of water is proportional to the angle of the handle at the moment. And if the pipe were huge enough we would be greatly amplifying our manual movement.

When we do this using a transistor, applying a variable electric signal to the base, the current intensity between the emitter and the collector varies according to the signal applied to the base, amplifying it. The ratio between the current intensity of the collector and the current intensity of the emitter is called gain, and we use the Greek letter β in Electronics to represent it.

Gain expresses how much output current intensity can be obtained in the collector in function of the current intensity of the emitter.

You can get huge gains with this system and is the foundation of all modern electronic amplifiers.

Every time you listen to music, there is a transistor (or more) amplifying a weak signal so you can hear it.

  • We all have a clear idea of what conductors and an insulators are. Semiconductors are a type of material which can become conductive or insulating at will, by applying them an electrical signal. And they have some interesting properties under the right conditions.
  • The most typical and most widespread semiconductors in Electronics manufacturing are silicon and gallium arsenide, but many materials, to a greater or lesser extent, presents a semiconductor effect.
  • Most of the components we know in Electronics, such as LEDs, transistors and diodes are semiconductors, and so are many other lesser known as thyristors and even solid state lasers.



Let’s start off using a general purpose transistor, that we can easily find anywhere, the P2N2222 transistor. All circuit schematic diagrams that include a transistor usually look like this:

Chapter 13 Schematic diagram 1
  • The transistor is labelled Q1 and usually is represented inscribed in a circle.
  • The arrow in the emitter shows the flow of current intensity. In this case this indicates that is a NPN transistor. If the arrow were the other way round, it would indicate that it would be a PNP transistor, but better we leave it for now.
  • M1 is everything we want to control (in this case a DC motor).
  • Pin 9 represents one of the Arduino pins that will control the external circuit.

A circuit like this allows the resistance between the emitter and the collector to be proportional to the control signal applied to the base.

In this example, a value of 5V applied to the base allows current intensity to flow without restrictions. And as voltage progressively decreases in the base (using PWM), the opposition to current intensity grows until current intensity is completely shut off, when voltage reaches 0V.

  • Transistor stands for Transfer Resistor.

We are going to power our load with 5V because we have no other. But we could use 12V, 24V power supplies or whatever we needed and go using more powerful DC motors without worrying about whether Arduino can power them or not. In fact, we can buy transistors capable of regulating 220V household alternating current (AC).

An advantage of using a transistor is that effectively isolates the control circuit of the base from the load between emitter and collector, making it nearly impossible to burn the Arduino using a circuit like this.

  • The number of commercial transistors is almost unlimited (and prices also) depending on their function and their ability to withstand different voltages, higher loads, dissipate more heat or do little electronic noise.
  • It is not a good idea to buy rare and expensive transistors designed for specific tasks. Unless you have a very good reason to buy an expensive transistor, it is best to stick to the cheaper models.
  • The P2N2222 has spent many years in the market for something. Start with it and we will further talk about others.




We will use a transistor to control the rotation speed of a small DC motor, but the same circuit would also allow us to control bigger DC motors, just by making sure that we use a transistor that withstand the load.

2n222 transistor pinout


To identify each lead, hold the transistor with the leads down, facing the flat face, where the name is labelled. From left to right they are the emitter, the base and the collector.

Chapter 13, Fritzing diagram


Chapter 13, Schematic 2


  • Important: Since the motor is an inductive load we should add a diode to protect the transistor.
  • It is not essential, but a transistor is usually more expensive than a diode (although not much) and more difficult to replace.




Let’s start with something simple, just turning on and off the dc motor without varying the speed.

Sketch 13.1
const int control = 9;

void setup()
     pinMode(control,  OUTPUT);

void loop()
     digitalWrite(control, HIGH);
     digitalWrite(control, LOW);

It is high time we started to pick up good habits, so instead of using a number to choose the Arduino control pin, the first line defines an integer constant called control that will be used in the digitalWrite() statement.

  • As the size of the programs grows, an error in a pin number can be very difficult to detect. But giving it a name instead, not only makes it easier to read but also easier to change on just one place without passing through the whole program looking for a specific number, if we wanted to choose any other pin.


To see how does the speed vary we could use the following sketch:

Sketch 13.2
const int control = 9;
void setup()
    pinMode(control,  OUTPUT);

void loop()
    for ( int n = 0 ; n < 255 ; n++)
        analogWrite (control,  n);

Where we listen as the DC motor speed increases until it stops and starts again. The reason is that the variation of voltage at the base of the transistor limits the current intensity through it, thereby changing the motor speed to which is connected. It would be quite easy to add a potentiometer to the circuit, so we can use its value to vary the DC motor speed. Cheer up, I leave it as exercise.




    • I am confident that the transistors will be a little less scary now. The image of the water tap or faucet is comfortable to imagine the operation of the transistor (although it has its limitations).
    • We have introduced the P2N2222 transistor, a typical transistor for applications where the current intensity doesn’t exceed half ampere and the emitter voltage is not too high, up to 40V.
    • We have used a typical circuit with a transistor to turn on an off a DC motor. But it will be also useful for other applications, such as driving 12V LED strips, for example.
    • Be careful: a typical LED strip usually consumes about 18 Watts using a 12V power supply, that is, 18W / 12V = 1.5 Amperes. More than enough to burn our P2N2222. This requires another model of transistor (we will discuss it further).
    • We use the Arduino PWM output pins to vary the rotation speed of the motor.