sab201
sab201

Reputation: 11

Pi Pico PWM function and toggle() pin function

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.

Thanks

I have made edit to include the Micropython code for switching the mosfet module.

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.

Upvotes: 1

Views: 213

Answers (1)

Jon Nordby
Jon Nordby

Reputation: 6289

PWM is generally best done by a dedicated hardware peripheral, not in software. At frequencies under 42 Hz a software-based solution is probably viable though. Note that you need to avoid triggering MicroPython GC pauses in the loop. And you should really measure the actual frequency with an oscillope, since there may be timing issues introduced by the software statements - that you may need to compensate for. It would become much more complicated if the device is to do other things at the same time. Or if the desired frequency is higher.

The only benefit of a software based solution is that it works on pins/chips that does not have PWM functionality in hardware.

Upvotes: 0

Related Questions