Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Temperature on analogRead(38)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Oakland, CA
    Posts
    15

    Temperature on analogRead(38)

    Hi,

    I'm having trouble getting a read from the on chip temp sensor. Is there any additional setup that has to be performed? I can't find anything in the datasheet or accompanying app note.

    Thanks,
    mauricio

  2. #2
    Senior Member Jp3141's Avatar
    Join Date
    Nov 2012
    Posts
    486
    Isn't it channel 26 (decimal) ? I think you interpreted 26 as hex and converted to decimal (-> 38) ?
    Also, the result is not directly in degrees -- you have to calibrate each part, but this is start:

    temperatureC = 432 - analogRead(temperatureChannel)*0.02936;

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Oakland, CA
    Posts
    15
    Thanks for the response. I see the channel 26 but I also see 38 getting mapped to 26 in analog.c. What I think the problem was is that I wasn't properly calibrating. Working now!

    Thanks again,
    mauricio

    Code:
    static const uint8_t channel2sc1a[] = {
    	5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
    	0, 19, 3, 21, 26, 22
    };
    
    int analogRead(uint8_t pin)
    {
    	int result;
    
    	if (pin >= 14) {
    		if (pin <= 23) {
    			pin -= 14;  // 14-23 are A0-A9
    		} else if (pin >= 34 && pin <= 39) {
    			pin -= 24;  // 34-37 are A10-A13, 38 is temp sensor, 39 is vref
    		} else {
    			return 0;   // all others are invalid
    		}
    	}
    	//serial_print("analogRead");
    	//return 0;
    	if (calibrating) wait_for_cal();
    	//pin = 5; // PTD1/SE5b, pin 14, analog 0
    
    	ADC0_SC1A = channel2sc1a[pin];
    	while ((ADC0_SC1A & ADC_SC1_COCO) == 0) {
    		yield();
    		// wait
    		//serial_print(".");
    	}
    	//serial_print("\n");
    	result = ADC0_RA >> analog_right_shift;
    	//serial_phex16(result >> 3);
    	//serial_print("\n");
    	return result;
    }

  4. #4
    Senior Member Jp3141's Avatar
    Join Date
    Nov 2012
    Posts
    486
    It's physically channel 26 (dec) in the MCU; Paul's code maps this from channel 38 in an analogRead() call. It's just a (deliberate) coincidence that 0x26 = 38 decimal, so analogRead(0x26) actually works correctly !

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,958
    I just tried it with this....

    Code:
    void setup() {
      analogReference(INTERNAL);
      analogReadResolution(12);
      analogReadAveraging(32);
    }
    
    void loop() {
      Serial.println(analogRead(38));
      delay(10);
    }
    I'm running this right now, and it's printing 2391 and 2392. When I touched the edge of a cold bottle of water to the top of the chip, it went up to 2411 & 2412. When I touch the top of the chip with my soldering iron (in the center, not near any pins), it went down rapidly... somewhere around 2050 before I took the iron way after several seconds.

    Of course this is an extremely imprecise test, but hopefully it helps?

  6. #6
    Senior Member Jp3141's Avatar
    Join Date
    Nov 2012
    Posts
    486
    Internally, the temperature sensor is just a diode (~ 0.7V at room temperature), and this falls at about -1.8 mV/C. With 12 bits, a code of 2400 represents 2400/4096 * 1.2, or 0.7 V -- as you find (1.2 is approximately the internal reference value).

    Each ADC code represents 1/4096*1.2 = 0.3 mV, or about 1/6 of a degree.

    Freescale doesn't guarantee the room temperature value or temperature sensitivity, but it is quite easy to get << 1 degree resolution from this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •