Digispark quickref shows two functions, one to read the internal temparature sensor, and the other reading Digispark input voltage. The sketch uses a LCD to display.
I only have a Digispark ATtiny85 module and a USB cable currently, so made use of DigiKeyboard library as before.
I compiled below sketch, and when prompted to insert USB cable, I click into an empty gedit window first before inserting the USB cable with Digispark module. After the module has been flashed the sketch runs and gives temperature and voltage output in gedit window:
3C 4.89V
5C 4.89V
4C 4.89V
5C 4.89V
13C 4.87V
3C 4.89V
5C 4.89V
5C 4.89V
5C 4.87V
7C 4.89V
7C 4.89V
15C 4.87V
7C 4.89V
5C 4.89V
7C 4.89V
7C 4.89V
7C 4.89V
5C 4.89V
5C 4.87V
7C 4.87V
7C 4.87V
7C 4.89V
15C 4.87V
7C 4.87V
7C 4.87V
7C 4.89V
7C 4.89V
Internal temperature sensor shows 7°C working temperature.
Next I disconnected USB cable, opened a new empty gedit tab, inserted USB cable and pressed my thumb firmly on the ATtiny85. As you can see the internal temperature measured raises slowly up to 20°C:
7C 4.89V
9C 4.89V
9C 4.87V
9C 4.89V
9C 4.87V
11C 4.87V
11C 4.89V
11C 4.87V
11C 4.87V
11C 4.89V
11C 4.89V
11C 4.89V
19C 4.87V
11C 4.89V
11C 4.89V
11C 4.89V
11C 4.89V
13C 4.89V
11C 4.87V
11C 4.87V
11C 4.89V
13C 4.89V
11C 4.89V
13C 4.89V
13C 4.87V
13C 4.87V
13C 4.89V
11C 4.89V
13C 4.89V
13C 4.89V
13C 4.89V
13C 4.87V
13C 4.87V
13C 4.87V
13C 4.89V
13C 4.89V
13C 4.89V
13C 4.87V
13C 4.89V
13C 4.89V
13C 4.87V
13C 4.89V
13C 4.89V
13C 4.89V
13C 4.89V
13C 4.87V
15C 4.89V
15C 4.89V
13C 4.89V
13C 4.87V
15C 4.89V
13C 4.89V
13C 4.89V
13C 4.89V
13C 4.87V
13C 4.87V
15C 4.89V
13C 4.89V
13C 4.89V
13C 4.89V
15C 4.89V
13C 4.87V
13C 4.89V
15C 4.89V
15C 4.89V
23C 4.89V
15C 4.89V
15C 4.87V
15C 4.89V
13C 4.89V
13C 4.89V
23C 4.87V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.89V
13C 4.89V
23C 4.89V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.87V
15C 4.89V
23C 4.87V
15C 4.87V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.87V
15C 4.87V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.89V
15C 4.87V
15C 4.87V
15C 4.87V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.87V
15C 4.87V
15C 4.89V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.87V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.89V
15C 4.89V
15C 413C 4.89V
16C 4.87V
17C 4.87V
15C 4.87V
15C 4.87V
16C 4.87V
17C 4.87V
15C 4.89V
15C 4.89V
25C 4.89V
17C 4.89V
15C 4.87V
17C 4.89V
15C 4.87V
15C 4.87V
15C 4.87V
15C 4.89V
15C 4.89V
17C 4.89V
17C 4.87V
15C 4.87V
17C 4.87V
15C 4.87V
17C 4.87V
25C 4.87V
17C 4.87V
15C 4.89V
15C 4.87V
15C 4.87V
17C 4.87V
17C 4.87V
17C 4.87V
17C 4.89V
15C 4.87V
17C 4.89V
17C 4.87V
17C 4.89V
15C 4.89V
17C 4.87V
17C 4.87V
17C 4.87V
15C 4.87V
15C 4.87V
15C 4.87V
17C 4.87V
25C 4.87V
15C 4.89V
17C 4.87V
15C 4.89V
17C 4.89V
17C 4.87V
15C 4.89V
17C 4.87V
17C 4.87V
15C 4.87V
17C 4.87V
17C 4.89V
17C 4.87V
17C 4.89V
15C 4.87V
17C 4.87V
17C 4.87V
15C 4.87V
17C 4.89V
15C 4.89V
17C 4.89V
17C 4.89V
17C 4.89V
17C 4.87V
17C 4.87V
17C 4.89V
17C 4.87V
17C 4.89V
15C 4.89V
15C 4.89V
17C 4.87V
25C 4.85V
17C 4.87V
17C 4.87V
17C 4.87V
15C 4.87V
15C 4.85V
15C 4.83V
19C 4.81V
17C 4.85V
17C 4.85V
15C 4.85V
15C 4.83V
17C 4.85V
15C 4.85V
15C 4.85V
17C 4.85V
15C 4.85V
15C 4.85V
15C 4.85V
15C 4.85V
17C 4.85V
15C 4.83V
15C 4.83V
15C 4.85V
17C 4.85V
15C 4.85V
15C 4.83V
15C 4.85V
17C 4.85V
19C 4.81V
15C 4.83V
17C 4.85V
19C 4.81V
19C 4.81V
19C 4.81V
19C 4.79V
19C 4.81V
19C 4.81V
19C 4.81V
19C 4.81V
15C 4.83V
19C 4.81V
19C 4.81V
19C 4.81V
19C 4.79V
19C 4.81V
19C 4.81V
19C 4.81V
19C 4.79V
21C 4.77V
19C 4.81V
Using gedit window for Digispark output display is not perfect, but works for easy cases.
Hermann.
#include <TinyWireM.h>
#include "DigiKeyboard.h"
void setup()
{
TinyWireM.begin(); // initialize I2C lib
DigiKeyboard.sendKeyStroke(0);
delay(1000);
}
void loop()
{
//read and convert the voltage to a decimal
long voltage = readVcc();
double decimalVoltage = doubleMap(double(voltage),0,6000,0,6);
DigiKeyboard.print(get_temp());
DigiKeyboard.print("C ");
DigiKeyboard.print(decimalVoltage);
DigiKeyboard.println("V ");
// delay so this updates approximately 4 times per second
delay(250);
}
double doubleMap(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref 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;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
int get_temp() {
analogReference(INTERNAL1V1);
int raw = analogRead(A0+15);
/* Original code used a 13 deg adjustment. But based on my results, I didn't seem to need it. */
// raw -= 13; // raw adjust = kelvin //this value is used to calibrate to your chip
int in_c = raw - 273; // celcius
analogReference(DEFAULT);
return in_c;
}