Built-in project · Arduino Uno R3

Motor on a transistor, on an Arduino Uno

A DC motor driven from pin 9 through a 2N2222, with a flyback diode across it. The PWM ramps up and down and the shaft follows. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

IntermediateRuns in your browser. Free, and no account needed.

Motor on a transistor · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Motor ramp: a motor through a transistor, sped up and slowed down.
//
//   pin 9  through 1 k into the transistor's base
//   the transistor's emitter to GND, its collector to one motor terminal
//   the other motor terminal to 5 V, and a diode across the motor with its
//   band (cathode) on the 5 V side
//
// A board pin can give about 20 mA. A small motor wants ten times that
// standing still, so the pin drives a transistor and the transistor drives the
// motor: a little base current through the 1 k lets a much larger one through
// from collector to emitter. The PWM on pin 9 switches that current on and
// off 490 times a second, and the motor's own inertia averages it into a
// speed.
//
// # The diode
//
// A motor is a coil. The instant its current is cut, the coil tries to keep it
// flowing and the voltage on the collector flies up until something gives,
// which is usually the transistor. The diode across the motor gives the
// current a way round instead: it is reverse biased while the motor runs and
// only conducts on that kick. Leave it out on the bench and the transistor
// lasts an afternoon.
//
// The duty walks from 0 to 255 and back, five counts every 40 ms, so a whole
// cycle takes about four seconds.

const int DRIVE = 9;
const int STEP = 5;
const int STEP_MS = 40;

int duty = 0;
int direction = STEP;

void setup() {
  pinMode(DRIVE, OUTPUT);
  analogWrite(DRIVE, 0);
  Serial.begin(115200);
  Serial.println("Mokxi Uno: motor on a transistor, pin 9 PWM");
}

void loop() {
  analogWrite(DRIVE, duty);
  if (duty % 50 == 0) {
    Serial.print("duty ");
    Serial.print(duty);
    Serial.println(" / 255");
  }
  duty += direction;
  if (duty >= 255) {
    duty = 255;
    direction = -STEP;
  } else if (duty <= 0) {
    duty = 0;
    direction = STEP;
  }
  delay(STEP_MS);
}

Parts list

6 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 9; Resistor, 1k Ω pin 1
  • Arduino Uno R3 pin 5V; Diode pin C; DC motor pin 1
  • Ground: Arduino Uno R3 pin GND; Transistor (NPN or PNP) pin E
  • Transistor (NPN or PNP) pin C; Diode pin A; DC motor pin 2
  • Transistor (NPN or PNP) pin B; Resistor, 1k Ω pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.