Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM question

Not open for further replies.


New Member
what is the function of the PWM command in PIC basic? is it used to drive the motor?or is it possible to drive the dc motor using the PWM command, without using the PWM circuit?i'm using PIC16F877
PWM is pulse width modulation

It will output at a certain frequency and you can then change the duty cycle.
This means that while the frequency remains the same you can change the length of the ON time of 1 period.

0% duty cycle is always off
50% duty cycle is half of one period on, other half off
100% is always on
Not directly,due to the rather small amounts of current the PIC can sink/source.But indirectly (through a transistor,MOSFET,etc) it can be done.
i am a PIC newbie. since i wan to build a car which its speed is able to vary, can i use the PWM pulse from the PIC to vary its motor i know, there is a PWM command in PIC basic. can i use it for that purpose?
is it the transistor is used as amplifier and switch and i just need to send the PWM pulse to it so that it can switch ON and OFF? that means i dont need any motor drive circuit?
The MOSFET's or transistors in this case would be a switch, and no external motor drive circuit would be necassary but....
1-This could only control the motor in one direction, so some form of direction control would be need to be used(such as an H-Bridge)
2-The PWM command outputs a pretty ugly signal, so minor filtering is needed(not sure how much for a motor circuit) such as a cap.
3-No other instructions will work during execution of the PWM command(so your pic couldn't do anything else while driving the motor)

If you are using Pic Basic Pro and your PIC has a hardware PWM module(CCP Module) the HPWM command would be better.This is a cleaner signal and runs in the background(so other commands will work while the pwm signal is used).I'll see if I can find some examples and post them.
Oh yeah , since you are switching an inductive load, some form of flyback protection would be needed.
thank u Eclipsed.
but in my project, the motor will only rotate in one direction, and its speed it able to vary. so i think H-bridge is not necessary. isnt it?
actually the speed is varying according to the distance of the transmitter source. when the transmitter source is far, the motor speed is increased in order to maintain constant distance with the transmitter.
and when the source is near with the car, the motor will rotate slowly. so that the car will not hit the trasnmitter. and when the transmitter is OFF, the motor will stop rotating.
do u have any idea on how to implement it with PIC16F877?
i use an IR and some comparator circuits. it is just a simple circuit. and it works fine.what i need now is the circuit to control the speed of the motor with PIC.
i need to submit the project by the mid of next month. hope somebody can help me
thank u Eclipsed.some of the links are useful
as i mentioned before, my car needs to vary the motor speed according to the distance of the transmitter source and the car will stop when the transmitting source is OFF. i have few questions:
(1) which command is appropriate?HPWM or PWM commands in PIC basic pro?
(2)i have read the programming manual and i know that the HPWM command is running continuosly in the background. that means the motor will continue to run without stopping, although the transmitting source is OFF?
You can alter the duty cycle and frequency using the HPWM command, just like you can with the PWM command.You just put a different value in the PWM module's registers(it's easier than it sounds)
can i directlu use the command in the PIC basic pro:
HPWM channel,dutycycle,frequency
to generate the PWM pulse

or i need to follow the steps shown in the following link:
**broken link removed**

do i need to filter out the PWM generated? if do, how to implement it?
You can do it either way(melabs example or with the HPWM command).That sample program will work for your app too.Just swap your distance calc code with their duty cycle sweep code and you should be set to go.Use their DUTY variable to alter speed ,for example, DUTY = 200 your motor speed = %20 or DUTY = 800 your motor speed = %80 or DUTY = 0 your motor speed = %0 or DUTY = 1000 your motor speed = %100.
Oh yeah, you shouldn't need to filter PWM from the CCP module of the pic.Just use a transistor or MOSFET and make sure your frequency is high enough, otherwise you will get a high pitch noise from the motor.
when the harware PWM is running, does the interrupt will terminate the execution of the HPWM? or the HPWM is stil running at the background while the interrupt is executing?
Hardware PWM will keep on running until you turn it off.
Interrupt will not influence HWPWM
can anyone plz help me again?
today i hooked up the circuit to test the HPWM feature of the PIC16F877 but it doesnt seem to be work properly.

my testing code is shown below:




HPWM 1,191,20000 'OUPUT 75% DUTYCYLE 20KHZ

HPWM 1,127,20000 'OUTPUT 50% DUTYCYLE 20KHZ



What's wrong with the code? i just want to output certain frequency on pin RC2 when port A pins receive signal, but it doesnt work properly.
by using the oscillosocpe, i can see that the output is not a pure square wave, but is distorted. the frequency is only 50Hz!!!! the duty cycle doenst change at all.

is that anything wrong with the code? plz help motor circuit wont run without it..

in this case,do i need to pull down all the unused pins?i only use two pins to detect signal and one pin as an output?

i connect the PIC based on the following diagram. but i also saw slightly different type of connection at **broken link removed**

they dont connect both Vcc and Vdd. which one is correct connection?
Not open for further replies.

Latest threads