AD22100 temp sensor arduino operation

hi all, didnt see this. thought id share it.

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

removing slow divisions gives

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

sweet. very nice Rob thx! :slight_smile:

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

(this should work and produce 3 numbers)

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

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:

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.

OK, for this one time :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;

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)

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

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)

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.

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