DC Motor - PWM

coparu67

Senior Member
#1
Hi
I'd like to control the speed of a 5V DC motor using a CHI035 board (PICAXE 18M2+)
There are two pins, B.3 and B.6, to work with PWM.

1) In the wizard, I can set the Frequency (61 - 500000Hz).
But, which is the best frequency to work with a DC motor? It doesn' matter?

2) With PWM, is it possible to get a reversible movement for the motor?

3) Could I use an external Stepper-Motor-Drive-Controller to control (PWM) two motors with reversible movement?

Thanks
 

AllyCat

Senior Member
#2
Hi,

1) Generally 500 kHz is far too high and may have high dissipation/losses, even assuming that it gives sufficient resolution (number of PWM steps). Sometimes you might use close to 20 kHz to avoid the motor "whistling" noise. However, often the lowest possible frequency is best; it gives the motor little "kicks" which can overcome static friction and give a smoother low speed drive, with subjectively higher torque.

2) The easy way is to use a relay/switch to reverse the coil connections! Alternatively you could switch one end of the motor coil(s) between ground and the supply rail (using a switch, relay or FET/bipolar transistors), but the phase (duty cycle) needs to operate in the opposite direction (slow -> fast) between forward and reverse.

3) An "H-bridge" is often used to drive individual motors and a stepper driver should have two (or more) suitable output stages. The stepper control logic may simplify driving the motors (e.g. reversing the PWM phase), but it will depend on the design of the logic. Normally the (two "bipolar") stepper coils will be driven by similar pulses in sequence, whilst you probably need independent control of the two motors?

Cheers, Alan.
 

hippy

Technical Support
Staff member
#3
The CHI035 board was not designed for PWM control of motor speed but I believe, if PWMOUT B.6 is used to set the speed, setting B.7 high or low will set the direction the motor will move in at that speed.

The L293D has two inputs which determine how the motor will move, and it won't turn when the two inputs are the same -

Code:
0 0 - Stopped
0 1 - One way
1 0 - The other way
1 1 - Stopped
By keeping one input high or low with PWM to the other, the motor will quickly switch between turning and stopped which gives speed control.

Note that you will have to adjust the duty depending on direction because there is a an effective signal inversion of PWMOUT depending on whether the other pin is low or high. 25% duty in one direction will mean 75% duty in the other direction if not corrected.

An external motor controller can be used if wired appropriately. It should be fairly easy to remove the L293D from the CHI035 and place it on breadboard or stripboard.
 
#4
I can confirm that Hippy's suggestion works well with the CHI035.
I would agree with Alan about frquencies. The PE6 default for flowcharts is 10kHz and suggest start with that. If the motor speed is uneven at low rpm, then reduce freq. If whistling is load enough to be annoying, then increase to above 20kHz (ie higher than humans can hear).
I tend to use a number of preset speeds (approx 8) which I found helps if using inputs to increase or decrease the speed.
Have fun!
 

hippy

Technical Support
Staff member
#5
I can confirm that Hippy's suggestion works well with the CHI035.
Many thanks for confirming that. Two diagrams which may help understanding of how it works and why the duty has to be adjusted -
Code:
B.7   __________________________________  Low
        ______      ______      ______
B.6   _|      |____|      |____|      |_  60% duty

          01    00    01    00    01

       .------.    .------.    .------.
Drive -| Fwd  |----| Fwd  |----| Fwd  |-- 60% on
       `------'    `------'    `------'
Code:
      __________________________________
B.7                                       High
      _        ____        ____        _
B.6    |______|    |______|    |______|   40% duty

          10    11    10    11    10

       .------.    .------.    .------.
Drive -| Rev  |----| Rev  |----| Rev  |-- 60% on
       `------'    `------'    `------'
Forward and reverse are arbitrary and depend on which way round the motor is wired.
 

erco

Senior Member
#6
I'm in the middle of a PWM project right now. Per Alan, I go with the lowest frequency the Picaxe can deliver, often ~62 Hz. I'm using a 14M2 to control an L298N H-bridge with 24V motors. I saved one pin per channel per the mod below.

[video]https://youtu.be/PTB6R1s44dI[/video]
 

PieM

Senior Member
#7
Could I use an external Stepper-Motor-Drive-Controller to control (PWM) two motors with reversible movement?
Yes, with L293D or L298, but apply PWM on enable pins. (pin 1 and 9 on L293D or pin 6 and 11 on L298)
 

hippy

Technical Support
Staff member
#8
I saved one pin per channel per the mod below.

[video]https://youtu.be/PTB6R1s44dI[/video]
I am not sure that pin saving works as desired if controlling bi-directional speed using PWM on an input.

With PWM on an input, considering post #5; one direction will alternate between "run and stop" (01/00) the other will alternate between "run and brake" (10/11).

It may be that there is often no observable difference in practice but the two are different in nature.
 

erco

Senior Member
#9
Thanks for your interest, hippy. Not so many people appreciate the difference between coast & brake! The motor does actually coast in both directions since I'm PWMing both Input pins, as opposed to the asymmetrical function shown in post #5, the bane of using a single PWM signal from pin B.6. My video is just to show that 2 control lines can still deliver all 4 functions, forward, reverse, coast & brake.

IOW, the motor is coasting in both directions when PWM is low using (01/00) and (10/00). Optionally it could brake in both directions when PWM is high using (01/11) and (10/11). That would demonstrate the 60/40 duty cycle swap shown in #5.
 
Last edited:
Top