PWM and Toggle function on MCUs

Thread Starter

sab201

Joined Nov 18, 2023
297
I am trying to learn to program MCUs for driving this MOSFET module.
5-36v-switch-drive-high-power-mosfet-trigger-module-2-400x400 (1).jpeg
https://www.electronicscomp.com/5-3...P-f7TWdUcEZBANgFEG1Ekoog8GPirvJwaAuLVEALw_wcB

I am now trying to program a rasberry Pi Pico with Micropython.

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. 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.

I like to know for this application, using the toggle function to switch ON and OFF that is using digital signals to drive the MOSFET would be a good idea or should I use the pwm function from the Pi Pico. What difference would it make performance wise. Do I need additional components like DAC or something to filter out noise.

Thanks.
 

geekoftheweek

Joined Oct 6, 2013
1,429
I have no experience with PI Pico so far so my response is more or less for microcontrollers in general.

The first thing to consider is how precise your timing requirement is. Hardware peripherals are always going to be better than software solutions from a timing standpoint. A software PWM will almost never be the same cycle times as the program will more or less never be on the right line at the exact moment it needs to in order to toggle the pin due to how programs work.

You may be able to get the timing correct within a few instruction cycles by using a timer and interrupt to call the toggle() function, but then you have other considerations to account for. Are there other interrupts being used that will potentially already be serviced when the timer interrupts which will delay the timer interrupt code? Are there higher priority interrupts that will cause the program to temporarily jump out of the timer interrupt?

More or less if you need precise timing control than the hardware PWM is the best answer. If you can handle a measureable variation in timing than the software toggle() will work.

A DAC won't filter noise. Capacitors are usually the answer there or inductors or a combination of the two. It all depends on what kind of noise you are dealing with.
 

Thread Starter

sab201

Joined Nov 18, 2023
297
A DAC won't filter noise. Capacitors are usually the answer there or inductors or a combination of the two. It all depends on what kind of noise you are dealing with.
Yes I know DAC wouldn't filter out noise, I wanted to know if DAC could be added to improve performance and any other type of device can be used to filter out noise.

Thanks for the detailed response. So I understand that if there are so much tasks involved in the program the software pwm wouldn't be precise. Right now I am using ne555 timers made on perf boards and capacitors and resistors for the pwm. I thought they could be replaced by microcontrollers so I chose a pi pico since that has a faster processor so the timing would be more precise.

As far as the program goes I only use 7 push buttons to trigger each pwm frequency output from the MCU. Each time a different push button is pressed that corresponding frequency will be used to switch the mosfet module. So the program is not complex and it does not have any other tasks other than generating frequency timing for the MOSFET.
 

Thread Starter

sab201

Joined Nov 18, 2023
297
Give us an idea of what this project is about, there are tricks and traps when driving electromagnets...
sketch-1719332832505.jpgThis is the basic circuit. I am making a dc electromagnet powered by a buck converter for healing purposes that takes pulsed current at different frequencies. I want to select different frequencies of the pulsed current using push buttons. I made analog pwm generators and switching them using radio push button circuit I already discussed this in another thread.

Now I want to use a microcontroller in place of the 555 Square wave generators and replace the radio push button circuit. I am learning to program it and I will be trying it out shortly but I am not sure how a digital pwm signal switch the MOSFET. Right now I am using analog signal to control the MOSFET. But with an MCU, i will have digital output signal so will the MOSFET switch correctly this is my doubt here.
 

geekoftheweek

Joined Oct 6, 2013
1,429
So I understand that if there are so much tasks involved in the program the software pwm wouldn't be precise.
That is pretty much the point I was trying to make. Whether or not it is an actual problem with your particular circuit or not depends on what your definition of acceptable variations are.

Judging by your description of what is happening I would say a software method would be good enough. I can't imagine the program getting so complex that the timing would get outside of a reasonable margin of error. A magnet will also provide an averaging effect that will "smooth out" imperfections in timing as long as the variations are within reason.
 

Thread Starter

sab201

Joined Nov 18, 2023
297
Judging by your description of what is happening I would say a software method would be good enough. I can't imagine the program getting so complex that the timing would get outside of a reasonable margin of error. A magnet will also provide an averaging effect that will "smooth out" imperfections in timing as long as the variations are within reason.
Ok Thanks for the information. The MCU will be generating pwm one at a time without doing any other tasks so this can be used.

I also did some searching and found that for low power applications DAC is not needed.

Now only thing I like to know is the outcome of using toggle() function and the pwm function of the Pi Pico mcu what difference it makes. I have to ask that in a programming forum.
 
Top