PID control loop engage method (SOLVED)

Hi everyone.

I basically have a set of parallel mosfets connected to a mosfet driver which is in turn connected to pin 5 of an arduino nano. The pwm frequency of pin 5 has been reduced on purpose from its default frequency of around 980 Hz to 62 in order to keep the switching loses of the mosfets low due to a non ideal cooling setup.

The mosfets are basically used to connect a dummy resistor load to"burn" excess current in the circuit.

A PID loop is used along with a current sensor to control the current in the circuit. The mosfets need to engage only after a non-zero threshold current is circulating, and are currently controlled like this:

void loop()
blah blah
if (circuitCurrent > threshold)
blah blah

The problem is that the way the code is currently implemented, creates as a side effect a “bang-bang” controller which takes over the PID controller since the void loop frequency is some orders of magnitude higher than the pwm frequency of pin 5. This results in the mosfets heating up due to switching loses and defeats the whole purpose of the PID controller.

How would you go about solving this issue?

Thank you in advance for your time. Any support is greatly appreciated.

Since your system is acting like a "bang-bang" controller, I'd guess the problem is that your coefficients are not optimized for your requirements. I'd go further and say that I image your "P" value is way too large.

Can you post your whole code and some plots to show what exactly is happening with your system? A circuit diagram wouldn't hurt either.

If you really need PID control, you cannot call the PID function with a conditional as is being done. Called in that manner, it's no longer PID, it's just broken code.

Now, taking a step back, I think you're mis-stating what you're really trying to do. If you want to dissipate energy above a certain threshold that can be determined by this test:

  if (circuitCurrent > threshold)

and the mosfets can turn off when this test is false, I don't see why PID is needed.

You don't use a resistor to "burn" (your quotes) excess current in a circuit, you use a resistor to add current, the effect of which is to drop the voltage. If you're trying to control the voltage to a specific voltage, as in a shunt regulator, then yes, you want PID (well, most likely just P&I). But, if you only want to reduce the voltage to a range and you want that to happen as fast as it can, you want simple on-off control.

Ultimately, if you need PID control, you need to call the PID loop every pass through loop(). Lose the conditional call and then the PID loop, when properly tuned, would have zero output (no mosfet turn on) when there is no error term.

Hi everyone.

Hi there. Your word description is kind of difficult to follow. You use terminology like 'burn' excess current, which isn't usual terminology for electronics.

You also mention "The mosfets need to engage only after a non-zero threshold current is circulating". That's hard to follow as well. If the mosfets aren't 'engaged' or active, then there's not going to be any current flowing, right? But.... I guess this depends on what sort of circuit you're thinking of. Probably need to draw a circuit diagram or system diagram ..... so that the aim becomes clear.

I am basically building an electric skateboard according to the picture provided and i am using a power resistor array to limit the battery charging current to less than 1C and the voltage across it to less than 25.2V during regenerative braking.

The excess current should be diverted to the power resistor only if it exceeds the battery’s 1C charging rate or the voltage across the battery rises above 25.2V.

The code basically needs to:

react fast to protect the battery
be precise when “burning” power so as not to waste any
keep the mosfet array cool

Maybe a PID solution is not the way to go in this situation.

Maybe a PID solution is not the way to go in this situation.

I think that is an accurate statement. You could probably design the circuit better so that you ensure no excess current is drawn or excess voltage is applied. How to exactly design such a charging circuit is a little above my pay grade, but is quite possible and allows you to reliably cut out the PID controller/Arduino.

Bottom line: ask someone to help you redesign the circuit so that all precautions are handled with analog electronics.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Ops schematic

Can you please show all your components and label pins and components.
What part number is/are your MOSFETs?
Are they logic level type?

Thanks.. Tom.. :slight_smile:

Could we please start with some basic and very important information into the thread?

What is your intended maximum charge rate during heavy braking? 2C? 3C? Higher? What about your low charge rate? 1C?

Have you calculated what your load resistor value needs to be? Ohms and watts? What are you basing those values on? I'm not saying "show me your work", just curious how you sized things.

I'm asking because as mentioned by more than one poster, your terminology is incorrect. You need to control voltage, not current. The current that flows is a function of the voltage applied and the resistance of the load, basic Ohms law. While the battery isn't a resistor, you still set the charge current by adjusting the voltage applied.

Also, have you addressed the fact that the Arduino analog inputs are unipolar, not bipolar as needed for the current measurements in your application?

Thank you very much for the support guys.

In the end i ditched the whole PID idea altogether and implemented a bang-bang controller. It manages to keep both the charging current and voltage withing the required spec. The mosfets do indeed warm up due to the higher switching speeds but that's not a problem a bit of active cooling, some heating compound and a heatsink can't solve.

Have a great day everyone. Cheers.

you use a resistor to add current,

This is an interesting concept.

I have a lot of unused resistors. Can I use them to power my laptop?


This is an interesting concept.
I have a lot of unused resistors. Can I use them to power my laptop?

Agreed. I wanted to get the details on how much energy the OP was trying push back into the lipo during braking, the concern being that it would be very easy to exceed the safe recharge rate of the cells - if there was no mechanical braking and the the brake chopper load resistor was undersized. Further, he never mentioned cell balancing so again, another area of concern.

I suspect the OP doesn't really know what the system is capable of and therefore, he has no idea that he (or worse, his children) may riding around on a potentially explosive device.