# Programming help, converting voltage reading to temperature.

I'm reading the voltage off of an electrical/mechanical water temperature gauge attached to a generator. I've tapped into the + voltage wire on the back of the gauge run it through a voltage divider then to one of the analog inputs on a mega. That was the easy part, now I can read the voltage 11-4 volts easily from a webpage or the serial output. I need some assistance converting the voltage to the approximate engine temperature.

The voltage drops as the temperature increases. The voltage starts out at 10 volts = 100 degrees F* and every .1 volts drop in voltage indicates 2 degree rise in temperature.

Can anyone come up with a simple function to translate the voltage into a readable temperature?

I could "hack" it but I'm trying to learn better ways of coding.

double temp = 100.0 + (10.0 - voltage) * 20.0

How does the convert code to voltage look like?
You can probably do a direct conversion (1 step) to temperature.

void water_temp(){
water_temp_voltage = (((water_temp_read / 1023) * 5) * 5);

}

This is the function I am using to read the voltage output from the gauge (I'm using an almost identical function to read battery voltage which consistently gives the same readings as my multi-meter).

Setup is: Gauge --> 5x Voltage Divider--> Analog pin on Arduino.

KeithRB:
double temp = 100.0 + (10.0 - voltage) * 20.0

Thanks, I'll give it a try when I get a chance.

Please consider the following

``````  int raw = analogRead(water_temp_input);
int water_temp_voltage = (raw * 25 / 1023);
``````

by first multiplying the reading first you can keep the math in the integer domain, without loosing too much precision.

in a oneliner as float
float voltage = analogRead(water_temp_input) * 0,0244379277;