PWM Motor Control with Soft Start, Soft Stop Example Please

# Thread: PWM Motor Control with Soft Start, Soft Stop Example Please

1. ## PWM Motor Control with Soft Start, Soft Stop Example Please

Hi All,

It has been a while!

I need to drive a small 12v motor with PWM speed control to include soft start and stop. I would like to keep the circuit board small and I have a bunch of 675's at my disposal. Ultimately I would like to use a single pot to determine both speed and direction with half-way being the motor stop position. Can you point me to some fairly simple code examples (it has really been a while :-) to get me going?

Thank you!!!

Ross

2. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Ross

Here's a snippet of some old code that might get you started with the pot control:

Code:
```DEFINE ADC_BITS     8   ' Set number of bits in result
DEFINE ADC_CLOCK    3   ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50  ' Set sampling time in uS

Speed   VAR WORD
Wait_1  VAR WORD

' Turn ON every other output varying speed with pot from center
Pots:

IF Speed < 128 THEN       'Checks if pot in CCW position range
wait_1 = ABS(Speed-128)   'Sets pot value to increase in CCW direction
PORTC = \$50               'Turn outputs 4 and 6 ON
ENDIF
PAUSE wait_1              'Pause with new pot value
PORTC = \$0                'Turn OFF all outputs

IF Speed >= 128 THEN      'Checks if pot in CW position range
wait_1 = (Speed-128)+1    'Sets pot value to increase in CW direction
PORTC = \$A0               'Turns outputs 5 and 7 ON
ENDIF
PAUSE wait_1              'Pause with new pot value
PORTC = \$0                'Turn OFF all outputs

GOTO Pots```
This routine sets the pot to a plus/minus from center position for direction and speed value. Change the A/D to 10 bit for better resolution.
It doesn't have dead band gap in center for the OFF position but you can change that.
Then instead of using the A/D value for the "PAUSE wait_1" control, use it to adjust your PWM signal.

3. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Soooo... I'm leaning towards ditching the dead-band and throwing in a DPDT slide switch on the board edge. The switch determines motor direction and that leaves me with PWM motor speed and Soft Start/Stop. To get up and going, I'm going to put a resistor/capacitor timer between the pot and the A/D pin. Power to the pot will be through a foot switch. When power is supplied, the capacitor charges through the pot position and resistor. When power is removed, the capacitive charge drains back through the resistor and pot. That gives me soft start/stop. I'll get that going first then move to writing code to eliminate the RC circuit.

Any thoughts, suggestions, wise-cracks?

Ross

4. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Your method simplifies it enough to get it started, especially after a long break.
I'm guilty of diving fingers first with "intentional coding" then get stuck with paralysis of analysis.

5. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Amen to that brother!

6. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Since you have the software, why would you put extra hardware for the soft start part?

Consider a loop with maybe a for-next to increase the PWM to the set point gradually.

Ioannis

7. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Loannis,

Thank you for your confidence! I definitely want to do just that! But alas, I am a lowly mechanical engineer that dabbles in this stuff once a century and must admit my limitations. I'll be lucky to write code that uses PWM... and works! Every time I get back to this stuff I feel like I'm starting completely over. (...what the hell is a TRIS???) I have to dust off my PicStart Plus programmer, remember how to make a board with a laser printer and find that dang green programming book. So I bow to the genius' of this forum for grace and understanding.

I will be starting with the BLINK program!

If I am successful, you shall be the first to know!

Thank you!

Ross

8. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Well, OK.

This is just a as simple example as can it get, to help you start to experiment.

Say your PWM variable to output is my_pwm and the range of values is 0-255:

Code:
```my_pwm=0

for i =0 to 255
my_pwm=my_pwm+1
hpwm channel, my_pwm, frequency
pause 1
next i```
Now you have a pwm signal ramping up to 100% in 2,55 seconds.

Ioannis

9. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Thank you Ioannis!

I've ordered parts for the circuit board. Should be complete and ready to program mid week. I'll be back when I start writing code.

Thanks all!

Ross

10. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Can I get some help please looking this over.

12f675 pwm motor speed control

pot connected at GPIO.4 (AN.3)

Output to the motor works. PWM works. Now trying to read pot. Not working.

I checked between wiper and leg of pot and I'm getting wierd voltage. I don't think I have the presets right. It is acting like my port is conducting 5volts when it should be reading an analog voltage in.

Any help would be appreciated. Thank you!

Ross
Code:
``` Define OSCCAL_1K 1  ' Calibrate internal oscillator

MOTOR VAR GPIO.2  ' Alias GPIO.2 to MOTOR
DUTY VAR Byte

DEFINE  ADC_BITS        10       ' Set number of bits in result
DEFINE  ADC_CLOCK       3       ' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    50      ' Set sampling time in uS

TRISIO = %000100     ' GPIO 0,1,3,4,5 ARE INPUTS, 2 IS OUTPUT (MOTOR)
ANSEL = %00001000   ' Set AN3 analog, rest digital
ADCON0 = %10001100  ' Select AN3, Right justify result
CMCON = 7           ' Analog comparators off

pAUSE 500

main:
PWM MOTOR,duty,20
GOTO MAIN
END```

11. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

TRISIO = %000100 ' GPIO 0,1,3,4,5 ARE INPUTS, 2 IS OUTPUT (MOTOR)// not really , quite the opposite

you need to make gpio.4 an input before you can use it as analog
have another read of the data sheet

12. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Yep... my bad. I was confused because the motor on GPIO.2 was working. The PWM statement was toggling the port to output. I will change the TRIS statement to correct it.

Can I get clarification on two other lines of code? I have trouble reading the data sheets. I am using AN3 on the chip. Is the following line correct?

ADCON0 = %10001100 ' Select AN3, Right justify result
Also... AN3 is on port GPIO.4. The Picbasic Pro manual's example uses the term "channel" instead of "port". I don't know if these terms are interchangeable as I have seen both used. When I read "channel" I think channel "AN3" which is on port "GPI0.4". Which of the following statements are correct please? I could use some clarification on the terminology.

I appreciate this community so very much! The support I have gotten here has really meant a lot to me!

Thank you!

Ross

13. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

You have to assign the result to a variable:

Pot VAR BYTE

MPGMIKE,

Thank you,

Ross

15. ## Re: PWM Motor Control with Soft Start, Soft Stop Example Please

Update:

I got the pwm motor control working:

Code:
```   Define OSCCAL_1K 1  ' Calibrate internal oscillator

MOTOR VAR GPIO.2  ' Alias GPIO.2 to MOTOR
LOW MOTOR         ' Make sure motor is off when powered up
DUTY VAR Byte

DEFINE ADC_BITS 10          ' Set number of bits in result
DEFINE ADC_CLOCK 3          ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50      ' Set sampling time in uS

TRISIO = %111011     ' GPIO 0,1,3,4,5 ARE INPUTS, 2 IS OUTPUT (MOTOR)
ANSEL = %00111000   ' Set AN3 analog, rest digital
ADCON0 = %10000001  ' Right justify result and enable A/D
CMCON = 7           ' Analog comparators off

pAUSE 500

main:
PWM MOTOR,duty,20
GOTO MAIN
END```
Thank you for everyone's help!

Ross