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?
PWM Help
-
- Site Admin
- Posts: 1322
- Joined: Mon Feb 03, 2020 1:15 pm
- Location: Toulouse
- Has thanked: 277 times
- Been thanked: 903 times
- Contact:
Re: PWM Help
It looks that you are using the ESP8266.
The PWM lower frequency is limited at 100 Hz
https://arduino-esp8266.readthedocs.io/ ... log-output
The PWM lower frequency is limited at 100 Hz
https://arduino-esp8266.readthedocs.io/ ... log-output
Re: PWM Help
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.
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.
Re: PWM Help
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.
'
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.
-
- Posts: 170
- Joined: Tue Jun 21, 2022 2:17 pm
- Location: South coast UK
- Has thanked: 104 times
- Been thanked: 65 times
Re: PWM Help
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.
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.
Re: PWM Help
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
Pin.Tone generates a signal without the possibility of changing the duty cycle.
I will try with Timer0.
Thank
Re: PWM Help
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
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
-
- Posts: 170
- Joined: Tue Jun 21, 2022 2:17 pm
- Location: South coast UK
- Has thanked: 104 times
- Been thanked: 65 times
Re: PWM Help
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.
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