Motor pinwheel on an Arduino Uno
Hold the button and a 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 Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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");
}
}
Parts list
8 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Pushbutton
- 1 × Resistor, 10k Ω
- 1 × MOSFET
- 1 × Diode
- 1 × DC motor
- 1 × Battery, 9 V, AA or 18650
How it is wired
7 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.
- Ground: Arduino Uno R3 pin GND; Resistor, 10k Ω pin 1; MOSFET pin S; Battery, 9 V, AA or 18650 pin V-
- Arduino Uno R3 pin 9; MOSFET pin G
- Arduino Uno R3 pin 2; Pushbutton pin 2a
- Arduino Uno R3 pin 5V; Pushbutton pin 1a
- Pushbutton pin 2b; Resistor, 10k Ω pin 2
- MOSFET pin D; Diode pin A; DC motor pin 2
- Diode pin C; DC motor pin 1; Battery, 9 V, AA or 18650 pin V+
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.