This is an old revision of the document!
Digital 2 is analog (ADC channel) 1
Digital 3 is analog (ADC channel) 3 Digital 4 is analog (ADC channel) 2 Digital 5 is analog (ADC channel) 0
Internal Temperature Sensor
Add this code to your sketch (outside of the loop and setup) and then call get_temp to get the temperature in C. Remember the internal temperature sensor is subject to the heat of the chip and not pre-calibrated.
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;
}
**Get operating voltage without using a pin*
The following will display on the LCD the current voltage coming into the Digispark without having to use a pin to measure it!
#include <TinyWireM.h> // I2C Master lib for ATTinys which use USI - comment this out to use with standard arduinos
#include <LiquidCrystal_I2C.h> // for LCD w/ GPIO MODIFIED for the ATtiny85
LiquidCrystal_I2C lcd(0x27,16,2); // set address (0x27 is the address of the Digispark LCD modules) & 16 chars / 2 lines
void setup()
{
TinyWireM.begin(); // initialize I2C lib
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on the backlight
// Print startup message to the LCD
lcd.setCursor(0,0); lcd.print("Powered by");
lcd.setCursor(0,1); lcd.print("Digispark!");
//delay so we can see the message
delay(1000);
}
void loop()
{
//read and convert the voltage to a decimal
long voltage = readVcc();
double decimalVoltage = doubleMap(double(voltage),0,6000,0,6);
//update the LCD
lcd.clear();
lcd.setCursor(0,0); lcd.print("Voltage:");
lcd.setCursor(9,0); lcd.print(decimalVoltage);
// 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<< | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}