Go Down

Topic: Programming an almost linear graph (Read 5490 times) previous topic - next topic

Daniel Formosa

Dear all, after some tests done, I have generated this graph (attached). It is not linear but almost, but the problem is that I need to work in a very accurate way, so how can i program the arduino for such a graph? tnx

Grams   Voltage
0   1.20E-01
1035   3.80E-01
2035   5.80E-01
3035   8.40E-01
4035   1.08E+00
5035   1.34E+00
6035   1.58E+00
7035   1.84E+00
8035   2.08E+00
9035   2.34E+00
10035   2.54E+00
11035   2.80E+00
12035   3.04E+00
13035   3.30E+00
14035   3.54E+00
15035   3.80E+00
16035   4.00E+00
17035   4.26E+00
18035   4.50E+00
19035   4.62E+00

That is the table plotted for the graph



so how can i program the arduino for such a graph?

I think that would depend on what you want the Arduino to do, wouldn't it?
The art of getting good answers lies in asking good questions.

Daniel Formosa

The aim is this:
I have a gas cylinder (on my loadcell and opamp), the gas cylinder when full weighs 19kgs.
Now when i open the gas cylinder, gas gets out of the cylinder and enters a cars a/c

Therefore i need the arduino to notice when for example 400grams have escaped the cylinder gas. That is why i plotted the graph.



First, you should put the data in Excel, make the graph, then add a linear aproximation. That way, you can find out where exactly is the graph not linear. Use one or two decimal places for the aproximation as the Arduino won't like it too much if you use more than three.

Once you find the interval where the line is linear, get the equation and program it. Then the part that will most likely not be linear are the ends. Ok, imagine that 4 intervals in the beginning are not linear. You can make a linear aproximation inside the interval...

in pseudo code:

Code: [Select]

value = readADC(0);
if (value == 0 ||value < 0.12)
    final_val = Linearize (value, 0, 0.12);
   if (value >=0.12 || value < 0.38)
       final_val = Linearize (value, 0.12, 0.38);

See what I mean??
This... is a hobby.

Daniel Formosa

Hmmm i think i got a bit your idea, but this code Linearize (value, 0, 0.12) is it a library in arduino???


Daniel Formosa

The linear approx graph is attached, i didnt understand this code but Linearize (value, 0, 0.12)



grams = 17.299 volts^2 + 4068.3 volts - 488.45

R^2 = 0.9996

The linear fit is just as good though.  I think that this is about as good as your data would allow you to estimate.

The best linear fit that goes through [0.12, 0] is grams = 4129.4 volts - 495.53 with an R^2 of 0.9995

Daniel Formosa

Wow gardner you lost me.
R^2 - whats that?


R-squared is a correlation coefficient that measures how well the mathematical curve fits the actual data.

R-squared close to 1.0 means you have a really pretty close fit.

Daniel Formosa

Ok didn't do this in mathematics engineering....
I found some help from this site http://wwweng.uwyo.edu/classes/meref/Curve_Fitting.pdf
Did you work out r2 using excel?



a job for - http://arduino.cc/playground/Main/MultiMap
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Yes. All in Excel.  It's not SAS, but I have it handy.  I've done a bit of fiddling with your data and I have a couple of observations.
(1) I think your last data point [4.620,19035] is bogus
(2) I think that the underlying volts/gram relationship is linear to well within the accuracy of the measurements you've made, and
(3) I think that the source of the errors is A/D sampling error, specifically in the imperfect values in the A/D's resistance ladder.

The orange sawtooth series is the difference between the linear regression and the actual gram values.  I bet you a buck, that the saw tooths correlate with the switching of specific bits of the A/D value and that would be an indication of error in the resistor ladder network used by the A/D sampler.

The error curve does not have any simple shape that would indicate a correctable non-linearity in the volt/gram function.

Daniel Formosa

Hehe tnx gardner for the info :).  But I have not used an A/D converter. That is still to be used....
I am using a load cell; giving output voltage for the weigh on it.
The instruments used is an oscilloscope (Voltage peak+) and a very good power supply.  Even the weights 1kg are exactly 1kg since I made a spent some time putting exactly 1kg of sand.

And yes i think that the last point is bogus...
Ill hook up the equipment again and take other readings but I think it is quite a good almost linear graph.

Another question is this, when you wrote :
  grams = 17.299 volts^2 + 4068.3 volts - 488.45

How did you work that out??

With excel I managed to get this summary:

SUMMARY OUTPUT                        
Regression Statistics                        
Multiple R   0.999765814                     
R Square   0.999531683                     
Adjusted R Square   0.999505665                     
Standard Error   0.031847119                     
Observations   20                     
   df   SS   MS   F   Significance F         
Regression   1   38.9645237   38.9645237   38417.49623   2.01014E-31         
Residual   18   0.018256302   0.001014239               
Total   19   38.98278                  
   Coefficients   Standard Error   t Stat   P-value   Lower 95%   Upper 95%   Lower 95.0%   Upper 95.0%
Intercept   0.122532035   0.01375446   8.908531055   5.12634E-08   0.093634987   0.151429083   0.093634987   0.151429083
X Variable 1   0.000241939   1.23436E-06   196.0038169   2.01014E-31   0.000239346   0.000244533   0.000239346   0.000244533

Meaning that it is quite a good almost linear graph, but still I need to be accurate in this since I am refilling a cars a/c tank.

tnx again


Another question is this, when you wrote :
  grams = 17.299 volts^2 + 4068.3 volts - 488.45

How did you work that out??

There's a "display equation" property on the trendline in Excel.  Also a "display R-squared".  This is simply the quadratic that Excel fit.  Incidentally, that was with the last point included, and when I chuck that one out the curve it fits is different.

Even the weights 1kg are exactly 1kg since I made a spent some time putting exactly 1kg of sand.

I would look to errors in these weights then.  Do you have a bucket with 10 Kg, one with 5, etc?  The error chart looks like a difference in the weight of the 10 Kg bucket vs the 5 Kg + 3 Kg + 2 Kg.  At the points where you swap the small weights out for the one large one is where you get these sawtooths.  It really does look like quantization noise of some type, and quantization of your physical weights could be a factor.  The mean error is something like 50 grams.  Are your weights accurate to that precision for sure?   What instrument did you use to measure them?

Daniel Formosa

Clicking on my excel graph - treadline gives me this equation and not the one you gave me (which i think is better, since this is for the linear equation) y = 0.0002x + 0.1225

What I did was I got 20kgs of sand, and put 1kg, 1kg, 1kg ..... into seperated plastic bags, so I have 20 plastic bags each 1kg. and exactly 1kg since I used a good electronic weighing scale to measure them.

The error could be since the base on which placing these plastic bags wasnt that good. Look at the photos attached you will understand by what I mean I think

What is your recommandation?? Do I use the linear equation in my code??


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131