Attiny85 pinout question - analog pins

So I connected my analog sensors to pins 3 and 4, by the numbering used in a pinout chart (the two pins between GND and Reset).

analogRead(3) returns the expected result, reading pin 3.
analogRead(4) returns a number very close to 0, instead of reading pin 4.

analogRead(2), however, returns the expected value from pin 4.

Why is this? Why is there a different pin numbering scheme for analogRead() vs digitalRead()/etc?

How do I unambiguously refer to a given pin, for example, for setting pinMode()? (Put another way, since two pins are referred to as pin 2, how do I know which numbering scheme a given built-in function uses?

This is indeed confusing.

Take a look at the ATtiny2/4/85 datasheet, figure 1-1 (Pinout). Each pin has several names. In those names, the "x" in PBx is the number referred to by pinMode() and digitalRead/Write(); However, the number in analogRead() is the x in ADCx.

So, when you wrote "analogRead(2)", it read from ADC2, which is PB4. PB3 happens to be ADC3 so no problem there, and ADC only goes from 0 up to 3, so attempting to read "ADC4" doesn't return anything.

Great, thanks a lot.

igendel:
So, when you wrote "analogRead(2)", it read from ADC2, which is PB4. PB3 happens to be ADC3 so no problem there, and ADC only goes from 0 up to 3, so attempting to read "ADC4" doesn't return anything.

Isn't ADC4 the internal temperature sensor or does the core not implement that?

evildave_666:
Isn't ADC4 the internal temperature sensor or does the core not implement that?

I re-checked and indeed, the temp sensor is connected to the ADC as "ADC4" - but the access logic is different (in the selection register MUX[3:0} it's not 0100). I doubt it was implemented in the Arduino analogRead() - I should check that too sometime :slight_smile:

On my ATTiny85, analogRead(4) was returning 2 - so yeah, doesn't look like it could be a temperature sensor.

I peeked at the source code of analogRead() in wiring_analog.c, in the Arduino core libraries and found this:

	// set the analog reference (high two bits of ADMUX) and select the
	// channel (low 4 bits).  this also sets ADLAR (left-adjust result)
	// to 0 (the default).
#if defined(ADMUX)
	ADMUX = (analog_reference << 6) | (pin & 0x07);
#endif

This seems like the relevant piece code for the ATtiny, so as I thought it can't access the temp sensor (it will attempt to read channel 0100 which is undefined, instead of the sensor at 1111).

DrAzzy:
...

How do I unambiguously refer to a given pin, for example, for setting pinMode()? (Put another way, since two pins are referred to as pin 2, how do I know which numbering scheme a given built-in function uses?

FYI

The attiny85 is the same chip used in Trinket and Digispark. Lots of pre-written stuff out there with examples and libs.

Here is a t85 using a thermistor & low-power options.
http://www.hackster.io/rayburne/hot-yet

Ray