Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Internal Temperature Sensor
- // Example sketch for ATmega328 types.
- //
- // April 2012, Arduino 1.0
- void setup()
- {
- Serial.begin(9600);
- Serial.println(F("Internal Temperature Sensor"));
- }
- void loop()
- {
- // Show the temperature in degrees Celcius.
- long pomiar = readTemp();
- Serial.print("ADC: ");
- Serial.print(pomiar);
- Serial.print("\t Temperatura: ");
- Serial.println(normalizeTemperature(pomiar),3);
- delay(1000);
- }
- long readTemp() {
- // Read temperature sensor against 1.1V reference
- #if defined(__AVR_ATmega32U4__)
- ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0);
- ADCSRB = _BV(MUX5); // the MUX5 bit is in the ADCSRB register
- #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
- ADMUX = _BV(REFS1) | _BV(MUX5) | _BV(MUX1);
- #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
- ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0);
- #else
- ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
- #endif
- delay(2); // Wait for ADMUX setting to settle
- ADCSRA |= _BV(ADSC); // Start conversion
- while (bit_is_set(ADCSRA,ADSC)); // measuring
- uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
- uint8_t high = ADCH; // unlocks both
- long result = (high << 8) | low; // combine the two
- return result;
- }
- float normalizeTemperature(long rawData) {
- // replace these constants with your 2 data points
- // these are sample values that will get you in the ballpark (in degrees C)
- float temp1 = 17;
- long data1 = 280;
- float temp2 = 22.0;
- long data2 = 294;
- // calculate the scale factor
- float scaleFactor = (temp2 - temp1) / (data2 - data1);
- // now calculate the temperature
- float temp = scaleFactor * (rawData - data1) + temp1;
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement