presettable constant current pwm with feedback

I need help on programming or what approach do I have to make. I am designing a presettable constant current pwm controller which tracks the output's current of the load. In this case the arduino will have two (2) inputs to read, the preset analog potentiometer setting and the sampled feedback voltage (current from the shunt resistance of the load). An output will be from one of the PWMs (yes, I do need some mosfets or with optocoupler).

Initially, the pwm will gradually power up the load then it reads the analog voltage set by the potentiometer (the value corresponds to a certain ampere value in terms of steps e.g. 0-255). When the load current hits the preset value, the duty cycle of the pwm is regulated and keeps track of any changes to the load current via the feedback analog voltage (duty cycle is increased and decreased accordingly).

I appreciate any help on this.

-jun

Hi 007JT,

As all the other posters who may reply they will say you have given us very little to go on.
So little baby steps?

Do you have any idea of the PWM frequency that you want to use?
If 490Hz or 980Hz (pins 5 and 6 on the Uno) will do then analogWrite will work as is.
If not then you have some decisions to make:

  • Use a timer?
  • Change the prescaler frequency for for analogWrite (there will be side effects!)
  • PWM in software
  • and others.

PWM frequency?

Others may disagree (depends on your tolerance to noise) but the PWM frequency should be at least 4x the maximum step frequency if you are using a stepper.
For other loads if depends on the noise that the load generates (obviously a resistor or an LED will not generate noise) but a motor sure does. A 980Hz noise from my steppers nearly drove me mad!
DC motors will no doubt have similar problems.

Regards Alan0

Hi AlanO,

I'm not really concerned of what PWM frequency it will operate as long as it can regulate the load current with reference to the preset value (or that can be adjusted any time by the potentiometer at A0) to the load.

Yes, it is a CURRENT REGULATOR PWM circuit.

The load is purely resistive but varies its current draw (like a loaded motor)...the reason why Arduino needs to keep track with the changes.

I'm trying to make a flow chart/diagram but meeting some difficulty on where to start --
Here are the steps I wanted:

  1. initially, gradually increase PWM duty cycle to the load (within 5 seconds or so)
  2. read the preset value of A0 (potentiometer)
  3. if feedback current sense value reaches preset value, stop increasing the duty cycle
  4. loop back to step 2

I just need a guide to do the code.

thanks!

-jun

PID loop?

Hi 007JT,

It seems as if there is a PID library you can use:
http://playground.arduino.cc/Code/PIDLibrary

There is a worked example for reading an analog input to drive a PWM ouput:
http://playground.arduino.cc/Code/PIDLibaryBasicExample

Here is a pretty good tutorial.
http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

But note the comment from the first reference:
"Is it worth the extra work? If you're using a more basic system and it's working for you, there's no real reason to use PID."

So best of luck, regards Alan0

I usually make my own regulators but it boils down to a PID loop if you want to go that way.
Anyway, what you do is generate a ramp reference from your input pot, subtract the current feedback from the ramp reference value (with proper scaling of course) to get a current error, multiply the current error by a factor then use the result to set the PWM time ratio.

This will give you a straight proportional or "P" regulator which will always have an error since you need the error to get an output but you can make the error smaller by making the multiplier factor larger with the restriction that it will become unstable if you make the factor too large.

To eliminate any steady state error you add up the errors over time to get an integral of error then add the result (with a multiplier) to the proportional error then use the result to set the PWM time ratio. This will give you a "PI" regulator.

The "D" comes in when you calculate the rate of change of error then subtract this from the error. It is used mainly to help stabilize an unruly process but I've found that if you get the P and I factors right it is rarely needed for a simple voltage or current regulator

I've used this algorithm in the analog world for electrostatic dust precipitator current control many times and it worked very well.

With DC motors it becomes a bit more complicated and works better if you have a current rate regulator inner loop but nothing you've said implies that you need anything like that.

Thanks AlanO and phoxx!

I will try to learn this...

BTW can you clarify if you are wanting to regulate the average current or the current when
the PWM is on? Is the load inductive?

hi MarkT...the load is not inductive and not that sensitive. No need for precise current regulation :slight_smile:

You don't need a full PID for this - you just adjust the duty-cycle depending on the measured current
directly.

duty_cycle = wanted_average_current / measured_current ;