 # Improving code for using GM oil/coolant temp sensor

Hey guys,

I’m pretty new to programing and need some guidance to improve my code.

The goal is to read two GM oil/coolant temp sensors, before and after the oil cooler, and output the temps on an Ardafruit RGB 20x4 display.

Right now the code works but I feel like there could be a couple of things that I’m missing that could make the code better/more reliable.

``````//2 GM coolant sensors p.n. 12146312
// The serial rate is 9600
// rgb codfe is set for bmw orange displays(close enough at least at this pont)

#define NUMSAMPLES 10
#include <LiquidCrystal.h>
#include <Wire.h>
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int brightness = 255;

void setup() {            //This function gets called when the Arduino starts
Serial.begin(9600);
pinMode(REDLITE, OUTPUT);
pinMode(GREENLITE, OUTPUT);
pinMode(BLUELITE, OUTPUT);
brightness = 255;
lcd.begin (20, 4);

}
double Thermister(int RawADC) {  //Function for the Steinhart-Hart equation
double Temp;
Temp = 1 / (0.0014338506806442454 + (0.00023802729624822978 + (9.417798854519078e-8  * Temp * Temp ))* Temp ); //values based of RvsT curve for the sensor
Temp = Temp - 273.15;              // Convert Kelvin to Celsius
return Temp;
}

void setBacklight(uint8_t r, uint8_t g, uint8_t b) {  // normalize the red LED - its brighter than the rest!
r = map(r, 0, 255, 0, 255);
g = map(g, 0, 255, 0, 255);

r = map(r, 0, 255, 0, brightness);
g = map(g, 0, 255, 0, brightness);
b = map(b, 0, 255, 0, brightness);

// common anode so invert!
r = map(r, 0, 255, 255, 0);
g = map(g, 0, 255, 255, 0);
b = map(b, 0, 255, 255, 0);
Serial.print("R = "); Serial.print(r, DEC);
Serial.print(" G = "); Serial.print(g, DEC);
Serial.print(" B = "); Serial.println(b, DEC);
analogWrite(REDLITE, r);
analogWrite(GREENLITE, g);
analogWrite(BLUELITE, b);
}

void loop() {             //This function loops while the arduino is powered

int samples[NUMSAMPLES];
uint8_t i;
int val;                //Create an integer variable
double temp;            //Variable to hold a temperature value
int temprounded;

for (int i = 0; i < 255; i++) {
setBacklight(255, 7, 0);
}
for (i=0; i< NUMSAMPLES; i++) {
delay(10);
}
val = 0;
for (i=0; i< NUMSAMPLES; i++) {
val += samples[i];
}

val /= NUMSAMPLES;
temp=Thermister(val);
temprounded=round(temp);
lcd.setCursor(0, 0);
lcd.print ("Oil Temp in: ");
lcd.print(temprounded);
lcd.print (" C");

for (i=0; i< NUMSAMPLES; i++) {
delay(10);
}
val = 0;
for (i=0; i< NUMSAMPLES; i++) {
val += samples[i];
}
val /= NUMSAMPLES;
temp=Thermister(val);
temp=Thermister(val);
temprounded=round(temp);
lcd.setCursor(0, 3);
lcd.print ("Oil Temp out: ");
lcd.print(temprounded);
lcd.print (" C");
Serial.println(temprounded);

delay(1000);            //Wait one second before we do it again
}
``````

I would appreciate any pointers/advise with this.

Thank you.

Akim.

Couple of things :

1. You haven’t shown your circuit , but if you are using a voltage divider to measure the sensor voltage , then if the battery voltage changes it will affect your reading , so you might need to also measure the battery voltage to compensate, or use a different circuit ( eg power them with a constant current).

2. If I was doing this I would calibrate the sensors by heating them up in water and measuring the resistance .

3. Bear in mind that the A/D only gives you a reading of 1part in 1024 at best and it’s unlikely you will be using the whole range of the A/D over the ranges of temperatures of interest , so some of the factors in your maths may not influence the result . The implied accuracy and overhead of using those calculations does not give you a higher resolution in output (so the temperature readout can never be better than 1 part in 1024 and may end up less )
( does you math include the effect of the voltage divider)

4. You could use experimental results and curve fit in a spreadsheet - it will give a simpler bit of maths, more accuracy , and save you some processor “horsepower” ( something like Ax + Bx^2 could well be good enough over you range of interest )

5. Other sensors , eg Ds18b20 , are easier to use , accurate, but probably much harder to mechanically fit unless mounted by clamping to pipework .

On the other hand...
5. If it works then that’s always fine !!