Pro Micro - 32u4 onboard Temperature reading

I’m using the Pro Micro (3.3v 8MHz) in a project and as I was reading the 32u4 documentation and discovered it has an on-board temperature sensor.

Is there any way to access that via the Arduino IDE? I know it probably wouldn’t be as simple as “analogRead(12)” (though that would be lovely). Is there some code I can add or modify though, to access it? The datasheet gives the MUX address for the temperature sensor as 0b100111 but I’m not sure what to do with that.

I did look in the Pro Micro add-on bundle, particularily the wiring_analog.cpp file, and while I could see the steps taken to perform an analog read, I couldn’t figure out how exactly it went from something like “analogRead(0)” to having the ADC registers correctly addressed.

Thanks!

I have put this together based on the wiring_analog.h file in the arduino core, and based on the Atmel datasheet.

Note that this is untested. If it doesn’t work, let me know and i will try to figure out why.

All you do is call readTempSensor(); and it will return the value (0 to 1023) from the temperature sensor.

int readTempSensor(){
    //Enable the Temp sensor. 0bxx0yyyyy sets mode.
    // xx is the reference, set to internal 2.56v as per datasheet
    // yyyyy is the lower 5 bits of the mux value so 00111 as per what you found
    ADMUX = 0b11000111; 
    ADCSRB |= (1 << MUX5); //MUX5 is the 6th bit of the mux value, so 1 as per what you found
    
    //Convert TWICE as first reading is a dud (according to datasheet)
    sbi(ADCSRA, ADSC); // start the conversion
    while (bit_is_set(ADCSRA, ADSC)); // ADSC is cleared when the conversion finishes
    //Second conversion
    sbi(ADCSRA, ADSC); // start the conversion
    while (bit_is_set(ADCSRA, ADSC)); // ADSC is cleared when the conversion finishes

    byte low  = ADCL;
    byte high = ADCH;

    return (high << 8) | low;
}

Wow that’s exactly what I was looking for!

Thank you very much TCWORLD!

Hi - I am trying the exact same thing but on a Pro-micro 5V.

I am a beginner.

When I copied the same code as above into the IDE ver 1.6 : it gave an error saying ‘sbi’ was not declared in this scope.

In the void loop() - I called for the readTempSensor() and I pasted the readTempSensor code outside of the {} of the void loop()

Any comments please?

Thanks

It looks like Arduino took away sbi() (or maybe there is an #include needed) in the new IDE. I have not tested it other than to see that it compiles, but you might try bitSet(ADCSRA, ADSC); instead. Alternatively, sbi() is a macro that in this case translates to: ADCSRA |= 1<<ADSC; so you could try that as well.

  • Chip

I don’t use the Arduino IDE much, but i believe sbi() & cbi() have been deprecated from g++ for some time. The preferred approach is to use the bit value operators.

Thank you guys. Let me try that.