Driving a DC (direct current) motor from a microcontroller can be very useful for rotational applications that do not require precision positioning. A useful example is controlling the speed of a cooling fan. This tutorial walks you through how to use the MPLAB® Xpress board to control a DC motor with a low side drive circuit.
Hardware Tools (Optional)
|Tool|| About|| Purchase|
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
This project drives a DC motor with a PIC16F18855 microcontroller on the MPLAB Xpress Development Board.
The project uses the following hardware:
- Understanding of P-channel and N-channel MOSFETs
- Introduction to PWM Module: [Link to Wiki]
- Microchip AN905 "Brushed DC Motor Fundamentals"
This tutorial focuses on driving the motor in a single direction using low-side control, meaning the MOSFET is placed between the motor and ground. MOSFETs are nothing more than an electronically controlled switch. Because micro controller I/O pins generally cannot directly provide the current necessary to drive a motor, the MOSFET allows the microcontroller to control the current to the motor without directly supplying it. For more information you can refer to the application note AN898 "Determining MOSFET Driver Needs for Motor Drive Applications". The following schematic demonstrates the low-side control circuit used for this tutorial:
Pay attention to which of the MTP50N06V pins correspond to Gate, Drain, and Source. The Gate pin should be connected to pin RA4 through the 10 KΩ resistor. R1 is placed there as a current limiting resistor to protect the microcontroller, while R2 pulls the MOSFET gate LOW unless the microcontroller drives the gate HIGH. The Drain pin should be connected to a power supply. Finally, the Source pin should be connected to the positive terminal of the motor and the source of the diode.
We will use the Pulse Width Modulation (PWM) module to control the gate of the MOSFET. Sending the 3.3 V signal from the PIC16F18855 to the gate of the MOSFET will turn ON the MOSFET allowing current to flow between the Drain and Source. The current flowing is proportional to the duty cycle value of the PWM signal.
From MPLAB Xpress IDE, click the MCC icon to open the Microchip Code Configurator. From there, choose the PWM6 module from under PWM in Device Resources:
The default settings for the module will work for this application.
The PWM module needs a timer, and therefore we will use Timer2 for this application. Choose the TMR2 under Timer in Device Resources:
This will open the TMR2 configuration window. The timer period is changed to 100 ms. The Clock Source needs to be changed to FOSC/4. To change the period, we must also increase the Prescaler to 1:128. Check your settings with the completed window below:
Changing the prescaler value will change the Actual Period to 15.872 ms, which is reflected in the PWM module window below:
Generate a .hex file by clicking Make and Program Device
Program the MPLAB Xpress board by dragging the generated project .hex file from the downloads section of the browser and dropping the file onto the XPRESS drive.
The Programmer LED on the MPLAB Xpress board should quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18855.
The MPLAB Xpress board will control the the gate of transistor Q1 and send a 50% duty cycle to rotate the motor at the fixed speed in one direction. To change the speed, the PWM signal needs to be changed. A smaller duty cycle will slow down the motor and a longer duty cycle will speed up the motor. This is caused by the averaging affect of the Q1 output signal.
To be able to control direction of the motor an H-Bridge drive circuit is recommended. The Bi-directional Motor Control page explains how to control direction with an H-Bridge setup.