AnalogInput, Car Temprature sensor, Look up table interpolation major headache

ok so i have a ford falcon im in the process of designing an ecu for it with my ether-mega 2560

the actual sensor is a bastard because when it gets hotter the resistance falls so its around the wrong way in a way.

The problem i am having is calibration issues

this is the output

Voltage From Sensor
2.89
turned into temperature
20.16

Voltage From Sensor
3.48
turned into temperature
10.60

this is the code
it seems to be in the rough range of the correct temperature but it never gets it spot on with interpolation
Any help would be very much appreciated

/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {

float calctemp = 0;

float array[32] = {
// Voltage From Sensor on left // Corresponding Temprature on right
0.12, 160,
0.20, 131,
0.30, 120,
0.36, 110,
0.47, 100,
0.61, 90,
0.80, 80,
1.04, 70,
1.35, 60,
1.72, 50,
2.16, 40,
2.62, 30,
3.06, 20,
3.70, 10,
3.97, 0,
4.42, -10,
};

// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (4.51 / 1023.0); // change for differant base voltage
// print out the value you read:
Serial.println(“Voltage From Sensor”);
Serial.println(voltage);

int x;
for (x= 0; x<32; x=x+2){
if (voltage >=array && voltage <=array[x+2])
{
calctemp = array[x+3] + ( (array[x+1] - array[x+3]) * ( (voltage - array) / (array[x+3] - array) ) );
// temp = base temp + (high temp- base temp) * (Vread - Vbase) / (Vhigh - Vbase)
}
}

Serial.println(“turned into temprature”);
Serial.println(calctemp);
delay(1000);
}

What temperature does the sensor measure? Outside air, intake air, oil, coolant? Some cars use the current through the resistor as a measure of airflow and temperature, as a means of calculating the air mass going into the engine. In such a case, voltage vs. temperature is not really a linear relation. So reading the voltage may not be the way to go. Some signalconditioning may be required. Also, in your conversion to temperature, I think some array indexes are missing. That could cause some inacuracy.

Coolant Temperature sensor , 2 wires to sensor , one is Ground other is straight into the ecu , i can see 4.6V coming from the ecu wire so im guessing its a voltage divider

the main problem is for example the voltage will be 3.7v which is 10C but it displayes 19C instead

its driving me mad lol

float voltage ;
float tempr ;

if ( voltage <= array[0] )
{
    tempr=array[1] ;
}
else if ( voltage >= array[30] )
{   
    tempr=array[31] ;
}
else
{
    for ( int i=0 ; i<= 14 ; i++ )
    {
        int ii=2*i ;
        if ( voltage >= array[ii] && voltage <= array[ii+2] )
        {
            tempr = array[ii+1] + ( (array[ii+3]-array[ii+1]) * ( voltage-array[ii] ) / ( array[ii+2] - array[ii] ) ;
            break ;
        }
    }
}

You need to ensure that the input voltage does not exceed the voltage of your arduino ( 5V or 3.3 V ).

It’s not clear what the 4.51 volts is, in your code.

Your code for the calctemp appears as scrambled crap, so it is impossible to determine what you are trying to do. But don't obsess over the fact that the table is in reverse order.

the 4.51 is the voltage across the non connected voltage divider, that just keeps the Voltage value in the program calibrated to the adc converter lol

thanks michinyon ill give your code a crack :D

// temp = base temp + (high temp- base temp) * (Vread - Vbase) / (Vhigh - Vbase)

This comment is correct,, but your conversion to the actual code appears to be wrong.

shifteh1: the 4.51 is the voltage across the non connected voltage divider, that just keeps the Voltage value in the program calibrated to the adc converter lol

Well if you want to do that, then you probabably want to feed 4.51 volts into the Aref pin of the arduino, but I think you don't actually really want to do that.

Just read the actual voltage on the pin and then scale it using 5V as normal.

im a bit of a noob lol sorry

With your Code michinyon i have it running , Thank you very much :D

next time i work with an array and forget the "0" location ill go and axe myself

What was the final coding you finally used? Would be curious to see.

Bwanna: What was the final coding you finally used? Would be curious to see.

The code that michinyon posted works as intended.

I just used the same code to work with a 10K thermistor and a 40-byte array (2X20) lookup table. Works perfectly!