Using a transistor to drive a higher-current or -voltage load

The transistor’s job is to act as a switch; the Arduino doesn’t need to touch the 6V external power supply shown in this circuit, but can just flip the “switch” on pin 5 to affect the motor on the left, or pin 8 to affect the motor on the right.

This circuit uses a ULN2803 transistor, which is actually a single IC (chip) with 8 different transistors in it.

A digital HIGH signal from the Arduino will turn the motor on, and a LOW signal will turn it off. Notice that the motors are always connected to power (they are plugged right into the 6V supply), so what the transistor does in this case is “turn on and off” their connection to ground. It’s a funny logic, but it works!

The circuit

../_images/uln2803schematic.png

The diodes attached parallel to the motors are there for the protection of the transistors. They are necessary because when power is suddenly shut off to a coil (motors have big coils in them), that coil has a tendency to kick with a high voltage for a brief period. The diodes give that kick somewhere to go so it doesn’t damage anything. You don’t need diodes like these if you’re powering something without a coil, such as an LED strip.

Arduino code to move the servo around a bit

Sample Arduino sketch to play with switching two loads on and off:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int motorOne = 5; // shortcut to refer to the motor pin later
int motorTwo = 8;

void setup() {
  pinMode(motorOne, OUTPUT); // we will be writing data to the motor pins
  pinMode(motorTwo, OUTPUT);
}

void loop() {
  // we can “blink” pin 8 (motorOne) by just turning it on for a bit
  digitalWrite(motorOne, HIGH);
  delay(500);
  digitalWrite(motorOne, LOW);


  // we can also fade pin 5 (motorTwo) up and down, because it’s a PWM (tilde)
  // pin so it can do analog write in addition to digital write

  // fade from 0 to 255, which is the full range of analogWrite
  for (int i = 0; i < 256; i++) {
    analogWrite(motorTwo, i);
    delay(5);
  }

  // fade back down from 255 to 0
  for (int i = 255; i > 0; i--) {
    analogWrite(motorTwo, i);
    delay(5);
  }
}

Download link for the above code: transistor.ino.

Table Of Contents

Previous topic

Using a hobby servomotor

Next topic

Tutorials