How to calculate analogue value when voltage fluctuates?

Hi,

First post so please be gentle.

I've got a circuit that I've designed that is basically temperature controlled relay. Its using an ATTINY85 as the brains, and a tmp36 sensor for the temperature readings.

To convert the analogue signal to temperature I'm using the age old formula:

value = analogRead(A1);
millivolts = (value / 1024.0) * 5000;
temp = (millivolts / 10.0) - 50;

The problem is that when the without the coil engergised, the reading is 5.12 volts, and with the coil energised, the voltage is 4.97 volts. This means that the formula above provides temps about 2 degrees different depending on if the coil is energised or not (because of the fixed assumption that the voltage is 5000 mV)

I'm currently power from a usb power source (not from a computer). But have access to other more powerful supplies if that will make a difference.

Is there anyway to get the actual voltage of the circuit at runtime so the calculation can be more accurate? Does that voltage drop sound like too much?

Thanks!

The ADC is ratiometric, which means that you need to accurately know the ADC reference voltage in order to make an accurate voltage measurement. The "age old" formula is almost never correct.

If you are currently using the power supply voltage as the ADC reference voltage, that is the problem. Instead use one of the two internal voltage references on the ATtiny85 and recalibrate the software.

See this reference page, but I'm not sure how it works on the ATtiny85.

Rob_Smart:
a tmp36 sensor for the temperature readings.

To convert the analogue signal to temperature I'm using the age old formula:

value = analogRead(A1);
millivolts = (value / 1024.0) * 5000;
temp = (millivolts / 10.0) - 50;

Not sure if you're trying to make a temp sensor or a voltmeter.

You should use 1.1volt Aref for a 'voltage output' sensor, and you should convert directly from A/D value to temp.
Voltage (millivolt) has nothing to do with anything.
Whoever wrote that line years ago did not understand analogue temp sensors and microcontrollers.

Only one line is needed.
Declare 'temp' as a float.

tempC = (analogRead(tempPin) * 0.1039) - 50.0; // for TMP36, calibrate by changing the last digit(s) of 0.1039

Leo..

Thanks, I hadn't really got round to the refactoring stage of the code, so pretty much just used what I found in the examples on the web. This seemed to be the standard way of the doing the calculation based on what I've seen.

I'll give the aref voltage a go to see if that works better.

Thanks again,

Rob

Basic test code.

// LM35_TMP36 temp
// works on 5volt and 3.3volt Arduinos
// connect LM35 to 5volt A0 and ground
// connect TPM36 to 3.3volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"

const byte tempPin = A0;
float calibration = 0.1039;
float tempC; // Celcius
float tempF; // Fahrenheit

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use internal 1.1volt Aref
}

void loop() {
  //tempC = analogRead(tempPin) * calibration; // use this line for an LM35
  tempC = (analogRead(tempPin) * calibration) - 50.0; // use this line for a TMP36
  tempF = tempC * 1.8 + 32.0; // C to F
  Serial.print("Temperature is  ");
  Serial.print(tempC, 1); // one decimal place
  Serial.print(" Celcius  ");
  Serial.print(tempF, 1);
  Serial.println(" Fahrenheit");

  delay(1000); // use a non-blocking delay when combined with other code
}

Leo..

Hi,

Yes, that worked a treat thank you.

Thanks very much,

Rob

Hi,

I'm seeing my temp readings Max out at 55.37 C

Is this because I'm using the lower internal reference voltage? is there a way around this?

Thanks,

Rob Smart

If the ADC reading is 1023, then the ADC input voltage is roughly equal to, or higher than the reference voltage.

Always a good idea to check the device data sheet, as shown in the image below.

tmp36.png

tmp36.png

No experience with the ATTINY85.
It might have a 2.56volt Aref that you can switch to.
That would drop temp resolution to ~0.2C though.

Not sure what your temp requirements are.
Could switch to an LM35. That one will measure about 5C to 105C on 1.1volt Aref.
Or a digital DS18B20 (-55 to 125°C).
Leo..

The Tiny85 indeed does have a 2.56V internal reference.

Another option (for the many Atmel chips that have only a 1.1V reference) would be a voltage divider on the sensor's output, to bring down the voltage to the ADC range.