Go Down

Topic: [solved] Lilypad Erratic Temperature Sensor Output (Read 1 time) previous topic - next topic

Andrew T.

Jun 12, 2013, 11:30 pm Last Edit: Jul 01, 2013, 04:50 pm by Andrew T. Reason: 1
Hello, I am new to Arduino but I am looking forward to learning about it and using it for some practical applications.  Currently, I am using a Lilypad Simple Board with the MCP9700 temperature sensor, both of which have been disconnected from the protosnap board.  My goal is to measure the ambient temperature and read it off of the COM port.  However, I am getting erratic readings on screen.

The board is connected to the computer via USB, with the sensor connected to the board in what I understand to be the correct way: hot to hot, ground to ground, and data to pin A2 (my own choice).

The code I am using I found online:

Code: [Select]
//TMP36 Pin Variables
int sensorPin = A2;

#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(2000);                                     //waiting a second
}


However, my output is unrealistic:

Quote
403
2.67V power supply
0.15 volts
-34.63 degress C
-30.33 degress F
159
6.76V power supply
0.40 volts
-9.72 degress C
14.51 degress F
150
7.17V power supply
0.72 volts
22.10 degress C
71.78 degress F
137
7.85V power supply
0.82 volts
32.01 degress C
89.61 degress F
136
7.91V power supply
0.66 volts
15.62 degress C
60.12 degress F


Troubleshooting, the code was modified to output the analog read of Sensor Pin A2.  The read out was erratic, increasing and decreasing regardless of when heat was applied to the sensor.  The temperature sensor was then replaced with the included light sensor.  There, the analog readout would increase with increasing light and vice-versa.  This leads me to believe that the problem may lie with the hardware (the temperature sensor).  On the other hand, I know that the sketch was meant for a different temperature sensor, but I do not see why it would not work in this case.  Any insight is appreciated.

pylon

I don't know where you have the bandgap code from but it's wrong.
One of the first lines of analogRead() is

Code: [Select]
if (pin >= 14) pin -= 14; // allow for channel or pin numbers


so you're actually reading the value of A0. You probably wanted to set the analog MUX to the internal voltage source:

Code: [Select]
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);

But that doesn't work with analogRead().

Please describe what you want to achieve and why you're not just reading the value from A2.

Andrew T.

I agree, there really isn't any reason to include it.  I modified the code.  The readings are now consistent but still incorrect (negative values).  The voltage output from the sensor is now decreasing with increasing temperature.  Is that characteristic of the sensor?

Code: [Select]
int sensorPin = A2;

void setup()
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop()
{
  int reading = analogRead(sensorPin);
  Serial.print(reading); Serial.println(" Bits");
  float voltage = reading * (5000 / 1024); //5000mV supplied by USB
  Serial.print(voltage); Serial.println(" Volts");
  float tempC = (voltage - 500)/10;
Serial.print(tempC); Serial.println(" degrees C");

float tempF = (tempC * 9 / 5) + 32;
  Serial.print(tempF); Serial.println(" degress F");
  delay(1000);

pylon

You have negative results from analogRead()? Please post the actual output you get.


This code
Code: [Select]
float voltage = reading * (5000 / 1024); //5000mV supplied by USB

probably doesn't produce what you expect it to. It's exactly the same as

Code: [Select]
float voltage = reading * 4;

That's the different between integer and float arithmetics.

Andrew T.

Here's my output (reading taken every 3 seconds).  Heat is applied from the 108-bit reading onward.

Quote
80 Bits
320.00 Volts
-18.00 degrees C
-0.40 degrees F

96 Bits
384.00 Volts
-11.60 degrees C
11.12 degrees F

84 Bits
336.00 Volts
-16.40 degrees C
2.48 degrees F

86 Bits
344.00 Volts
-15.60 degrees C
3.92 degrees F

87 Bits
348.00 Volts
-15.20 degrees C
4.64 degrees F

89 Bits
356.00 Volts
-14.40 degrees C
6.08 degrees F

108 Bits
432.00 Volts
-6.80 degrees C
19.76 degrees F

0 Bits
0.00 Volts
-50.00 degrees C
-58.00 degrees F

0 Bits
0.00 Volts
-50.00 degrees C
-58.00 degrees F

97 Bits
388.00 Volts
-11.20 degrees C
11.84 degrees F

24 Bits
96.00 Volts
-40.40 degrees C
-40.72 degrees F

0 Bits
0.00 Volts
-50.00 degrees C
-58.00 degrees F
0 Bits
0.00 Volts
-50.00 degrees C
-58.00 degrees F

0 Bits
0.00 Volts
-50.00 degrees C
-58.00 degrees F


Also, you're correct, I didn't take into account floating point approximation.  I'll look into fixing that as well.

pylon

Please take a picture of your setup and post it. Take care that every wire is visible on the picture. Your output doesn't look like the readings of a thermal sensor, so let's check the wiring.

Andrew T.

The picture is attached.  Here's the reference:

Red:  + on Arduino, + on Temp Sensor
Black: - on Arduino, - on Temp Sensor
Green: A2 on Arduino, S on Temp Sensor
Black USB: to computer

pylon

My guess is that you don't have a connection with the crocodile connectors. Solder wires and try again.

Andrew T.

I soldered the wires but no luck.  I ended up buying a new temp sensor and everything is working  properly; I'm getting room-temperature readings.  I decided to go back to using the bandgap reference because I can't assume 5000mV is supplied by the USB.  It also means that I don't have to worry about floating vs integer arithmetic since the supply voltage reading is a floating point number.  Overall, this was a good learning experience.  Thanks for your help.

Go Up