I have built a power supply which monitors current and shuts down if a limit is exceeded. I am using an Arduino Micro for control, and using analogRead() to read a voltage which is proportional to the current.
The analog reference for this is a nice stable zener 4.3 v. diode. I have the current limit set to 0.5 A, and I am powering a resistor at 0.25 A. This will work fine for a few hours, then the system triggers a shutdown, which would imply that it measured 0.5 A. or more. This can't be, since the load is a nice docile, unchanging 50 watt resistor. The mathematics of the situation is such that a 0.25 A reading should give an analogRead output of 140, while at 0.5 A., iI should get 280. That is a large gap, and should preclude an error occurring.
The code for the shutdown is below.
data = analogRead(A3ReadPin); //compute and output average current
volts = zener*(float(data)/1023.0); //actual voltage on current read pin
current = (volts - 0.051)/2.208; //linear equation for volts --> current
if (current > maxAmps3) failFlag++; //current above preset limit
if (failFlag >= 2) //had two successive high readings ==> shutdown
{
digitalWrite(P3ShutoffPin, HIGH);
lcd.setCursor(0, 3);
lcd.print("P3 overcurrent");
for (i = 0; i < 3; i++) //alert routine
{
digitalWrite(BuzzerPin, HIGH);
delay(200);
digitalWrite(BuzzerPin, LOW);
delay(100);
}//for i...
} //if (current ...
Given three hours between false alarms, indicates that I have invoked analogRead about 216000 times between false alarms. Has anybody noticed any erratic but rare behavior from analogRead()?
Thanks in advance for any suggestions.
Can you post the schematic? I'm a little unsure how things are wired there
Also, please post the full code. Some questions that arise are:
Is the signal connecting to the 50W resistive load steady-state, PWM or other?
Is the 50W resistor wire-wound type? These have the poor high frequency properties due to their inductance.
In your code, is the failFlag reset anywhere? Are other analog inputs used?
As for the resistor, it is a big wirewound honker. The power source is not PWM, but steady state from an LM317 driving power transistors. I have checked the voltage actually being read by analogRead() by monitoring it with a max/min voltmeter. The voltage being read remains rock-steady, so it is indeed the reporting process that goes bad somehow. This system is using two analog read pins to monitor power supply current and voltage. Is there any reason to worry about using multiple pins for analogRead()?
Full sketch.
Full schematic.
Try reading the same analog input twice in a row keep only the second read.
You need to try to catch it in the act and find out what it's doing wrong when it fails IMO.
You should try to retain the analog read and all the interim values between that and the decision to shut down being made; possibly this will shed some light on the matter.
Further Followup
The voltage is 0.08 v., and is the output from a MAX 472 chip, which is a current amplifier used to measure the current being drawn from a regulated power supply comprising a MOSFET modulated by an LM317.
Both on an OScope and on a max/min voltmeter, the voltage looks very clean and steady, and at the moment of failure, I get a high (600 vs. 188) analog reading, while the max/min voltage reading shows no change.
I have tried moving the input analog signal away from any adjacent pin in use, and I use a 50 ms. delay between the readings, and I even used a bypass capacitor to ground on the pin. I tried, as suggested above, read twice, just keep the last. No help. I do not suspect the current source, since it has stayed steady even when the failure occurs. The time between failures can go anywhere from an hour to about 12 hours. I'm beginning to suspect some flaw in the microprocessor, but I don't have another identical one at the moment to plug in.
I do average my readings for display output, but the fact is that besides the running code with the analogRead() in it, displayed every cycle, I have also monitored both the voltage read by analogRead(), and the voltage supplied for ARef from a zener. Both of those are rock-steady at all times.
The most curious thing is that whenever this fails due to a bogus high reading, the output value of the analogRead() is 600-603, while normal reading is 184 - 188.
At this point, I think it must be something in the Micro itself.
This circuit is built up on a breadboard which is probably a jungle full of inductances and capacitances. But I really can't believe that that would become prominent in an almost entirely DC environment. I'm going to splurge on anther Micro, and see what happens. I'll revisit this when I find out.