So what is the signal level coming from that current sensor?
On the Arduino you can set the ADC to use the internal 1.1V reference, making it a lot more sensitive (and probably more stable).
anishkgt:
int ReadCurrent()
{
float iSense;
int i;
for (i = 0; i < ISamples; i++)
{
reading = analogRead(A2);
}
iSense = reading / ISamples; // get average value
iSense = ((iSense * 2.76) / 1023);
iSense = (iSense / 0.00003);
return (iSense);
}
No i noticed that that increasing the sampling rate would change the reading but which one would be more true/precise or near to precise. Also varying the weld time does not have any effect on the weld current as seen here.
Would anyone know why ?
There's a lot wrong with your code.
- variables types: many are signed while they can never become negative. Make them unsigned.
- you always use int while often you can use byte (as they don't get that large), such as for the pin numbers and ISamples.
- reading is global - why? Make that local. There are probably more such globals that should be local.
Then the snippet where you read the current. So many issues I'll just correct them and add comments in the code itself:
float ReadCurrent() // You actually return a float (iSense), not an int.
{
float iSense; // Here this float gets declared.
unsigned int reading; // local to this function, and the proper type: the maximum possible is ISamples*1024 = just over 50,000. Doesn't fit in a (signed) int.
for (byte i = 0; i < ISamples; i++) // a byte is big enough for i, and it can be local to this loop.
{
reading += analogRead(A2); // You surely want to add up the readings!
}
iSense = (float)reading / (float)ISamples; // get average value - cast to float or you get an int as result, losing a lot of precision.
iSense = ((iSense * 2.76) / 1023);
iSense = (iSense / 0.00003);
return (iSense);
}
But it still won't work. Every analogRead takes about 100 us. Your period lasts 20 ms, so that would require 200 samples to read a complete period and get a single wave (obviously you have to change the type of reading to store the sum of 200 samples to unsigned long).
However when you measure a complete wave, and take the average, what you get is of course the offset value, not the peak to peak value of the wave!
What you really want to know is the peaks of the wave: the highest point, and the lowest point, and then the difference between the two. That number varies with the actual wave. The way to accomplish that is to sample a few waves, record the highest and lowest values you got, and use that to calculate the current.
float ReadCurrent()
{
unsigned int maxReading = 0;
unsigned int minReadnig = 1024;
for (unsigned int i = 0; i < 1000; i++) // This should sample just over 5 complete waves.
{
unsigned int reading = analogRead(A2);
if (reading < minReading) minReading = reading;
if (reading > maxReading) maxReading = reading;
}
float iSense;
unsigned int readingDifference = maxReading - minReading;
// calculate current with the difference between the peaks.
return (iSense);
}