Go Down

Topic: lilypad temperature sensor (Read 3107 times) previous topic - next topic


Dec 30, 2009, 10:30 pm Last Edit: Dec 30, 2009, 10:31 pm by tobycatlin Reason: 1
Not doing very well today maybe its the hangover. I am stuck with my stepper motor and so i thought i would play with a lilypad temp sensor i have and have got stuck with that too.

I couldn't find any examples of code for the lily pad and so have nabbed some other code that i thought might work


int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

void setup()
Serial.begin(9600); // start serial communication

void loop()

for(i = 0;i<=7;i++){ // gets 8 samples of temperature

samples = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples;


tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit

if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature

Serial.print(" Celsius: ");

tempc = 0;

delay(1000); // delay before loop

I have the power to the lilypad running from the 5v and gnd and then the third wire running to analogue pin 0

Its currently 23 deg c in this room as measured by my multimeter temp feature (and that feels right cos its bloody freezing in here) and the output from the above code shows 62 deg c.

the value i read off the pin 0 is 128 at 62

If i hold the temp sensor the value does rise but i don't know whats wrong. i might just give up for tonight and grab some whiskey and a sofa.

Unless anyone has any ideas??


oh the lilypad uses a MCP9700, a small thermistor type temperature sensor


Just found this little sketch that does the job beautifully:


//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
void loop()                     // run over and over again
  // get voltage reading from the secret internal 1.05V reference
  int refReading = analogRead(BANDGAPREF);  
  // 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

for the rest of the tutorial go here: http://www.ladyada.net/learn/sensors/tmp36.html

David Rock

I'm trying the same thing, and I'm getting wildly variable readings.  I'm using a Duemilanove and the LilyPad temp sensor, with 5v and analog 0 as the sensor input.  

When I read the voltage with a multimeter, I'm getting a consistent reading of 0.77v, which is correct for the current temp.  The raw readings I am getting range from 190 to 230.  If the voltage is consistent, shouldn't the value being read on pin 0 also be consistent?

What would cause such a wide range in sensor data?

David Rock

Ok, so I think the variations are from a poor connection.  I am able to get a consistent reading now.  The next thing I am curious about is it appears that the voltage reading is affected by the speed at which I try to read the value.  Using the above code as is, I get a voltage that is consistently 0.03v above what I get from my multimeter.  If I change the delay to 5 seconds, instead of 1, I get a reading that matches up exactly with what I get from the multimeter. I also see voltages that are consistently higher if I try to read faster (e.g., 500ms).

So, what is it about the speed of the polling interval that would cause the reading to go up or down? Specifically, why does trying to read on a shorter interval cause the value to be higher?  I assume this is an electrical issue, but I have no idea what the reason would be.



hey toby,

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!!!!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131