Summary of DC motor speed control using PWM using PIC16F876
This article details a PWM-based DC motor speed control project using a PIC16F876 microcontroller. The author created a custom circuit to avoid issues with existing solutions, such as excessive component counts and audible buzzing from low-frequency PWM. The design utilizes the CCS C compiler, EAGLE for schematic/PCB layout, and specific timer/CCP configurations to adjust motor speed based on ADC input.
Parts used in the Motor Speed Control Project:
- PIC16F876 microcontroller
- CadSoftUSA EAGLE software
- CCS PCM compiler
- ADC (Analog-to-Digital Converter)
- Timer 1
- Timer 2
- CCP1 module
- CCP2 module configured for PWM
- Fan motor
If you do not have EAGLE, you can download a full working version from CadSoftUSA http://www.cadsoftusa.com
Here is a zip file with the EAGLE schematics and PCB layout. http://hans-w.com/MotorSpeedEAGLE.zip
New ! Source code available
C source code… requires CCS PCM compiler.
PCM is available from CCS http://www.ccsinfo.com/picc.shtml
//(C)Copyright 2002 Hans Wedemeyer Houston Texas U.S.A
// for use with Fan motor speed control
// see schematic PWM Motor Speed Control
// Aug. 10 / 2002 This matches the new EAGLE schematic and PCB layout.
//
#include “Motor.h”
long ADCValue=0;
long ADCValueOld=1;
#byte portA = 0X05
#byte portB = 0X06
#byte portC = 0X07
void main()
{
set_tris_A(0b00101011);
set_tris_B(0b00000001);
set_tris_C(0b00000000);
portA=0X00;
portB=0X00;
portC=0X00;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,WDT_288MS);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_1,255,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_PWM);
enable_interrupts(global);
set_adc_channel(0);
delay_us(10);
while(1)
{
ADCValue = Read_ADC();
delay_ms(100); // monitor 10 times a second
if ( ADCValue != ADCValueOld )
{
set_pwm2_duty(ADCValue);
ADCValueOld = ADCValue;
}
}
}
Source code and Hex files are available here: http://hans-w.com/motorspeed.zip
////////////////////////////////////The original text and pictures://////////////////////////////////////////////////////////////
I tried several ready made PWM speed control circuits. Of the few I tried, there were problems that I did not like.
Here is the schematic.
1) Lots of components.
2) PWM running as a low frequency which became audible as a loud buzzing sound from the motor.
3) I needed to build it now, and did not have all the components.
My solution, although it’s much like cracking nuts with a sledge hammer, solved these problems.
For more detail: DC motor speed control using PWM using PIC16F876
- Where can I download the EAGLE schematics?
A zip file containing the EAGLE schematics and PCB layout is available at http://hans-w.com/MotorSpeedEAGLE.zip. - What compiler is required for the source code?
The C source code requires the CCS PCM compiler which is available from ccsinfo.com. - Why did the author create a new circuit instead of using ready-made ones?
Ready-made circuits had problems including too many components, audible buzzing from low frequency PWM, and lack of immediate availability of all parts. - How often does the system monitor the ADC value?
The system monitors the ADC value ten times a second using a 100-millisecond delay. - Which CCP module is configured for PWM output?
The setup_ccp2 function is called with CCP_PWM to configure the second Capture Compare Pulse Width Modulation module. - What internal clock source is used for the ADC?
The code uses setup_adc(ADC_CLOCK_INTERNAL) to select the internal clock for the analog-to-digital converter. - Is there a way to get the compiled hex files?
Source code and Hex files are available for download at http://hans-w.com/motorspeed.zip. - What date was the schematic updated to match the new layout?
The schematic and PCB layout were matched on August 10, 2002.

