ATTINY85 Analog Read

I'm displaying the supply voltage of arduino UNO on a OLED screen. The UNO code is perectly working.

When I transfer this code to ATTINY85 the voltage readings are incorrect. How to show the correct voltage? (I transfered the code via burning a bootloader & program via an UNO ICSP)

int getVCC() {
   return min(11264/analogRead(12),99); 
}
voltage = getVCC();

	oled.drawChar(100,0,voltage/10+'0');
        oled.drawChar(106,0,'.');
        oled.drawChar(112,0,voltage%10+'0');
        oled.drawChar(118,0,'V');

Please show the full sketch and a schematic of the circuit in which the ATTiny85 is supposed to do its analog sampling.

This will answer several questions we may have, for instance how you're running the OLED from a uC that has no hardware I2C support (supposedly you're using some kind of soft-wire approach here?), but also how the analog input is wired (and from what kind of signal source) and what happens in the initial part of your sketch where everything is being set up.

In short: if you give complete information, you may get useful responses. Based on the little snippet you gave so far, I wouldn't know where to start.

Your post was MOVED to its current location as it is more suitable.

Thanks. I'm making an oximeter.It has done using UNO & it says how to transfer to attiny85. Everything works nicely except supply voltage....!!

All my questions and comments still stand. I can't do anything for you before you address those. Good luck.

1)It uses software I2C.
2)No specific ADC pin. It just calculates the internal 1.1V ref to get the VCC.
3Here is the schematic

Thanks for the follow up!

Please explain a little more. The internal 1.1V reference is fixed. You can't calculate anything about it; it's there and cannot be changed. You cannot measure the 1.1V internal reference, let alone use it to calculate Vcc.

I see no pin on the ATTiny85 that would read any analog signal.

I'm not sure what you're trying to do. If you are trying to measure Vcc, then you'll need a voltage divider to bring Vcc to below 1.1V and then measure that value with the 1.1V reference. You can then calculate Vcc in your code. Note that the ATTiny85 does not seem to have a Vcc input to its ADC input MUX as shown in the block diagram on page 123 of the datasheet.

So please explain a bit more what you're actually trying to measure.

It can with both uCs.

However, in standard Uno analogRead should not support this value.
(Take a constant and AND to limit to the pin number.)
But I think it was available in ATtinyCore.

1 Like

I'm trying to measure VCC (supply voltage to Attiny 85) without the traditional ADC pin.
See this thread & post 14 show the code.

Ok, but it won't say anything about Vcc. The only useful information measuring the internal reference would give is for calibration purposes. This doesn't seem to be what OP is trying to do (although I'm not yet sure what he IS traying to do...)

I don't think you can.
I'd do it the way I proposed: voltage divider on Vcc, then measure it using an ADC pin. So with an external connection to the uC.

You got it. But why only Attiny85 gives wrong figures

No, he just pointed out you can measure the internal 1.1V reference. That's not what you're trying to do. The table @chrisknightley posted shows very clear that the ATTiny85 cannot do what you're trying to do with it. It physically can't measure Vcc without an external connection.

Whether 1.1V is accurate is another story

If VCC fluctuates, you can calculate VCC back by measuring 1.1V with reference to VCC.
It is quite used as a means of measuring VCC without using any pins.
(In many cases, the individual difference of 1.1V is measured first, and the magic number is hard-coded.)

You'd still have to measure Vcc, since the internal reference is supposed to be quite stable even if Vcc fluctuates. To measure Vcc, the ADC will need an input from it. The ATTiny85 as you've shown doesn't have this connection internally, so it needs to be made externally. Since Vcc is higher than Vref, there will also need to be a voltage divider.

Am I missing something?

Rest of the world does nowadays.

Ok, then show how you've set up your ADC in your sketch. Which you still haven't posted, so I cannot see to what extent your sketch matches how the 'rest of the world' does it successfully.

e.g.
VREF=VCC and measure 1.1V
VCC is 5.0V -> 1.1V / 5.0V = 255 / 1024 (10-bit ADC)
VCC is 3.3V -> 1.1V / 3.3V = 341 / 1024 (10-bit ADC)

This means that VCC can be calculated back.

Ok, but then the ADC will have to be set up to use Vcc as Vref. As I said above: we'd need to see this sketch.

19 posts and OP still isn't specific on what he's trying to do and we need to guess at it. I'm out.

I think analogRead used VCC as VREF by default, but I strongly agrees that OP need all sketches to be provide.