PWM duty cycle explanation

Jean-Yves

New Member
Hello,
David Lincoln, author of book "Programming and customizing Picaxe" (second edition) describes how work PWM command.
Page 161, a program for controlling motor speed and direction use a formula for determinate duty cycle in relation with the value of potentiometer. I would understand equation in line:
pwmonper = avalue - revmin*21/10 MAX 1023.
Why *21/10 ?
Congratulations.
Jean-Yves
 

srnet

Senior Member
Assuming this is a DC motor, there is no specific relationship between PWM duty and motor speed, each type of motor would be different.

Maybe the 21/10 is a constant for that specific motor ?
 

Goeytex

Senior Member
The formula cannot be properly analyzed without all of the variables or givens.
Please include the missing code lines for pwmout, readadc & revmin.
 

eclectic

Moderator
The circuit and code can be viewed (legally I assume)
by visiting Amazon.co.uk
finding the book
then "looking inside"

I won't print it here for very obvious reasons.

e
 

Buzby

Senior Member
Well I never knew that !.

The 'look inside' only shows a few pages, but the 'search inside' accessed a lot more, maybe all of them.

Thanks e !.

Cheers,

Buzby
 

Buzby

Senior Member
I got 13 results for PWM search.

One difference might be that I'm logged in.

EDIT : 13 results, but not all clickable. The page we need is there, it's page 161.
 

nick12ab

Senior Member
I got 13 results for PWM search.

One difference might be that I'm logged in.

EDIT : 13 results, but not all clickable. The page we need is there, it's page 161.
Logging in didn't change anything. It depended on which amazon site I used - i tried another and got more pages but there were a lot of pages missing after 161 so if there's any more description of the code on the following pages I can't see it.
 

Jean-Yves

New Member
Hello,
Thanks for your posts. I have buy the "paper" book. It's excellent for beginners.

It's OK for me to understand all lines of program except the part of line
pwmonper calculate period coefficient *21/10.
This is program from book of David Lincoln. He use 08M and classic L293D.
Thanks for any contribution.
‘Controlling motor speed and direction with a potentiometer
#picaxe08m
Symbol fwdmin = 492 ‘analog value for minimum forward speed
Symbol revmin = 532 ‘analog value for minimum reverse speed
Symbol aport = 4 ‘define port for potentiometer input
Symbol pwmport= 2 ‘define port for pulse stream output

Symbol avalue = w 0 ‘define port for analog reading
Symbol pwmonper = w1 ‘define port pwm dutycycle

Output 0 ‘set pin 0 as output
Output1 ‘set pin 1 as output

Do
Readadc10 aport, avalue ‘read potentiometer position
Select avalue
Case 0 to fwdmin
Pins =$02 ‘set motor direction forward
Pwmonper = fwdmin – avalue*21/10 MAX 1023
Case revmin to 1023
Pins =$01 ‘set motor direction reverse
Pwmonper = avalue- revmin*21/10 MAX 1023

End select
Pwmout,pwmport,255,pwmonper
Loop
 

hippy

Ex-Staff (retired)
The project appears to use a pot like a balance control. At 'the top' the motors are off, as turned left it moves forwards, right it moves backwards, It goes faster the further it is turned.

The general principle seems to be that it translates the raw pot input, 0-1023

into two bands, 0-511, 512-1023

then fiddles these to give duty for speeds of : 1023-0, 0-1023

The code has a dead-band for the top so is a little more complicated. The 21/10 is 2.1 and presumably to convert whatever the range of values (0-492) to (0-1023) so max extent of the pot gives max speed.

The calculation should be -

maxspeed = maxpot * K

K = maxspeed / maxpot

K = 1023 / 492

K = 2.0792682926829268292682926829268

Rounded up

K = 2.1

For PICAXE integer maths -

K = 21/10
 

Jean-Yves

New Member
Hello hippy,
It's exactly how works motor with a dead-zone Stop. For completing your explanation of the conversion can you describe some numeric sample of pot value into the both equations. I am disturbed with negative number obtained. Picaxe maths not accept normally negative numbers.
 

hippy

Ex-Staff (retired)
The easiest way to see how things work is to run the code under the simulator.

There are no negative values but maybe I confused things; 0-511 meant 0 to 511.
 

Jean-Yves

New Member
Hello hippy,
Thanks for precision, but it is always not clear for me. Give me some real numeric samples of potentiometer value and result calculations in "two bands " for obtain speed increasing forward or reverse off limits fwdmin and revmin.
My brain is probably tired.
 

PieM

Senior Member
Hello hippy,
Thanks for precision, but it is always not clear for me. Give me some real numeric samples of potentiometer value and result calculations in "two bands " for obtain speed increasing forward or reverse off limits fwdmin and revmin.
My brain is probably tired.

avalue = 0 à 1023
dans le cas où avalue est < fwmin
alors moteur en avant
avec une vitesse égale à (fwmin - avalue)* 2.1 limitée à 1023

dans le cas où revmin<avalue <1023
alors moteur en arriere
avec une vitesse égale à (avalue- revmin)* 2.1 limitée à 1023

(fwmin - avalue) et (avalue- revmin) sont toujours > 0

Sorry for my french ! :rolleyes:
 

Jean-Yves

New Member
Hello Piem,
Bingo! Your development give me the solution. Picaxe mathematic formula not use ( ) for separate function soustraction and multiplication into the equation.
Thanks Piem and Picaxe Forum community.
Jean-Yves
 
Top