Pi Pico PWM function and toggle() function #15640
Replies: 3 comments 13 replies
-
Should you desire any kind of accuracy, DO NOT use the Pin toggle mechanism. |
Beta Was this translation helpful? Give feedback.
-
don't forget there is always machine.Timer for setting up long period recurring events. A couple of timer tasks, one to turn the pin on and the other off, will run in the background until you tell them to stop. A not particularly great example is this: from machine import Pin, Timer
from time import sleep_ms
led = Pin("LED", Pin.OUT)
on_timer = Timer(-1)
off_timer = Timer(-1)
def blink_start(t1, t2, on, off):
t1.init(
period=on + off,
mode=Timer.PERIODIC,
callback=lambda t: led.on(),
)
sleep_ms(on) # wait to init off timer
t2.init(
period=on + off,
mode=Timer.PERIODIC,
callback=lambda t: led.off(),
)
while True:
on_time = int(input("Tiempo ON?"))
off_time = int(input("Tiempo OFF?"))
blink_start(on_timer, off_timer, on_time, off_time) (nicked from here) takes two millisecond values from user input and blinks the LED based on those values |
Beta Was this translation helpful? Give feedback.
-
Correct me if I'm wrong, but wouldn't that introduce quite a bit of jitter due to varying interrupt latency? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to write a program for the pi pico using Micropython to send pwm signals to a mosfet module to turn it on and off to send pulsed current through a DC electromagnet. The MOSFET switches power ON and OFF power to a DC electromagnet at frequencies 0.1, 7, 14, 21, 28, 35 and 42 Hz. I also plan to make it work on other higher frequencies. The load current to be switched will be around 4 to 4.5 Amps DC at 12 Volts DC. The MOSFET could switch ON at 3.3 Volts output from the Pi Pico Pins.
My doubt here is that - since the Pi Pico can produce a PWM output should I use that function in the program to control the MOSFET using PWM or I can use the toggle() pin command to switch ON and OFF a pin and use that to control the MOSFET. What difference will it make? I only need a 50 percent duty cycle for all frequencies and I will not be changing the duty cycle so the toggle() function works fine. But I do not know what is the difference in using the two methods. In toggle() method I can directly give the value in seconds to get the 50 percent duty cycle. By using pwm I have to set the desired frequency and then set the duty cycle by choosing a value between 0 to 65550.
I will be using push button switches to select one frequency at a time so the pi pico will only be doing one task at a time. I like to know which of these two methods would be efficient and what are the drawbacks.
I wrote a program and used the toggle() pin function. It works fine. Should I use the pwm function instead. Will it be more efficient. What is the difference.
This is the code
import machine
import utime
pin_external = machine.Pin(15, machine.Pin.OUT)
while True:
pin_external.toggle()
utime.sleep(5)
This program gives out a voltage of 3.3 Volts every 5 seconds ON state 5 seconds and OFF state 5 seconds at pin15. That is a 0.1 Hz frequency at 50 percent duty cycle. Same I can do for 7 Hz by giving a utime.sleep(0.0714285) in the last line of the code above replacing the utime.sleep(5).
Time, T = 1/f
Since duty cycle is 50 percent
On time = T/2 Off time = T/2
utime.sleep(T/2) is the number in the code.
Beta Was this translation helpful? Give feedback.
All reactions