It is easy to control the speed of a DC motor using PIC16F877A microcontroller since this microcontroller has a CCP module to generate a PWM signal, and by varying the duty cycle of the PWM signal the power delivered to the motor will also vary which causes the speed to change. This topic shows how to make a DC motor speed controller using PIC16F877A where a potentiometer is used to control motor speed.
If you need to see how to use potentiometer with PIC16F877A visit the following topic:
PIC16F877A ADC example with CCS PIC C compiler
And if you need to see how to generate a PWM signal using PIC16F877A visit the following topic:
PIC16F877A PWM example with CCS PIC C compiler
DC Motor speed control with PIC16F877A circuit:
To control the speed of a DC motor only one transistor is needed, in this project an N-type mosfet is used as shown in the circuit schematic below:
The potentiometer used to control the speed of the motor, it is connected to AN0. The microcontroller reads the analog data from AN0 channel and uses the digital value (after conversion) to set the PWM duty cycle.
PIC16F877A CCP1 module is used as PWM with a frequency of 500Hz.
The MCU must be supplied with 5V between pins VDD and VSS.
DC Motor speed control with PIC16F877A CCS C code:
The code is described through comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // DC motor speed control using PIC16F877A CCS C code #include <16F877A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #device ADC = 10 #use delay(clock = 8000000) unsigned int16 i ; void main(){ setup_adc(ADC_CLOCK_DIV_32); // Set ADC conversion time to 32Tosc setup_adc_ports(AN0); // Configure AN0 as analog set_adc_channel(0); // Select channel AN0 setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM setup_timer_2(T2_DIV_BY_16, 250, 1); // Set PWM frequency to 500Hz delay_ms(100); // Wait 100ms while(TRUE){ i = read_adc(); // Read from AN0 and store in i set_pwm1_duty(i); // PWM1 duty cycle set delay_ms(10); // Wait 10ms } } |
DC Motor speed control with PIC16F877A video:
The following video shows a hardware circuit for this project.
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Hi there, please share the PID controller code for dc motor in ccs c
The tutorial is very helpful. Thank you.