> For the complete documentation index, see [llms.txt](https://monasheng.gitbook.io/zerotohero/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monasheng.gitbook.io/zerotohero/arduino-zero-to-hero/advanced-skills/dc-motors.md).

# DC Motors

DC Motors are used to provide a driving torque, usually for locomotion purposes. As the name suggests, DC motors are powered using *direct-current* electricity.

## Fundamentals

The most common type of DC Motor, called a *permanent magnet motor* essentially consists of:

1. A series of permanent magnets connected to a stationary outer casing (referred to as a *stator*), and,
2. A series of long coils of wire that can rotate on a shaft (referred to as a rotor).

When a voltage is applied to the rotor coils of a DC motor, a current runs through them and they induce a magnetic field. This field interacts with the permanent magnet son the stator and creates a torque that causes the rotor shaft to spin.

<figure><img src="/files/KAoxVEUfJUsLfRk3TVGw" alt=""><figcaption><p>DC Motor Principles. From: <a href="https://commons.wikimedia.org/wiki/File:Electric_motor.gif">https://commons.wikimedia.org/wiki/File:Electric_motor.gif</a></p></figcaption></figure>

### Motor Parameters

The amount of torque you can extract from a DC motor, and the top-speed you can reach, are functions of its construction and the inputs you apply. In general:

* Higher input voltages result in higher motor speeds (RPM).
* Higher coil currents result in higher output torques (Nm).

The particular relationships between voltage-speed and current-torque depend on the motor construction - number of coil turns, size of magnets, etc. Motors are usually provided with a datasheet that shows graphs of these relationships.

{% hint style="danger" %}
:zap: **Warning:** Motors will have a maximum input voltage you can safely apply, and maximum current that the coils can bear. Consult the datasheet for each motor about more information about these limits.
{% endhint %}

## DC Motor Speed Control

Controlling the speed of a DC motor is theoretically straightforward, as this is near-linearly related to the input voltage to the coils.&#x20;

Achieving speed control however can be quite tricky, as:

* You need to be able to alter the input voltage with some precision,
  * while delivering reasonably high currents (e.g. several $$\text{A}$$ for a WARMAN-spec robot),
    * without letting this current draw alter your supplied voltage (i.e. by compensating for battery voltage sag), and,
      * with (ideally) the ability to change the polarity of this voltage to reverse the motor.

While microcontrollers have the ability to author analog voltages using `analogwrite()` and a PWM pin, the maximum current they can suppy is *not even close* to that which might be drawn by a DC motor, and any attempt to do so will certaintly fry the board.

### DC Motor Drivers

Mercifully, this is a solved problem. Off-the-shelf *DC Motor Controllers* (sometimes called DC *Motor Drivers*) exist to serve this purpose, by:

1. drawing input power from a high-current-capable source, like a battery,
2. receiving a speed command from an MCU via PWM, and/or a direction command from the MCU as a digital signal, and,
3. providing an output voltage to a motor as a function of this PWM command.

<figure><img src="/files/hdUfa6frV6LpWEzND1W0" alt=""><figcaption><p>DC Motor Functional Block Diagram</p></figcaption></figure>

Some motor drivers also have switching ability on their output as a safety function, and require one or more `ENABLE` signals to be sent from an MCU in addition to speed and direction in order to turn on.
