Analog read using P2
Hello all,
I am trying to read thermistor Analog values using P2, and converting into temperature reading.
I have developed code for P2 as below.
I am using pin 14 of P2. and setup pin as below.
_pinstart(Temp_pin, P_ADC_1X | P_ADC, 13, 0); //init ADC pin
and running this tsr_thread1() in new cog using
cogid4 = __builtin_cogstart(tsr_thread1(), stack4);
void tsr_thread1() { unsigned int beta = 3380, units, tens; float temperature, resistance; while (true) { uint32_t a = _rdpin(Temp_pin); // a = (10000*5/a) - 10000; //a = thermistor.read_u16(); /* Read analog value */ /* Calculate the resistance of the thermistor from analog votage read. */ resistance = (float)10000.0 * ((16383.0 / a) - 1.0); /* Convert the resistance to temperature using Steinhart's Hart equation */ float beta_inv = (float)(1/beta); float ln_res = (float)log(resistance/10000); float amb_temp = (float)(1/298.15); temperature = amb_temp + beta_inv * ln_res; temperature = (float)1/temperature; temperature = (float)temperature- 273.15; // temperature = // (1 / ((log(resistance / 10000.0) / beta) + (1.0 / 298.15))) - 273.15; //if (temperature >= 40) { printf("Temperature = %f resistance=%f %d\n", (float)temperature, (float)resistance,a); //} _waitms(1000); } }
This is the output i am getting
Temperature = 25.000000 resistance=9559.456000 8376 Temperature = 25.000000 resistance=9599.234000 8359 Temperature = 25.000000 resistance=9557.121000 8377 Temperature = 25.000000 resistance=9557.121000 8377
As per cartridge code, temperature value should be in 40 c to 65 c.
@evanh tried to help with different set of code(https://forums.parallax.com/discussion/comment/1544807#Comment_1544807), and that also giving me 25 c temperature.
I have tried to interface normal NTC thermistor to P2 as well. But every time i am getting 25 c temp.
If i increase room temp then also i am getting 25 c temp.
So seems to be issue in Analog read using P2. Because when i interface NTC thermistor to Arduino then i am getting proper room Temperature values. Can someone please help?
Comments
Have you tested the analog input in isolation with a test voltage? Remember, too, that your analog readings have to be calibrated to the Vcc and Vss levels of the chip. This code is in Spin2 but should make sense. The variable ap is the analog input pin (it's global in this library). I've attached the entire object.
Probably a silly question, but your thermistor is part of voltage divider circuit, right?
It was issue with the pin of P2.
I was using pin 14 and when i changed pin to 55 then able to get proper thermistor readings.
here is the c code, which takes 5 samples of reading, make average reading and calculate temperature.
Started pin 55 with _pinstart(Temp_pin, P_ADC_1X | P_ADC, 13, 0); //init ADC pin
Glad you sorted that problem. Still, to get a calibrated reading from an ADC pin you have to know the ground and Vcc values (with ground applied to the pin the ADC will not read 0). This code (from my Spin2 object) takes a new reading, adjusts with the calibration values (callo [Gnd] and calhi [Vcc]), and then scales into a user-set range (urlo to urhi). If 0 and 3300 are used for urlo and urhi this method returns the pin voltage in millivolts.