Search/

Request a free quote

Online Circuit Simulator

DC motor speed control with an encoder: PI controller simulation

We typically respond within one business day.

A small 12 V DC motor turns a flywheel. An encoder on the flywheel sends pulses to an ATmega328P, which sets the motor voltage with PWM through a MOSFET. The program measures the speed every 10 ms and runs a PI controller. Here the whole loop is simulated together: the circuit, the motor and flywheel, the encoder and the chip running the program.

PlayPauseTimeThe recorded run could not be loaded.SpeedDutyCurrentbrakeThe flywheel is drawn 40 times slower than it turns.

Recorded run of the default program: blue, speed; orange, PWM duty; dashed, the setpoint of 1992 rpm. A brake of 20 mN m acts from 0.8 s.

What is being simulated?

The motor is a permanent magnet DC motor. Its armature has resistance R and inductance L, and it makes a back voltage k times its speed. The flywheel and rotor have inertia J, a little viscous friction b and a steady friction torque from the bearings and brushes.

L di/dt = v - R i - k w

J dw/dt = k i - b w - T_friction

PartValue
Supply12 V
Armature resistance R2.0 ohm
Armature inductance L1.0 mH
Motor constant k0.020 V s/rad
Inertia J (rotor and flywheel)3.2e-5 kg m2
Friction torque4 mN m
Encoder128 lines, two channels
PWM31.4 kHz

From these values the mechanical time constant is J R / k^2 = 0.163 s, about 330 times the electrical one (L / R = 0.49 ms). At full voltage the motor would run at 5506 rpm and draw 5.85 A when stalled.

What does the default program do?

It counts the encoder pulses in an interrupt, both edges of channel A, so one turn is 256 counts. Every 10 ms it takes the count as the speed: one count per 10 ms is 23.4 rpm. The knob on A0 sets the target. The PI controller then sets the duty: the error times 4.04, plus an integral that adds 0.246 times the error each step and stays inside the duty range.

The integral time, 164 ms, matches the motor time constant. The controller cancels the motor lag, so the speed rises without overshoot: in the recorded run it reaches 90 % of the setpoint at 0.09 s and overshoots by 0.3 %.

Default program in AVR assembly
Default program in C

Lessons and challenges

  1. Open loop. Set the duty to 100 % with no controller. The speed settles at 5506 rpm with a time constant of about 0.16 s, as the formulas say.
  2. Proportional only. Set KI to 0. The speed settles at 63.7 counts per 10 ms instead of 85: a proportional controller needs an error to make any duty at all.
  3. PI. With the default gains the error goes to zero. Check: within 2 % of the setpoint by 0.25 s, overshoot under 3 %.
  4. Gain and resolution. Each count of error moves the duty by KP. Above about KP = 33, the limit of the sampled loop, the duty swings over a wide range every few samples while the flywheel keeps the speed ripple near one count.
  5. Load. A 20 mN m brake acts at 0.8 s. The PI controller brings the speed back to 85 counts; with proportional only it stays low.
  6. Challenge: position. Change the driver to an H-bridge and write a position controller that turns the flywheel exactly one turn (256 counts) in under 0.6 s with less than 10 % overshoot.

Where do the parts go?

You can mount the encoder on the flywheel or on the motor shaft and choose its number of lines, and you can change the flywheel. Fewer lines give coarser speed readings: with 32 lines one count per 10 ms is 94 rpm, and the duty steps grow with it.

How were the numbers checked?

The motor model was checked against the exact two-pole step response, the steady speed with friction and the averaged PWM voltage. The programs, in assembly and in C, were run on an emulated ATmega328P with the motor model, and again on the project framework solver: both give the same speed within 1 %.

Speed and PWM duty of the motor against time in the recorded run
Blue: speed (rpm, left). Orange: duty (%, right). Dashed: setpoint. Dotted: the brake starts.

Get your project started

We typically respond within one business day.

Request a free quoteor call (866) 621-6920

Newsletter

Engineering notes from Dreiym

New articles on power systems, corrosion and forensic engineering, by email when we publish. Free account included: keep your calculator work, reports and exports in one place.

Latest article: DC Arc Flash in Battery, UPS and PV Systems: How the Hazard Is Assessed

One email per new article. Unsubscribe in one click.

You are subscribed: new articles come to you by email.

Manage in My account