PWM Help

Code tips and tricks for beginners
Post Reply
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

PWM Help

Post by f1test »

I need PWM signal with 4 Hz.

My code is:
"
OPTION.PWMFREQ(4)
pwm(15)=100
while 1
a=a+1

wlog a
pause 500
wend
"
My multimeter show 100 Hz. the oscilloscope as well

I can reach frequencies higher than 100 hz but not lower. Does anyone know why?
User avatar
cicciocb
Site Admin
Posts: 1900
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1270 times
Contact:

Re: PWM Help

Post by cicciocb »

It looks that you are using the ESP8266.

The PWM lower frequency is limited at 100 Hz

https://arduino-esp8266.readthedocs.io/ ... log-output
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

Re: PWM Help

Post by f1test »

Yes is 8266.

I think the limitation is not hardware. In micropython I managed to go down to 4 Hz
"
import machine

p4 = machine.Pin(4)

pwm4 = machine.PWM(p4)

pwm4.freq(4)

pwm4.duty(50)

"
I even tested 1Hz, it works correctly.
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

Re: PWM Help

Post by f1test »

I did another test in Arduino. And here the frequency can be reduced below 100 Hz.

'
const int ledPin = 4;

void setup() {
analogWriteFreq(4);
analogWrite(ledPin, 17);
}

void loop() {


}
Maybe these languages ​​use other tricks to lower the frequency below 100 Hz.
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: PWM Help

Post by BeanieBots »

I've just tried:-
PIN.TONE pin, freq [,duration]
and it goes down to 1Hz.

Another option might be to use a timer and set the period if you want very low frequencies.
Not sure how stable that would be if you are also using WIFI and/or interrupts.
I guess it really depends what you need the pulses for.
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

Re: PWM Help

Post by f1test »

The example proves that the hardware can go down to 1 Hz.
Pin.Tone generates a signal without the possibility of changing the duty cycle.
I will try with Timer0.

Thank
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

Re: PWM Help

Post by f1test »

This is my attempt. Use Timer0 and Timer1.
If you can, please improve it
" pin.mode 4 , output
timer_0=1
frecventa = 4
duty=0.2
perioada = 1000/frecventa
timp_calc = perioada * duty
timp_ramas = perioada-timp_calc
wlog timp_calc
wlog timp_ramas

timer0 timp_calc, Led_Off

wait


Led_Off:

pin(4)=0
timer0 0
timer1 timp_ramas, led_on
return

Led_on:

pin(4)=1
timer0 timp_calc, Led_Off
timer1 0
return
"

Changing the value of the Duty variable from 0.01 to 0.99 can be used to change the load factor.
Changing the value of the frecventa variable changes the PWM frequency
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: PWM Help

Post by BeanieBots »

Something like this should get you close.
You might need to fiddle with the numbers a little to compensate for errors in the timer period and code execution time.

Code: [Local Link Removed for Guests]


MyPin = 2
Pin.mode MyPin, output
Duty = 50       'Set your desired % duty cycle
Frequency = 4   'Set your desired frequency

Period = 1/Frequency * 1000
OnTime = Period * Duty / 100

Timer0 Period, Doit

wait

Doit:
 Pin(MyPin) = 1
 Pause OnTime
 Pin(MyPin)=0
Return

Post Reply