Pages: [1]   Go Down
Author Topic: AD22100 temp sensor arduino operation  (Read 1132 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14026
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

removing slow divisions gives
Code:
raw = analogRead(A0);
float temp = raw * 0.217226044 - 61.1111111;
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sweet. very nice Rob thx!  smiley
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14026
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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()
{}
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: March 06, 2013, 06:14:31 pm by grendle » Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14026
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, for this one time smiley-wink

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)

Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14026
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14026
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: