Home/ Projects/ Motor speed control
Project 10 of 16 Motion Intermediate

Motor speed control

Turn the knob, the motor changes speed. PWM switches power on and off faster than the motor can respond, so average voltage falls and so does speed. You also learn why a motor never connects to a GPIO pin: it draws far too much current and dumps voltage spikes back down the line when it stops. The L9110 module handles both problems for you.

ESP32 & ESP8266 IoT Automation Kit
One of 16 projects in the ESP32 & ESP8266 IoT Automation Kit — the parts below are from that kit.

What this project uses

Every item is in the kit — nothing extra to buy.

Everything for this project

ESP32 & ESP8266 IoT Automation Kit

All 24 parts in one box, with this project and 15 more to build from them.

AED 180.00 See the kit
Need only these parts? Order them loose · from AED 115.63
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 115.63

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

10 Motor speed control Arduino C++
/*
  10 — Motor speed control
  Board: ESP32.  Parts: potentiometer, DC motor, L9110 module.

  Wiring:
    Potentiometer: outer legs -> 3V3 and GND, wiper -> GPIO 35
    L9110 module:  VCC -> VIN (5 V), GND -> GND, A-1A -> GPIO 16, A-1B -> GPIO 17
                   Motor -> the module's MOTOR A screw terminals
                   (never a motor straight on a GPIO: far too much current, and
                    a voltage spike back down the line when it stops)

  Turn the knob, the motor changes speed. PWM switches the power on and off
  faster than the motor can respond, so the average voltage — and the speed —
  follows the duty cycle. Below ~30 % most small motors just hum: MIN_DUTY skips that.
*/

const int POT_PIN = 35;
const int A1A_PIN = 16;     // PWM = speed
const int A1B_PIN = 17;     // LOW  = forward (swap the two for reverse)
const int MIN_DUTY = 70;    // 0..255; below this the motor stalls and hums

void setup() {
  Serial.begin(115200);
  pinMode(A1A_PIN, OUTPUT);
  pinMode(A1B_PIN, OUTPUT);
  digitalWrite(A1B_PIN, LOW);
}

void loop() {
  int raw  = analogRead(POT_PIN);                 // 0..4095
  int duty = map(raw, 0, 4095, 0, 255);
  if (duty < 8) duty = 0;                         // knob at the bottom = fully off
  else if (duty < MIN_DUTY) duty = MIN_DUTY;

  analogWrite(A1A_PIN, duty);                     // ESP32 core 2.0.2+ supports analogWrite

  Serial.print("knob "); Serial.print(raw); Serial.print("  duty "); Serial.println(duty);
  delay(50);
}
Needs ESP32 board package 2.0.2 or newer (analogWrite).

More from this kit

All projects →

Root
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.