Add Temperature Sensor

Post Reply
QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Add Temperature Sensor

Post by QuadBow »

Some people are interested in receiving the air temperature. Multiwii supports this request by reading the barometer's temperature.
However, this is an internal temperature which is much higher than the temperature of the air due to internal heating. Thus, I have integrated an additional temperature sensor in order to overcome this issue.

There are several temperature sensors available on the market. Some of them are based on i2c (LM75), others on onewire (DS18S20), and others are analog (LM35, KT110).

I have implemented an analog LM35, which has a third pin as output and therefore it can be connected easily to a free analog input of the board.
There exist several sensors of the LM35 family, which differ in accuracy, temperature range, and costs. I bought the cheap LM35DZ for just 1.10€ which has a resolution of 0.25°C and a temperature range from 0°C onwards. It fits to my purpose since I don't use to fly when it is too cold...

What I did was enabled the temperature measurement in config.h via

Code: Select all

/********************************************************************/
/****           temperature monitoring                           ****/
/********************************************************************/
#define TEMPERATURE LM35
#define TEMPERATURE_SMOOTH 32


than defining the pin being used for the temperature measurement in the section of the used microcontroller (in my case MEGA) in file def.h

Code: Select all

  #define TEMPERATUREPIN             A1    // Analog Pin 1 for temperature measurement by LM35


I inserted the used variable in the analog structure of file types.h as follows.

Code: Select all

typedef struct {
  uint8_t  vbat;               // battery voltage in 0.1V steps
  uint16_t intPowerMeterSum;
  uint16_t rssi;              // range: [0;1023]
  uint16_t amperage;
  #if defined (TEMPERATURE)
    uint16_t temp;            // temperature in 0.01°C steps by LM35
  #endif
} analog_t;


The actual code is being placed in the switch loop for analog sensors in the file multiwii.cpp. Search for the first lines and change the third line from 3 to 4 cases.

Code: Select all

  // query at most one multiplexed analog channel per MWii cycle
  static uint8_t analogReader =0;
  switch (analogReader++%4) {


Than I added the code as a switch-case.

Code: Select all

  case 3:
  {
  #if defined(TEMPERATURE) && (TEMPERATURE == LM35)
      static uint8_t temp_ind = 0;
      static uint16_t temp_vec[TEMPERATURE_SMOOTH], temp_sum;
      uint16_t temp = analogRead(TEMPERATUREPIN);
      #if TEMPERATURE_SMOOTH == 1
        analog.temp = temp * 50; // 0.01V/°C and 5mV/step
      #else
        temp_sum += temp;
        temp_sum -= temp_vec[temp_ind];
        temp_vec[temp_ind++] = temp;
        temp_ind %= TEMPERATURE_SMOOTH;
        analog.temp = (temp_sum / TEMPERATURE_SMOOTH) * 50; // 0.01V/°C and 5mV/step
      #endif
      debug[3] = analog.temp;    // show temperature in debug entry in 0.01°C each step
  #endif
  }


The result is the temperature in 0.01°C steps, but, due to the accuracy of the analog/digital converter the resolution is just 0.5°C.
I omitted to increase the resolution, since I forward the temperature via FRSKY telemetry to the display FLD-02, which has a resolution of just 1.0°C.
Thus, the result is fine with me. It shows a difference of ca 10°C compared with the barometer's temperature.

Have fun!

sismeiro
Posts: 173
Joined: Tue Feb 21, 2012 12:33 pm

Re: Add Temperature Sensor

Post by sismeiro »

Hi,

Thank you for your code and explanation on how to add it, I hope some one pick it up and add it to the main MultiWii code. It's a "nice to have" feature only with a few bucks of investment and I always wondered how cold is it a few miles away of the earth ground. You will never know when I will do a project to do that. :)

Regards,
Luis Sismeiro

MHefny
Posts: 18
Joined: Sun Jan 05, 2014 4:27 pm
Location: Cairo, Egypt
Contact:

Re: Add Temperature Sensor

Post by MHefny »

Very nice.
BTW some Gyros such as 6050 has built in temperature sensor .... I believe it can be used without extra hardware.
The same for BARO as each baro has a temperature sensor that is necessary for its operation. you will find this code in sensors.cpp in the baro section.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Add Temperature Sensor

Post by QuadBow »

MHefny wrote:BTW some Gyros such as 6050 has built in temperature sensor .... I believe it can be used without extra hardware.
The same for BARO as each baro has a temperature sensor that is necessary for its operation. you will find this code in sensors.cpp in the baro section.

You are absolutely right, those devices do have a built-in temp sensor.
However, that is their disadvantage, because the temperature drift caused by the internal heating of the sensor should not be underestimated.
I found differences like: Baro's temperatur= 30°C and the external temperature sensor's temperatur= 20°C.
So, it is the question which level of accuracy you need...

MHefny
Posts: 18
Joined: Sun Jan 05, 2014 4:27 pm
Location: Cairo, Egypt
Contact:

Re: Add Temperature Sensor

Post by MHefny »

OIC ... you point is very valid.
maybe the temp sensor of baro sensing internal chip temp which could be higher due to heat dissipation inside.
or maybe the outter sensor gets cooled by propellers...
what do u think ?

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Add Temperature Sensor

Post by QuadBow »

MHefny wrote:maybe the temp sensor of baro sensing internal chip temp which could be higher due to heat dissipation inside.
or maybe the outter sensor gets cooled by propellers...
what do u think ?

The internal baro sensor measures higher values because of its own power dissipation - in particular if the heat damping resistance is higher when a protection shield is used which consists of foam...
The propellers don't cool the external temperatur down, since they only keep the sensor at the ambient temperature of the air. So, the external sensor measures the true air temperature.

A propeller can only cool down something which is wet, because of the vaporization. You will get the feeling of being cooled down, when a propeller blows to your sweated skin. But, a copter doesn't sweat...

brm
Posts: 287
Joined: Mon Jun 25, 2012 12:00 pm

Re: Add Temperature Sensor

Post by brm »

yeah, i am happy my cpu cooler works.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Add Temperature Sensor

Post by QuadBow »

brm wrote:yeah, i am happy my cpu cooler works.

I am afraid you have not understood the issue:
Your cooler does not cool down the cpu's temperature below the ambient temperature of the air!
or maybe the outter sensor gets cooled by propellers...
what do u think ?

And that was exactly the question I have answered.

brm
Posts: 287
Joined: Mon Jun 25, 2012 12:00 pm

Re: Add Temperature Sensor

Post by brm »

peltier cooling,
i still think my cpu cooler works.

but you do not know why these devices do have some sort of temperature measing device?
it ends up comparing apples with bananas ;-)

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Add Temperature Sensor

Post by QuadBow »

brm wrote:peltier cooling,
i still think my cpu cooler works.

What does the peltier effect have to do with the origin question?
brm wrote:but you do not know why these devices do have some sort of temperature measing device?

Of course, in order to compensate the measurement drift caused by temperature. Why are you interested in the answer to that question?
brm wrote:it ends up comparing apples with bananas

So, why do you compare propeller cooling limited by the ambient temperature with peltier cooling down below the ambient temperature?

Post Reply