I put the analogRead routine in a for loop because I've read that one measurement can give an error.
I've connected an led to physical pin 6 and a 10k resistor between physical pin 7 and ground, i.e. thinking that the Attiny85 will measure 0 volts but it measures something else since the led on pin 6 lights up in the void loop().
I added analogReference(INTERNAL) to the code but it did not solve the problem.
To use a pin as an analog input, you simply use the analogRead() function, referencing the respective analog pin number.
(As you may have noticed, you didn’t need to specify pinMode() when using analogRead(). When a pin can be used as either analog or digital, analog is the default.)
I actually needed this for a void setup() routine that would check the battery voltage on start-up. I want the led to blink if Vcc is below 3.3/ 3.5V. I don't know what the analogRead() value for that would be assuming that the Attiny uses the internal 1.1V as reference?
What will be the maximum voltage from the battery? You should use a voltage divider to bring that voltage down to below ATtiny supply voltage and then work out what reading you will get if you scale your threshold 3.3V by the same factor. If the scaled down 3.3V exceeds the 1.1V reference, you will need to recalculate the voltage divider until it is below 1.1V.
Hackscribble:
What will be the maximum voltage from the battery?
I want to use 3 x 1.5AA batteries, i.e. 4.5-5V max, so it falls below the 5.5V max input given in the datasheet. This way I won't need a voltage divider. Or do I need it anyway if I want to calculate the input voltage?
btw, does the internal reference voltage change with decreasing Vcc?
p.s I wish the tiny85 had serial communication so I could use the serial monitor to see what value analogRead() gives when Vcc falls below 3.3V.
You will still need a voltage divider to bring down the 3.3V to less than the reference voltage. Otherwise you will get a maximum ADC reading for all voltages over the reference.
Assuming that the internal reference works like the one on the Uno, then it will not reduce with supply voltage.
You could run SoftwareSerial to get data out if the ATtiny.