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

Thread: Teensy (ATmega32U4) on-chip temperature sensor

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    2

    Teensy (ATmega32U4) on-chip temperature sensor

    I'm trying to read temperature data in Celsius and Fahrenheit by using Teensy's 2.0 on chip temperature sensor. I know that since i'm trying to use the on chip temperature sensor, I have to work with ADMUX and MUX to get the voltage value then convert them to Celsius and Fahrenheit but i'm not sure what to do regarding the ADMUX and MUX.

    Code:
    int sensorPin = 0; //the analog pin is connected to
                       //the resolution is 10 mV / degree centigrade with a
                       //500 mV offset to allow for negative temperatures
    
    /*
     * setup() - this function runs once when you turn your Arduino on
     * We initialize the serial connection with the computer
     */
    void setup()
    {
      Serial.begin(9600);  //Start the serial connection with the computer
                           //to view the result open the serial monitor 
    }
    
    void loop()                     // run over and over again
    {
      // gets voltage reading from the temperature sensor at analog in 0
      int reading = analogRead(sensorPin); 
    
      // converts reading to voltage, for 3.3v arduino use 3.3
      float voltage = reading * 5.0;
      voltage /= 1024.0; 
    
      // Print sensor 1
      Serial.println("Temperature Sensor 1");
    
      // Print out the voltage
      Serial.print(voltage);
      Serial.println(" volts");
    
      // converts voltage reading to temperature in degree Celsius
      float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                                    //to degrees ((voltage - 500mV) times 100)
    
      // Prints out temperature with unit Celcius
      Serial.print(temperatureC);
      Serial.println(" degrees C");
    
      // converts Celcius to Fahrenheit
      float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
    
      // Prints out temperature with unit Fahrenheit
      Serial.print(temperatureF);
      Serial.println(" degrees F");
    
      // Converts Celcius to Kelvin
      float temperatureK = temperatureC + 273;
    
      // Prints out temperature with unit Kelvin
      Serial.print(temperatureK);
      Serial.println(" degrees K");
    
      delay(1000);  //waiting a second
    }

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,640
    Here is a sketch that reads leonardo/teensy2 internal temp sensor
    Code:
    // internal temp adc 8 for littlebits/leonardo  teensy 2
    // we do raw  ADMUX or patch wiring_analog.c
    //  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1252144888
    // calibration http://nyfiken.org/filer/kalibrering.ods
    
    void setup() {
      Serial.begin(9600);
      ADMUX = _BV(REFS1) | _BV(REFS0) | 7;   // Set internal V reference, temperature reading
      ADCSRB = 0x20;    // ref  24.6
      delay(100);
      chipTempRaw(); // discard first sample
    }
    
    void loop() {
      int raw = chipTempRaw();
      float temp = chipTemp(raw);
      float ftemp = 1.8f * temp + 32;
      Serial.print(temp);
      Serial.print("  ");
      Serial.print(ftemp);
      Serial.print("   ");
      Serial.println(raw, DEC);
      delay(1000);
    }
    
    float chipTemp(float raw) {
      const float chipTempOffset = -142.5;
      const float chipTempCoeff = .558;
      return((raw * chipTempCoeff) + chipTempOffset);
    }
    
    int chipTempRaw(void) {
      ADCSRA &= ~(_BV(ADATE) |_BV(ADIE)); // Clear auto trigger and interrupt enable
      ADCSRA |= _BV(ADEN) | _BV(ADSC);        // Enable AD and start conversion
      while((ADCSRA & _BV(ADSC)));             // Wait until conversion is finished
      return(ADCL | (ADCH << 8));
    }
    You will probably need to calibrate coefficients in chipTemp(). I usually put the board in freezer for 20 minutes, then record temperature and output from chipTemp as board warms and do a least-squares fit to get coefficients.

  3. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Quote Originally Posted by manitou View Post
    Here is a sketch that reads leonardo/teensy2 internal temp sensor
    Code:
    // internal temp adc 8 for littlebits/leonardo  teensy 2
    // we do raw  ADMUX or patch wiring_analog.c
    //  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1252144888
    // calibration http://nyfiken.org/filer/kalibrering.ods
    
    void setup() {
      Serial.begin(9600);
      ADMUX = _BV(REFS1) | _BV(REFS0) | 7;   // Set internal V reference, temperature reading
      ADCSRB = 0x20;    // ref  24.6
      delay(100);
      chipTempRaw(); // discard first sample
    }
    
    void loop() {
      int raw = chipTempRaw();
      float temp = chipTemp(raw);
      float ftemp = 1.8f * temp + 32;
      Serial.print(temp);
      Serial.print("  ");
      Serial.print(ftemp);
      Serial.print("   ");
      Serial.println(raw, DEC);
      delay(1000);
    }
    
    float chipTemp(float raw) {
      const float chipTempOffset = -142.5;
      const float chipTempCoeff = .558;
      return((raw * chipTempCoeff) + chipTempOffset);
    }
    
    int chipTempRaw(void) {
      ADCSRA &= ~(_BV(ADATE) |_BV(ADIE)); // Clear auto trigger and interrupt enable
      ADCSRA |= _BV(ADEN) | _BV(ADSC);        // Enable AD and start conversion
      while((ADCSRA & _BV(ADSC)));             // Wait until conversion is mobdro finished 
      return(ADCL | (ADCH << 8));
    }
    You will probably need to calibrate coefficients in chipTemp(). I usually put the board in freezer for 20 minutes, then record temperature and output from chipTemp as board warms and do a least-squares fit to get coefficients.
    Thanks help for me..

Posting Permissions

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