Built-in project · Arduino Uno R3

Motor pinwheel on an Arduino Uno

Hold the button and an IRF520 MOSFET on pin 9 runs a DC motor from a 9 V battery, with a 1N4007 across the motor to catch its kick. The whole build, an Arduino Uno and 6 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.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $36. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Switching a motor with a MOSFET
  • A flyback diode across an inductive load
  • Pull-down resistor on a button
  • Common ground between two supplies
  • Why a pin cannot power a motor directly
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

A pin gives a few milliamps at 5 V, but a motor wants hundreds at 9 V. An IRF520 MOSFET sits between them as a switch. A voltage on its gate lets the 9 V battery's current flow through the motor.

Hold the pushbutton and pin 2 reads HIGH. The sketch copies that straight to pin 9, the MOSFET gate, so the motor spins. A 10k resistor holds pin 2 low when the button is up.

A diode across the motor catches the voltage spike from its coil when it switches off. The battery's minus must be joined to the Uno's ground.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 10k Ω pin 1Arduino Uno R3 pin GND
MOSFET pin SArduino Uno R3 pin GND
Battery: AA, coin cell, LiPo pin V-Arduino Uno R3 pin GND
MOSFET pin GArduino Uno R3 pin 9
Pushbutton pin 2aArduino Uno R3 pin 2
Pushbutton pin 1aArduino Uno R3 pin 5V
Resistor, 10k Ω pin 2Pushbutton pin 2b
Diode pin AMOSFET pin D
DC motor pin 2MOSFET pin D
DC motor pin 1Diode pin C
Battery: AA, coin cell, LiPo pin V+Diode pin C

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 pinwheel: hold the button and a MOSFET runs a DC motor from a 9 V
// battery.
//
//   pin 2    the button: one leg to 5V, the other to pin 2 and through a 10k
//            resistor to GND
//   pin 9    the MOSFET's gate
//   MOSFET   source -> GND, drain -> the motor's other lead
//   motor    one lead to the battery's +9 V, the other to the drain
//   diode    across the motor, its stripe to +9 V
//   battery  its - joined to the Uno's GND
//
// A pin gives a few milliamps at 5 V and a motor wants hundreds at 9. The
// MOSFET is the switch between them: a voltage on its gate (and almost no
// current) lets the battery's current through the motor. The diode catches the
// spike the motor's coil throws back when it is switched off, which would
// otherwise hit the MOSFET.
//
// The grounds have to be joined. The gate voltage only means something
// measured against the source, which sits on the battery's minus.

const int BUTTON = 2;
const int MOTOR = 9;

int last = LOW;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(MOTOR, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: motor pinwheel");
}

void loop() {
  int pressed = digitalRead(BUTTON);
  digitalWrite(MOTOR, pressed);
  if (pressed != last) {
    last = pressed;
    Serial.println(pressed == HIGH ? "motor on" : "motor off");
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

It makes BUTTON (pin 2) an input and MOTOR (pin 9) an output, then opens serial at 9600 and prints a start message. Nothing else is needed.

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(MOTOR, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: motor pinwheel");
}

loop sketch.ino

It reads the button and writes the same level to the motor pin, so the motor runs only while you hold the button. When the reading differs from last, it saves the new value and prints "motor on" or "motor off", so the monitor shows each change once.

void loop() {
  int pressed = digitalRead(BUTTON);
  digitalWrite(MOTOR, pressed);
  if (pressed != last) {
    last = pressed;
    Serial.println(pressed == HIGH ? "motor on" : "motor off");
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The motor does not spin This project
    Check that the battery's minus is joined to the Uno's GND and the MOSFET source. The gate voltage only works against the source.
  • The diode seems to do nothing, or things get hot This project
    The diode goes across the motor with its stripe on the +9 V side. Backward, it shorts the battery.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The load stays partly on or flickers when the pin is not driving, such as at boot or in input mode. MOSFET
    A floating gate picks up noise. Add a pull-down of about 10 k from gate to source on an N-channel part, or pull a P-channel gate up to its source voltage.
  • The N-channel load is always on or never turns on. Source, gate, and drain legs were mixed up. MOSFET
    Use the N-channel as a low-side switch: load to the supply, drain to the load, source to ground. Check your datasheet pinout, since 2N7000 and BS250 differ. Share the grounds.

Change it and keep it

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