chebe: (Default)
[personal profile] chebe
The LilyPad range includes a temperature sensor, which is a MCP9700 thermistor type sensor, used for measuring ambient temperatures. It has positive and negative power terminals, and a 's' terminal that you connect to an analogue pin on the LilyPad microcontroller. You set up the code in the same way as for other sensors, and read the value:

//analog input 0/digital 14
int tempSenPin = 0;

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

void loop()
{
  int reading = analogRead(tempSenPin);
  delay(1000);
}


But, there's something interesting about temperature sensors. There is a mathematical formula describing the correlation of the voltage reading (what you get when you do an analogRead), and the real objective temperature. This is described in the sensor's datasheet (available from SparkFun).

Voltage-out = Temp-coefficient * Temp-ambient + Voltage@0C

The Voltage-out is based on the analogRead, and Temp-coefficient and Voltage at 0-degrees-Celsius is provided in the datasheet (and pulled out on the SparkFun page for ease). So the unknown we're looking for is Temp-ambient. We rearrange the formula to get:

Temp-ambient = (Voltage-out - Voltage@0C)/Temp-coefficient

The voltage at 0-degrees-Celsius is 0.5V, and the Temp-coefficient is 10mV per degree Celsius. Since this last reading is in mV we'll convert all the V to mV, essentially multiply by 1000.

LadyAda has a good tutorial on reading temperatures, and says that to get the Voltage-out you take the analogRead of your sensor value, and multiply the voltage divided by the range of values. The LilyPad is 3.3V (or 3300mV), and analogue readings range from 0-1023. This gives us:

Voltage-out = sensorReading * (3300/1024)

Putting it all together yields:
Temp-ambient = ((sensorReading *(3300/1024)) - 500)/10

So back to our LilyPad's loop function:
void loop()
{
  int reading = analogRead(tempSenPin);
  
  float voltage = reading * (3300 / 1024); 
  
  float tempC = (voltage - 500)/10; 

  delay(1000);
}


To convert the Celsius temperature to Fahrenheit you simply multiply by 9 divided-by 5, and then add 32.

This does give a seemingly accurate reading (oddly enough I seem to lack any other kind of ambient thermometer), except that it fluctuates quite a bit. But in and around what seems like a decent value. I may have to do some form of averaging to smooth it out.

I was using the new Simple Board, so thought I'd see if there was any difference using the Main Board. There's not, exact same results. So my only other thought is that maybe the USB-power is fluctuating and skewing the results. Of course, I don't have a ready way of reading the values while it's solely on battery power, so I can't check. Interesting though. I wonder if anyone has seen this before?
Page generated 2017-Mar-25, Saturday 09:46 am
Powered by Dreamwidth Studios