Sunday, September 16, 2007

Arduino + Temperature



Back again with Arduino. This post will be extremely short, because getting a temperature value with +-1ºC is extremely easy and already done. Take a look at this portuguese blog entry. Using an LM35, a device that outputs a linear voltage of 10mV/ºC.

Introduction

I want to monitor the temperature of a room in my house. I want a PC to store this values and a windows application that shows the current temperature of the room.

  • I will use an LM35 as temperature sensor. This device outputs a linear voltage of 10mV per ºC.
  • Arduino will be programmed as a sensor interface that will be waiting for a serial command to get the value of the sensor and send it back using the usb serial cable.
  • To close the loop, I will implement python code to read the temperature from Arduino and then stored in a database and show it in the task bar

References

Thanks to this entry of the Blog do Je programming the Arduino was easier. I´ve also reused the main image from this blog.

Arduino code

I use the following Arduino code to gather the voltage output from the LM35

/*
This program reads the temperature from a LM35 when a serial command is received
and sends back the result
*/

int potPin = 0;            // select the input pin for the LM35
int ledPin = 13;           // select the pin for the LED
float temperature = 0;     // variable to store the value coming from the sensor

void setup()
{
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(115200);
  Serial.println("Ready");
  digitalWrite(ledPin, HIGH);
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch == 's')
    {

      val = analogRead(potPin);    // read the value from the sensor
      temperature = (5.0 * val * 100.0)/1024.0;
      Serial.println((long)temperature);

    }
  }
}
Pretty easy isn't it? The only weird thingy is the conversion formula. Let's explain it a little bit:
  • The analogRead function returns values from 0 to 1023 where 0 means 0V and 1024 means 5V.
  • Bearing in mind that each 10mV is 1ºC we have to multiply 1V by 100 to get the ºC.
  • And that's it

Python code

As the Arduino stage was easy I spent more time coding in python. I finally used wxPython for the GUI

  • I managed the way to create an icon in the task bar showing the temperature.
  • I configured a timer to ask Arduino for the temperature periodically.
  • I also coded a class to create and add data to a database stored in a file using sqlite.
  • At the end I used py2exe to wrap all the development in a nice .exe files and several dlls so there is no need to install python to run the application.here (I was testing an DS1620 and LM35 as temperature sensor at the same time so the python code reads two temperatures, I haven't had time to update the code just for one temperature value. There are not too much comments, I'm not proud of this code but I hope it will work as a reference. Sorry!)
  • I´ve put everything in a .zip code

12 comments:

  1. With that configuration of the LM35, won't it put out a negative voiltage at temperatures below 0 C ?

    I don't think Arduino analog pins like negative voltages !

    ReplyDelete
  2. Maybe you are right but I hope my home will never be bellow 0C

    ReplyDelete
  3. Cool !! This is the essence of open source & open hardware : use , reuse , modify and adapt to our needs . Im glad to have helped you in your project.
    Good hacking to you !!

    ReplyDelete
  4. Thanx for that post. I used it for the basis of my color sensor project. I was invaluable!
    soham

    ReplyDelete
  5. [...] I know you don't want to build anything, but... The Boundingbox Blog Archive Arduino + Temperature [...]

    ReplyDelete
  6. Little mistake: from 0 to 1023 (total 1024)

    ReplyDelete
  7. I agree with the author: there is no (pratical) reason to measure indoor temperatures under 0 °C ....

    KIS !

    The choice of LM35 is excellent... I'm using PHP instead to Python...and all works fine...nice job!

    ReplyDelete
  8. Jeronimo, please advice to everyone uses Arduino 10000 that they must disableThe Boundingbox » Blog Archive » Arduino + Temperature autoreset (a new feature in the latest Arduinos that avoid to push reset button before sketch uploading)...otherwise whenever the serial is opened the first readings are lost because a reset event happens at opening of serial.

    ReplyDelete
  9. When you say from 0 to 1024, you mean from 0 to 1023, right?

    Nice job!

    ReplyDelete
  10. Hi iam wondering why there is a 100k resistor between output and ground

    ReplyDelete
  11. Thanks a lot. You're right. Post updated.

    ReplyDelete
  12. Hi, you could config the Analog input full range to be 1.1V instead of 5V it would limit the temperature range to 110C but increase the precision because now 0V=0 1.1V=1023.
    Also adding a cap in parrallele to the input and gnd help stabilize the value

    Hope this help your project.

    ReplyDelete