I will try and explain this the best I can, I have a project where I need to alter the current using PWM (battery tester), where I have a preset value of say 3amps it adjust the sensor value and when the battery voltage fall I need to decrease the sensor value to maintain the 3amps but having a little issue
If I try this code, The current readings start to increase until the 3amps is reached but the readings become unstable jumping from 2.75amps to 3.2amps and the sensor value fluctuates between 232-235
if ((!cutoff_voltage_reached) && (Vresults1<= current_draw)){
// decrease duty cycle to compensate for smaller load
sensorValue--;
if (sensorValue < 195){ //set minimum current value 2.50amps
sensorValue = 195; //set minimum current value
}
}
else if ((!cutoff_voltage_reached) && (Vresults1>=current_draw))
// increase duty cycle to compensate for larger load
sensorValue++;
if (sensorValue >255){ //set maximum current value 3.0amps
sensorValue = 255; //set maximum current value
}
pwmWrite(9,sensorValue);
To combat this which seems to work where the current increase's to 3amps the reading is stable and the sensor value does not fluctuate it holds at 232,if the current passes 3amps the sensor
value does not increase like it does above, I think this is because of this delta =abs(last_current - Vresults1);
delta =abs(last_current - Vresults1); // calculate the absolute value of the difference between previous and current light value
if (delta<Vresults1){ // only adjust duty if current falls
if ((!cutoff_voltage_reached) && (Vresults1<= current_draw))//{
// decrease duty cycle to compensate for smaller load
sensorValue--;
if (sensorValue < 195){ //set minimum current value
sensorValue = 195; //set minimum current value
}
}
else if ((!cutoff_voltage_reached) && (Vresults1>=current_draw))
// increase duty cycle to compensate for larger load
sensorValue++;
if (sensorValue >255){ //set maximum current value
sensorValue = 255; //set maximum current value
}
pwmWrite(9,sensorValue);
last_current = Vresults1;
How could I write the code to obtain smooth readings and if the current increases or decreases the PWM(sensor_value) if I increase/decrease my input voltage(battery voltage),
I only need to adjust the current if it falls above or below my preset value if the input voltage is increased or decreased which happens over a period of time, I've look at P.I.D but I don't it is suited using PWM
Why not use a basic LM317 with a current shunt....simple and accurate.
I assume you realize what PWM is, right? It's not "smooth" and it's not "analog". You might be able to get something useful by with [u]smoothing/averaging[/u], but even that will be tricky unless it's synchronized with the PWM. And for 8-bit PWM, I assume you need at least a 256-element smoothing array/buffer.
Note that you'd be smoothing the readings, not the actual PWM.
bluejets:
Why not use a basic LM317 with a current shunt....simple and accurate.
Later on I will need to increase the current which the LM317 will not be able to handle
DVDdoug:
I assume you realize what PWM is, right? It's not "smooth" and it's not "analog". You might be able to get something useful by with [u]smoothing/averaging[/u], but even that will be tricky unless it's synchronized with the PWM. And for 8-bit PWM, I assume you need at least a 256-element smoothing array/buffer.
Note that you'd be smoothing the readings, not the actual PWM.
I do understand what PWM is, I'm using 2 Mosfet's with a mosfet driver plus 2 .47R 90W resistors and 60A 60Mv shunt to measure the current in the negative line with a circuit that monitors the current going in and out the battery which always gives a positive voltage out regardless which way the current flows and an output to show +/- depending which way the current flows, it show's a - while current is been drawn and a plus while the current is been put into the battery, This saves me having 2 current sensors, When I used a PIC using this code and same current sense circuit with 10bit PWM(in basic)
CSEN1 = AD_RESULT1
If PWM_INPUT1 = 1 Then
If CSEN1 < MINDUTY1 Then ' don't allow greater than max duty 20.00amps
AD_RESULT1 = CSEN1
Dec duty2
If duty2 < 195 Then
duty2 = 195
EndIf
ElseIf CSEN1 > MAXDUTY1 Then ' don't allow below minimum duty 0.0amps
Inc duty2
If duty2 > 255 Then
duty2 = 255
EndIf
EndIf
ElseIf PWM_INPUT1 = 0 Then
duty2 = 255' turn off the mosfets
EndIf
The readings remained +/- .25amps and fairly stable which was acceptable and smoother than using Arduino, I just wanted to port the project over to Arduino, If I set the sensor value manually say 232 the readings are stable on the LCD where I'm using an ADS115 (16bit) to measure the voltage/current but when the voltage falls so does the current and when I increase the voltage I need the current to be reduced. So I know it's not the PWM causing the issue, I think it's down to the way I'm controlling the current, My idea behind this project is to test the battery and charger with it with limited current set by me.
I want to be able to set a threshold say 3amps(example) and it maintains the 3 amps if the voltage increases/decreases so like a constant current load. I've used PWM because they run cooler rather than been run in the linear mode like some constant current loads are control via a DAC, I found this method produces more heat in linear mode where the power resistors handle the heat in my design
After some more playing and studying both codes, I found out where I went wrong I did not set a max current or min current, Now I have set them the current is reading spot on and now reading smooth on the display and actual current clamp meter.
if ((!cutoff_voltage_reached) && (Vresults1<= current_draw_max)){
// decrease duty cycle to compensate for smaller load
sensorValue--;
if (sensorValue < 195){ //set minimum current value
sensorValue = 195; //set minimum current value
}
}
else if ((!cutoff_voltage_reached) && (Vresults1>=current_draw_min))
// increase duty cycle to compensate for larger load
sensorValue++;
if (sensorValue >255){ //set maximum current value
sensorValue = 255; //set maximum current value
}
pwmWrite(9,sensorValue);
Steveiboy:
Later on I will need to increase the current which the LM317 will not be able to handle
That's why I said " LM317 with current shunt"....