A motor on Arduino Uno

Steps done: 0 of 5

~9GNDUNOATMEGA328P-PU

Guess the answer and press Run

D9, PWM0 %rpm0 %time, s0.0

Lesson steps

What will analogWrite(motor, 128) do?

Pick an answer before you run it.

What will the motor do with analogWrite(motor, 128)?

Sketch

motor_ramp.inoTo board
// Motor through an IRLZ44N MOSFET: gate through 220 Ω to D9, 10 kΩ from gate to GND
// 1N4007 diode across the motor, motor on three AA batteries, battery minus to GND

const int motor = 9;
int upMs = 0;        // pause per speed-up step, ms
int downMs = 0;      // pause per slow-down step, ms
int holdMs = 2000;   // full speed, ms

void setup() {
  pinMode(motor, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (int s = 0; s <= 255; s += 5) {
    analogWrite(motor, s);
    delay(upMs);
  }
  Serial.println("полный ход");
  delay(holdMs);
  for (int s = 255; s >= 0; s -= 5) {
    analogWrite(motor, s);
    delay(downMs);
  }
  Serial.println("стоп");
  delay(2000);
}

Tap a line or an orange number

Serial monitor

Serial, 9600 baudboard time 0.0 s

About the lesson

A 130 motor with a fan blade powered by three AA batteries, the battery minus shared with the board GND. A MOSFET IRLZ44N turns the motor on: the gate through 220 Ω to D9 and 10 kΩ to GND, a 1N4007 diode across the motor with the stripe to the plus. About 12 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.

Build it for real

Parts

  • Arduino Uno R3
  • Mini breadboard
  • 130 motor with an impeller
  • MOSFET IRLZ44N
  • 1N4007 diode
  • 220 Ω and 10 kΩ resistors
  • Holder for 3 AA batteries
  • 6 male-to-male jumper wires

Wires

FromTo
D9220 Ω resistor, then the MOSFET gate (G)
Gate (G)10 kΩ resistor, then the source (S)
Source (S)Board GND and the battery minus
Drain (D)Motor pin M2 and the diode anode
Battery plusMotor pin M1 and the diode cathode (stripe)

The motor runs on batteries. At startup it draws up to 500 mA, and from the board 5V over USB the board would reset. Connect the battery minus to the board GND.

The sketch below goes to the board with the default numbers: upMs 0, downMs 0, holdMs 2000. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.

Flash it to your own board

The whole sketch

// Motor through an IRLZ44N MOSFET: gate through 220 Ω to D9, 10 kΩ from gate to GND
// 1N4007 diode across the motor, motor on three AA batteries, battery minus to GND

const int motor = 9;
int upMs = 0;        // pause per speed-up step, ms
int downMs = 0;      // pause per slow-down step, ms
int holdMs = 2000;   // full speed, ms

void setup() {
  pinMode(motor, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (int s = 0; s <= 255; s += 5) {
    analogWrite(motor, s);
    delay(upMs);
  }
  Serial.println("полный ход");
  delay(holdMs);
  for (int s = 255; s >= 0; s -= 5) {
    analogWrite(motor, s);
    delay(downMs);
  }
  Serial.println("стоп");
  delay(2000);
}

What to keep in mind on a real board

Never connect the motor to a pin directly: a pin gives up to 20 mA, and a 130 motor draws up to 500 mA when stalled. The pin or the whole microcontroller will burn out.

Without the diode, the motor coil gives a spike of tens of volts when switched off and breaks down the transistor. The diode stripe faces the battery plus.

The IRLZ44N opens at 5 V on the gate. The similar IRF520 is rated for 10 V, does not open fully from an Uno pin and gets hot.

The sketch takes 2376 bytes of flash and 222 bytes of RAM.

Licenses of the lesson code

The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.

The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.

MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.