Category Archives: Uncategorized

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

}

 

Advertisements

Michael Margolis’ “Arduino Cookbook”

I just bought a copy of Michael Margolis’ “Arduino Cookbook” from O’Reilly Press, and it almost makes this blog feel redundant. This is exactly what I’ve been looking for as a teacher – snippets of code to let you do just about anything you want with the Arduino board. I haven’t had time to go through it in depth, but if your priority is to get your project up and running rather than learning how to code from the Arduino reference webpage, this book is for you. Very highly recommended.