Go Down

### Topic: Lilypad Temperature Sensor. Help (Read 1 time)previous topic - next topic

#### colleenjmorgan

##### Mar 28, 2011, 08:48 am
Hi,

I am using the Lilypad Temperature sensor with the Lilypad Arduino w/ATmega328.

At this point I am having difficulties getting a consistent reading of the ambient temp.  It jumps from as low as 15 degrees Celsius to 25 degrees Celsius. The actual temp should be 24 degrees Celsius.

My code is below.  Any help would be really appreciated!

Code: [Select]
`//analog input 0/digital 14int tempSenPin = 0;void setup(){  Serial.begin(9600);  pinMode(tempSenPin, INPUT); }void loop(){  int reading = analogRead(tempSenPin);    float voltage = reading * (3300 / 1024);     float tempC = (voltage - 500)/10;     Serial.print(tempC); Serial.println(" degrees C");  delay(1000);}`

#### robtillaart

#1
##### Mar 28, 2011, 01:52 pm
Analogpins don't need a pinMode to be set,

There is a math error in    voltage = reading * (3300 / 1024);    3300/1024 = allways 3
so this line just states   voltage = reading * 3;
to make it float voltage = reading * 3300.0 / 1024;   //  note the .0 to force float!
==> code below

Quote
The actual temp should be 24 degrees Celsius.

So 25 degrees is an acceptable measurement, but 15 is not. As the math does not fluctuate It could be a loose contact; Or maybe you need to do an averaging of the temperature. I added a RAW reading print in the code below.

Can you post e.g. 20-50 readings?

Code: [Select]
`int tempSenPin = 0;void setup(){  Serial.begin(9600);  Serial.prinln("start");  // for debugging only to see it works.}void loop(){  int reading = analogRead(tempSenPin);  Serial.print("RAW: "); Serial.println(reading);  float voltage = reading * 3300.0 / 1024;      float tempC = (voltage - 500)/10;     Serial.print(tempC); Serial.println(" degrees C");  delay(1000);}`

There is also some code on - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1262208606 - it uses different math.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### MarkT

#2
##### Mar 28, 2011, 09:01 pm
How is this wired up?  The conductive thread used for the lilypad is not electrically ideal BTW, it might be the problem.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### colleenjmorgan

#3
##### Mar 29, 2011, 06:34 am
Thanks so much,

My temp sensor is returning much more consistent and accurate readings now using the code changes you suggested.

It may also be that I am now in an airconditioned office where the temperature is regulated.

Now to write some code to do something with my temp inputs : )

#### robtillaart

#4
##### Mar 29, 2011, 12:53 pm
Quote
Now to write some code to do something with my temp inputs : )

- red led is the temperature is hot, blue if too cold, green = perfect
- monitor max and min value during a day (optional with timestramp)
etc

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up

Please enter a valid email to subscribe