Go Down

Topic: AD22100 temp sensor arduino operation (Read 1 time) previous topic - next topic

grendle

hi all, didnt see this. thought id share it.
http://www.analog.com/en/other-products/militaryaerospace/ad22100/products/product.html

Code: [Select]
a = analogRead(A0);
   a= ((a*(5.0 / 1024)) - 1.375) / 0.0225;

robtillaart

removing slow divisions gives
Code: [Select]

raw = analogRead(A0);
float temp = raw * 0.217226044 - 61.1111111;
Rob Tillaart

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


robtillaart

As I don't have such a sensor, could you time 1000 samples to see the difference?

(this should work and produce 3 numbers)
Code: [Select]

volatile float raw;

void setup()
{
  Serial.begin(9600);

  // first time the analog read
  unsigned long start = millis();
  for (int i=0; i<1000; i++)
  {
    raw = analogRead(A0);
  }
  unsigned long stop = millis();
  Serial.println(stop - start);

  // time method 1
  start = millis();
  for (int i=0; i<1000; i++)
  {
    raw = analogRead(A0);
    raw = ((raw *(5.0 / 1024)) - 1.375) / 0.0225;
  }
  stop = millis();
  Serial.println(stop - start);

  //time method 2
  start = millis();
  for (int i=0; i<1000; i++)
  {
    raw = analogRead(A0);
    float temp = raw * 0.217226044 - 61.1111111;
  }
  stop = millis();
  Serial.println(stop - start);

}

void loop()
{}
Rob Tillaart

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

grendle

#4
Mar 07, 2013, 12:09 am Last Edit: Mar 07, 2013, 12:14 am by grendle Reason: 1
ofcourse Rob, I'll set it up and run it tonight when I'm home and report the results.

nvm, was able to get this done lol.

119
161
120. 

Also Rob, I was going to pm you about the math:
Code: [Select]
raw = analogRead(A0);
float temp = raw * 0.217226044 - 61.1111111;


can you show me how you did that please? I have been messing around with it a bit last night but I couldnt get it.

robtillaart

OK, for this one time ;)

a= ((a*(5.0 / 1024)) - 1.375) / 0.0225;

<==>

a =  ((a*(5.0 / 1024)) - 1.375) * 44.4444444444;  // as 1/0.0225 = 44.4444444..;

<==>

a =  (a*(5.0 / 1024)) * 44.4444444444 - 1.375 * 44.4444444444;  // distribute the multiplication;

<==>

a =  (a*(5.0 / 1024)) * 44.4444444444 - 61.11111111;

<==>

a = a * (0.004882812)  * 44.4444444444 - 61.11111111;

<==>

a = a * 0.217226044 - 61.1111111;

Quote
119
161
120.

means
- an analogRead takes about 119 microsec  [OK]
- the original formula takes 161 - 119 micros() = 42 microsec; [OK]
- the optimized formula takes 120-119 micros() = 1 microsec;

I cannot believe the last measurement, seems to be optimized away

rewritten the last loop to
for (int i=0; i<1000; i++)
  {
    raw = analogRead(A0);
    raw = raw * 0.217226044 - 61.1111111;
  }

gives

119
162
128  ==> 9 micros for the formula

So the new formula drops factor 4 by going from 4 to 2 Floating Point operations (esp removing divisions)

Rob Tillaart

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

grendle

Quote
- an analogRead takes about 119 microsec  [OK]
- the original formula takes 161 - 119 micros() = 42 microsec; [OK]
- the optimized formula takes 120-119 micros() = 1 microsec;

I cannot believe the last measurement, seems to be optimized away


i know right thats great! thank you so much for taking the time to show me this, your way is much faster/optimized. i gotta practice the math now lol.

robtillaart

When you develop code there are three steps:
- get requirements right
- make it right
- make it faster (if needed)

you should not optimize code if you do not need to. (but they are fun to do and useful to understand)
Rob Tillaart

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

grendle

Quote
When you develop code there are three steps:
- get requirements right
- make it right
- make it faster (if needed)

you should not optimize code if you do not need to. (but they are fun to do and useful to understand)


so you can tell im noobtastic at this arduino stuff huh, lol. i thank you for the instruction, and im going to apply what ive learned to some of my other sensors and see how it works out. i have been out of school for 30 years now, when i was in high school i probably could have done the math no problem, but now, not that easy lol. they say when you retire to get a hobby, this is the hobby i chose lol. but seriously, im going to apply what you've said to my projects, so rest assured your parting of knowledge will not go unheeded, really appreciate it Rob, look forward to more of your posts.

robtillaart

Glad to be able to help - if more questions arise do not hesitate to ask
Rob Tillaart

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

Go Up