13. Taking DC Motors for a spin!

Background

This project provides speed control of a DC motor via the serial terminal.

This program allows a DC motor to rotate at different speeds via PWM. We control the speed of the motor using the PWM. The speed can be selected using the keyboard. It can vary between 0 – 255. 2 LEDs will indicate if the requested speed is above or below the current speed.

Also, I tried powering a big boy motor that’s used in a kid’s ride-on vehicle thing however I discovered that it required more than 18V. Once I increased the speed, it would pull down motor VCC all the way to 4.5V. I captured it in Showcase.

The Theory

Switches

Microcontrollers are not designed to drive electrical loads such as motors. The solution is to use a transistor or FET as a switch.

Working with low-side switches is simpler since the GND of the transistor ( emitter for NPN BJT ) is connected to circuit/global ground rather than the high side of the motor.

For an NPN transistor, a small I_BE allows you to control a larger I_CE. This is leverage. For a NPN transistor, this occurs by raising Vb > Ve. I.e. you have to exceed V_BE which is normally 0.7V.

Most transistors have a voltage drop across the collector and emitter. This means you must increase Vcc to compensate. Also, heatsinking maybe required. If collector/emitter saturation voltage is 2V and you’re running a 1A motor, 2W is dissipated in the transistor.

MOSFET’s unlike BJT’s do not require current-limiting resistors on the base/gate since their not current-driven. They have no voltage drop and consume miniscule current.

Some FETs are designed to switch on and off very rapidly, making them suitable for PWM.

DC Motors

We make a DC motor spin by simply applying a voltage across its two terminals. It spins faster with more voltage and spins the other way if you reverse the voltage.

Hardware Connections

Schematic is available in the GitHub repository.

The Software

There is only one file.

This program allows a DC motor to rotate at different speeds via PWM. We control the speed of the motor using the keyboard. It can vary between 0 – 255. 2 LEDs will indicate if the requested speed is above or below the current speed. They represent acceleration or deacceleration.

Different motors respond to different PWM frequencies. In this program, the prescaling is 256 (TCCR0B |= (1 << CS02);), thus PWM frequency is 125 Hz. At 1024 prescaling, the PWM frequency is 31.25Hz.

We send the OCRnx value to a pin. This contains a PWM signal.

We initialize timer 0 for fast PWM

Fast PWM mode permits high frequency PWM signal generation.

The Showcase

Spinning the motor at max speed. It’s quite loud!

Attempting to spin a large DC motor. This was unsuccessful

I couldn’t run the big motor at 18V. Once I increased the speed, it would pull down motor VCC all the way to 4.5V. Perhaps I need to use the L293D motor driver.