Experiment changing PWM with pot

Gramps

Senior Member
This should be so simple but it will not work!
Mr. Lofton's code from years ago using a 18M2 part.
I changed to 28X2 part (and changed pin numbers, ofcourse)
Test results;
LED lights about half brightness but pot does not change brightness.

Code:
;Lewis Lofton's code at: https://www.youtube.com/watch?v=qY_kpREPTb4&pp=ygUQbGV3aXMgbG9mbGluIHB3bQ%3D%3D
;He used an 18M2 part

;The program below uses three commands. Pwmout sets up B.3 for a 1000Hz 50 percent duty cycle square wave. Readadc10 reads the position of a 5k or 10k control connected to B.4 and returns a value from 0-1023. Val must be a 16-bit "word" variable. The command "pwmduty" alters the duty cycle of the square wave from B.3 "on the fly" that is without turning it off and resetting the whole sequence. The higher the duty cycle the faster the  motor should run. (Or brighter the LED.)


#picaxe 28X2
#no_data
#no_table


symbol speed_control = B.2
symbol val = w0 ; word (16-bit) user variable
symbol pwmPin = C.1

init:
pwmout C.1, 199, 399

main:
readadc10 speed_control, val 

; read 10-bit ADC into variable w0
pwmduty pwmPin, val  ; set pwm duty
goto main        ; loop back to start
[code/]
 

hex

Active member
Interesting.....
readadc10 function does not seem to work if you use a symbol for pin selection.
Don't know why that would be.

Here is my attempt at working code:
Code:
;Lewis Lofton's code
;He used an 18M2 part

;The program below uses three commands.
;
; Pwmout sets up B.3 for a 1000Hz 50 percent duty cycle square wave.
;
; Readadc10 reads the position of a 5k or 10k control connected to B.4
;   and returns a value from 0-1023.
;   Val must be a 16-bit "word" variable.
;
; The command "pwmduty" alters the duty cycle of the square wave from B.3 "on the fly" that is
;   without turning it off and resetting the whole sequence.
;   The higher the duty cycle the faster the  motor should run. (Or brighter the LED.)

#picaxe 28X2
#no_data
#no_table

symbol speed_control = B.2
symbol val = w0                             ; word (16-bit) user variable
symbol pwmPin = C.1

init:
    pwmout pwmPin, 199, 399                 ; pin, period, duty

main:
    readadc10 B.2, val                      ; read 10-bit ADC into variable w0
                                         ; seems to be that using a symbol for pin does not work here
    
                ; adjust ADC value to suit chosen PWM duty range
                val = val * 7
                val = val / 10
                val = val + 0
                
                pwmduty pwmPin, val                     ; set pwm duty
                
                pause 100                               ; limit update rate
    goto main                               ; loop back to start
end
I notice that comment mentions B.3 for PWM out, but code uses C.1
 

kfjl

Member
Your program works for me:

Code:
#picaxe 08m2
#no_data

symbol speed_control = C.4
symbol val = w0 ; word (16-bit) user variable
symbol pwmPin = C.2

init:
pwmout pwmPin, 199, 399

main:
readadc10 speed_control, val

; read 10-bit ADC into variable w0
pwmduty pwmPin, val  ; set pwm duty
goto main        ; loop back to start
 

AllyCat

Senior Member
Hi,

With X2 parts, if you use a symbol in a READADC command, you must specify the ADC channel number, not the Port.pin number. Explained by technical in post #41 HERE.

For fault-finding issues like this, put a SERTXD(#val," ") immediately after the READADC to isolate the problem and then simplify the program until it works!

Cheers, Alan.
 

Gramps

Senior Member
Your program works for me:
kfil, is that because you are using an 08M2 and not a 28X2?
AllyCat said,
With X2 parts, if you use a symbol in a READADC command, you must specify the ADC channel number, not the Port.pin number.
 
Last edited:

hex

Active member
Thanks AllyCat

Seems a bit of a pain having to refer to datasheet to convert port number B.2 to analogue channel 8, but I just realised that I can hover over pin B.2 on the editor simulator diagram to get the information ADC8

Typing B.2 in the code window allows hover to give the ADC8 information too.

Maybe not so bad as it first seemed
 
Last edited:

AllyCat

Senior Member
Hi,
...when we run the SERTXD command, the "read out screen" is not appearing. Where did it go?
The Terminal Window might be "under" the PE6 Tab on the Task Bar.

It's usually opened with f8 , or Menu : PICAXE : Terminal , or by a #terminal 9600 (or 4800 for M2 chips) at the top of the program.

Cheers, Alan.
 

hex

Active member
Hex, I'm not sure what this code does. The program runs fine with out it.
ADC returns values of 0 to 1023 decimal (if full range used)
PWM accepts values 0 to 799 (as coded if I understand correctly)
Without the adjustment, higher ADC values are out of range for PWM and thus have no effect on output

In particular - third line of adjustment code (val = val + 0) does absolutely nothing. It is just a place-holder for a possible minimum value
for example - a servo may work with PWM values 30 to 750 for full sweep. feeding the servo with values of 0 or 1023 might cause the servo to "rattle"

If you are not using a servo, and you are happy that your pot will have no effect for the top 20% (approximately) of it's travel, then delete this part of my code. This bit of code was for my satisfaction only. You do not need to use it

Not sure if I am being sufficiently clear, but I hope it helps anyway :)
 

Gramps

Senior Member
If you are not using a servo,
Thank you for your well thought out explanation.
We are using a PN00218-CYT7 Cytron 20A 6-30V DC Motor Driver MD20A and the program works perfectly without the additional code
 
Top