Sunday, October 18, 2015

Temp / Voltage Without Sensors

The Arduino UNO (and 328 clones) has the ability to report the rail voltage and temperature of the chip. You don't need any additional wiring or sensors, just the Arduino itself. The temperature is not very accurate, and reports in Celcius, so we added a "adjustment" factor until it read room temperature, and converted to Fahrenheit. This is a fun "ok, I made it blink, now what" project.

Code


void setup()
{  
Serial.begin(9600);   

}   
void loop() 
{ 
  
 float volts = readVcc();
 Serial.print(volts/1000);
 Serial.println(" VDC");

 float temp = readTemp();
 temp = temp/1000;
 temp = temp + 25.7; //adjustment 
 temp = (temp * (9/5)) + 32; //conversion to fahrenheit
 Serial.print(temp);
 Serial.println(" F");
 delay(1000);
}   

long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

long readTemp() {
  long result;
  // Read temperature sensor against 1.1V reference
  ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = (result - 125) * 1075;
  return result;
}

No comments:

Post a Comment