Arduino 12: Transistors

Several people wrote in about my earlier post concerning relays, noting that it’s a bit risky to control a relay from the Arduino as you can easily fry the IC chip. A  safer method is to use a transistor, which basically accomplishes the same thing (and more cheaply):it lets a small current control a much larger one. This lets you use the Arduino to turn large electric devices on and off when they are running off a large separate power supply.

There are two type of transistor: NPN (negative-positive-negative) and PNP.  The difference is whether you want to turn the transistor on with a signal that is a high voltage (5 V) or low (ground). NPN transistors are turned on with a high voltage, and that’s what we will use.

Transistors have three pins, with a plastic top with one side flattened. If you are looking at the flat side, the pins from left to right are called the collector, base and emitter. The middle pin, the base, is where you connect the control signal from the Arduino. The collector pin on the left is wired to the device, and the device must be connected to its own positive power supply terminal. The emitter pin on the right is connected to ground for the Arduino, AND to the ground of the outside power supply. The Arduino and the device being controlled must have a common ground.

To have the Arduino run a motor, first put the transistor in a breadboard. Connect the ground on the Arduino to the ground channel on the breadboard, and the ground channel to the emitter pin on the transistor. Wire pin 9 on the Arduino to the base pin of he transistor. Wire the collector to one of the motor terminals, then the other motor terminal to the positive terminal of a battery pack. Then wire the negative terminal of the battery pack back to the ground channel on the breadboard.

This code has the Arduino pulse the motor on and off at 1 second intervals.

int transistorPin = 9;

 

void setup(){

pinMode(transistorPin, OUTPUT);

}

 

void loop(){

digitalWrite(transistorPin, HIGH);

delay(1000);

digitalWrite(transistorPin, LOW);

delay(1000);

}

 

About these ads

10 responses to “Arduino 12: Transistors

  1. Oddly enough, using this exact configuration with a motor I know works well, I am not getting any results, I have double- and triple-checked everything and the motor is not spinning.

    Any help?

  2. Just wanted to let you know that it was extremely helpful. I only wanted to add that it works with PNP as well, or at least i think i have a PNP (3904).

  3. Pingback: 2N2222 transistor from Radio Shack links Arduino UNO and Teensy 2.0 | Sub-Etha Software

  4. Pingback: 2N2222 transistor from Radio Shack links Arduino UNO and Teensy 2.0 | Appleause

  5. Thanks that pretty much just what i was looking for, strange i couldn’t get my head around PNP and NPN transistors until now with a very practical application.

    Jason

  6. Hi, I don’t undestand one thing: why Arduino needs the same ground as the transistor/Engine?
    By the way great tutorial! I really undestood it! Thx!

  7. Nice tutorial for beginners! I understood it.
    Only question: the power for the arduino board is separate from the power for the motor? the ground is not the same for the arduino and the motor right?

    • Hi Mihai,
      You do use an external power supply for the motor separate from the Arduino, but you use a common ground – wire the Arduino ground to the external power supply ground.
      Paul

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s