Hopefully someone can point out where my train of thought is off on this.
I am trying to use a teensy, and the analogRead function to get a temperature out from a GM coolant sensor, Part No. 15326388 (Thermistor). Link to Datasheet here.
I first tested this one a Arduino nano, to make sure my pulldown resistor was the correct value and verify everything worked. Once this was moved over to a Teensy 3.2, its showing very different values and it doesnt make sense. The same resistor, and coolant sensor were used.
Nano Code:
#include <math.h>
void setup() {
Serial.begin(115200);
}
double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celsius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
return Temp;
}
void loop() {
int val;
double temp;
val=analogRead(0);
temp=Thermister(val);
Serial.print ("Temperature: ");
Serial.println(temp);
delay(20);
}
Teensy Code:
#include <Arduino.h>
#include <math.h>
//Update Intervals
const int ectCalcInterval = 100;
unsigned long ectCalcLastUpdate;
//Engine Coolant Temp
int val;
double temp;
double readTemp;
int ect = 0;
double Thermister(int RawADC);
void loopECT();
double Thermister(int RawADC) {
readTemp = log(((10240000/RawADC) - 10000));
readTemp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * readTemp * readTemp ))* readTemp );
readTemp = readTemp - 273.15;
readTemp = (readTemp * 9.0)/ 5.0 + 32.0;
return readTemp;
}
void loopECT(){
val=analogRead(0);
temp=Thermister(val);
ect = temp - 55; // adjust signal for Nissan gauge
ectCalcLastUpdate = millis();
}
void setup(void) {
Serial.begin(115200);
}
void loop(void) {
if ((millis() - ectCalcLastUpdate) >= ectCalcInterval) {
loopECT();
}
delay(2);
}
Wiring:
This was wired to A0 on both the Teensy and the Nano.
What I am seeing out of the teensy now are readings of 663F, and the analog read is reading 1023 which would be maxed out even though the coolant probe was at 136F.
I have thought maybe I need to change analogReadResolution(), my PullDown is the wrong value, or I am just blatantly overlooking something obvious.
Any Help would be appreciated.
Hello there!
One thing that caught my eye is that you are using two different boards, a Teensy3.2, and a Nano.
A Teensy3.2 runs on 3.3V logic, where most Nano boards run on 5V logic. This voltage is also the analog reference voltage. The Arduino uses an equation to calculate the value from 0-1023 that you will get from an analogRead() command.
Equation: result = floor((inputVoltage/referenceVoltage) * 1023) [I think it's floor]
Example:
Analog input from some source = 2.5V
Nano analog reference = 5V
Teensy analog reference = 3.3V
NanoResult = floor((2.5V / 5V) * 1023) = floor(0.5 *1023) = 511 (maybe 512)
TeensyResult = floor((2.5V / 3.3V) * 1023) = floor(0.758 * 1023) = 775
Now I may be wrong with the equation and such, but the idea is the same, that the analog reference voltage is different, and will cause different results when reading the same voltage.
You can test this by hooking up both boards and reading the voltage from the same node in your circuit. Printing each analogRead value to the serial monitor should show different numbers.
Thanks for that, seems to be exactly what this is. Now i just need to dig and find out how to get it to read this sensor operating at 3.3v.
The Nano reads it without changing anything, so I know this is definitely due to the ADC aref voltage on the Teensy.
Remove the reference voltage from your calculations. The bridge allows for ratiometric calculation, when AREF equals the bridge supply voltage. Then the ADC readings should be the same with every controller, and can be mapped into temperatures the same way.
You should NOT connect your sensor to Vin on the Teensy. The ADC inputs are not 5V tolerant.
As mentioned above, the reference voltages are different for the two boards, but your supply voltage is the same.
For ratiometric sensors like these (you just have a voltage divider, essentially), the absolute supply voltage doesn't matter, as long as it's the same as the reference voltage (write out the formulas for voltage dividers yourself).
As stated, you must use the 3.3v reference for Teensy 3.2, otherwise you risk damage to the processor. The digital pins of the Teensy 3.2 and 3.5 are 5V tolerant but analog pins are not.
You should always use the same code base when doing things like this, just use #defines or other means to just change the input pins. Why? Well, mistakes. For example, in the Nano code, you have:
readTemp = readTemp - 273.15 - 260;
But, in the Teensy code you have:
readTemp = readTemp - 273.15;
avr_fred:
Well, mistakes. For example, in the Nano code, you have:
readTemp = readTemp - 273.15 - 260;
But, in the Teensy code you have:
readTemp = readTemp - 273.15;
Thanks for pointing this out, this was actually removed to unify them during my troubleshooting before posting. The commit on my local machine still had this, forgetting to fetch and pull the repo always causes headaches.
At some stage you will need to change your sensor/ resistor arrangement if you want to use it as it was originally intended. See your own link to the spec circuit.
bluejets:
At some stage you will need to change your sensor/ resistor arrangement if you want to use it as it was originally intended. See your own link to the spec circuit.
I changed the Vcc to the sensor from 5v to 3.3v, It works with the current sketch but has an offset of about 18%, which is most likely due to the Pull-Down resistor. I will have to find the correct new value for that resistor.
Thanks everyone for all the help, it was informative since this is my first time with a Teensy and didnt even think to check the reference voltages used.