Hello, I've posted about this before, and you all have given great feedback. Just to recap, I have a load cell hooked up to a strain meter, which is then output in to the arduino. I have 8 pounds applied to the load cell and that equals .80 volts read into the analog output pin. Of course, when I read it on the serial monitor, it isn't a constant .80 volts as it fluctuates because of noise. I have tried to average the serial monitor to get a better read to as close to .80 volts as I can get. Here is part of the code that I'm working with right now:
int sensorValue = analogRead(sensorPin);
float sum = 0;
for(int i=0; i< 5; i++)
{
sum = sum + sensorValue;
delay(1000);
}
float average = sum / 5.0 ;
float voltage = average * 5.0 / 1023;
Serial.println(voltage, 3);
I changed the delay 3 times (300ms 1sec and 1 min) and then took a sample size of about 200 from the serial monitor and posted it on an excel spreadsheet to take the average. The average was always around .80V which is what I want, but the max and min are not close to .80V. There really is not difference between the 3 time intervals I've done. The max - min of each sample of each time interval is about .40V. It seems like the serial monitor is just printing out the voltage it reads after the delay instead of taking the average. I figured that if I took the average, the number it prints should be roughly the same each time. After having it average for a minute, there would be times where it would print .835V and then .795V (rough estimate). Is my code not really taking the average? Thank you for taking the time to read this.
So all I have to do is have my code look like this?
int sensorValue = analogRead(sensorPin);
long sum = 0;
float voltage = sensorValue * (5.0 / 1023.0);
for(int i=0; i< 5; i++)
{
sum = sum + sensorValue;
float voltage = sum / 1023;
delay(300);
}
Serial.println(voltage, 3);
I apologize for my ignorance. I'm new to the arduino scripts. So what exactly is happening with this script. Is it reading it 5 times and taking the average? If I change i<5 to i<100 will it average 100 samples?
ekraft:
So all I have to do is have my code look like this?
int sensorValue = analogRead(sensorPin);
long sum = 0;
float voltage = sensorValue * (5.0 / 1023.0);
for(int i=0; i< 5; i++)
{
sum = sum + sensorValue;
float voltage = sum / 1023;
delay(300);
}
Serial.println(voltage, 3);
I apologize for my ignorance. I'm new to the arduino scripts. So what exactly is happening with this script. Is it reading it 5 times and taking the average? If I change i<5 to i<100 will it average 100 samples?
No, you only read once when you set the value of sensorValue to the return value of analogRead() in the line: int sensorValue = analogRead(sensorPin); You need to actually use the analogRead() function inside your for loop.
Go back to your first code (in this thread) and replace the line: sum = sum + sensorValue; with: sum = sum + analogRead(sensorPin); Unless you need the value of that analogRead() for something else you can entirely get rid of the variable sensorValue.
Also, 5 data points isn't really statistically significant. Reduce (or remove, as the analogRead() function takes several milliseconds) your delay and play with varying the number of samples you take per average. (Change the 5 in the for loop and the 5 you divide with for the average to a common variable. Then for your different test runs you only need to make one change where you set the variable instead of two changes.) The more samples the better; up to the point that you are spending too much time collecting data without giving a result.
I am not sure what you mean by "statistically significant" - I suspect you are just throwing that term around. The confidence limits around the mean can be calculated as:
Mean +/- ts/sqr(n)
where t is a constant based on n and your confidence level, s is the standard deviation, and n is the number of points.
Ignoring s for the moment, if n = 1, than the 95% confidence limits are mean +/- s12.7
At n = 5, we have mean +/- s1.15, an order of magnitude better
At n = 100, we have mean +/- s*0.2, only about 6x better than 5 samples.
The takeaway is that even 5 samples dramatically improves your estimate of the true mean.
even 5 samples dramatically improves your estimate of the true mean
But nowhere near as much as 100 samples. Obviously it is a case of the more samples the better in the time available to take them, albeit it is a case of diminishing returns.
Not necessarily, it depends on the standard deviation. If the noise is only 1-2 LSB's then there is probably not much to be gained by going to 100 samples, unless you are measuring a really low level.