views:

705

answers:

5

I am connecting a LilyPad Temperature sensor to a LilyPad Arduino 328 Main Board with the goal of reading fairly accurate ambient temperature readings. The sensor is receiving power and giving a responses I'm able to read over serial.

The problem I am confronted with is that reading from the sensor is giving me very unusual - although consistent numbers. I'm reading the analog sensor input and converting to volts like this...

loop(){
    float therm;   
    therm = analogRead(2); // Read from sensor through Analog 2
    therm *= (5.0/1024.0); // 5 volts / 1024 units of analog resolution
    delay(100);
}

This yields a consistent reading of about 1.1 Volts which the sensor documentation indicates would be a ambient temp of about 60 degrees Celsius when the true ambient temp is about 23 degrees. The sensor is not close in proximity to any other electronics so I can't foresee that being the problem.

Is my code for reading the sensor incorrect? Could my sensor be faulty?

A: 

According to this documentation, analogRead returns an integer. Have you tried casting it to a float like so:

therm = (float)analogRead(2);

What does the sensor voltage read on a voltmeter? Does the reading change when you change the temperature of the sensor? (Holding your hand on it should be enough to change the reading.)

Jeanne Pindar
you can safely cast int -> float in c (with some precision loss). The original answer would be useful, though.
FryGuy
+5  A: 

Isn't the lilypad a 3.3V arduino, so that means it should be (3.3/1024.0), which would be 0.726V, or 22.6 C?

FryGuy
+1  A: 

Try this. I had exactly the same problem.read more here: http://www.ladyada.net/learn/sensors/tmp36.html

//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree centigrade with a
                        //500 mV offset to allow for negative temperatures

#define BANDGAPREF 14   // special indicator that we want to measure the bandgap

/*
 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
 */
void setup()
{
  Serial.begin(9600);  //Start the serial connection with the computer
                       //to view the result open the serial monitor 
  delay(500);
}

void loop()                     // run over and over again
{
  // get voltage reading from the secret internal 1.05V reference
  int refReading = analogRead(BANDGAPREF);  
  Serial.println(refReading);

  // now calculate our power supply voltage from the known 1.05 volt reading
  float supplyvoltage = (1.05 * 1024) / refReading;
  Serial.print(supplyvoltage); Serial.println("V power supply");

  //getting the voltage reading from the temperature sensor
  int reading = analogRead(sensorPin);  

  // converting that reading to voltage
  float voltage = reading * supplyvoltage / 1024; 

  // print out the voltage
  Serial.print(voltage); Serial.println(" volts");

  // now print out the temperature
  float temperatureC = (voltage - 0.5) * 100 ;   //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((volatge - 500mV) times 100)
  Serial.print(temperatureC); Serial.println(" degress C");

  // now convert to Fahrenheight
  float temperatureF = (temperatureC * 9 / 5) + 32;
  Serial.print(temperatureF); Serial.println(" degress F");

  delay(1000);                                     //waiting a second
}
tobycatlin
A: 

Shouldn't this float supplyvoltage = (1.05 * 1024) / refReading;

be this float supplyvoltage = (1.1 * 1024) / refReading;

?

The internal reference voltage is 1.1: http://arduino.cc/en/Reference/AnalogReference

I'm a bit confused about this.

Paul
A: 

Hey,

I'm using a LilyPad Arduino 328 Main Board with a Lilypad Temperature Sensor(MCP9700). i used the code u suggested but i'm getting a ambient temp of 55 degree Celsius. Which is totally impossible.(my thermometer shows 31 degrees).

Been on it a whole week and still cant figure out what's wrong. HELP!!!!

TK13