Analog pin numbers vs Digital pin numbers

I've noticed in the ATtiny167 that I need to use different pin numbers for analogRead vs digitalRead. Is this intentional or a mistake? Shouldn't they be the same?

For an Uno, don't all of these read the same pin?

analogRead(A3);
digitalRead(A3);
analogRead(17);
digitalRead(17);

I think that the "standard" core corrects for this, and allows either analog "channel" numbers (0-6) or digital pin numbers (14-19) to be used by analogRead(). This code might be missing from the 167 core.

I guess my question is more along the lines of: what do we want?

There is something called analog_pin_to_channel that doesn't appear to be implemented in the ATtiny core. It's not a big deal. I can manage it. Digital pin 15 is Analog pin 10, even though it's physically the same pin. Okay.

The whole notion of Arduino pin mapping is, I presume, to make things easier and more portable. While working with this ATtiny I know I am going to be fixing bugs that I encounter. So I need some way to know if it's a bug or an intended simplification. Maybe it's up to me?

With pin mapping, it is up to you. For example, I have an ATtiny84 board that maps five physical pins to digital pins and has no analog pin mapping.

The analog pin numbers are usually just the multiplexer channel number (possibly with an offset) (e.g. ADC0 is analog pin zero, ADC1 is analog pin 1). That makes analogRead extremely simple. Luckily the m328 channel numbers coincide nicely with physical location / digital pin numbers. That is rarely (never?) true with ATtiny processors.

However, you are certainly free to add a bit of code to analogRead so the analog pin numbers can be translated. The code would be nearly identical to the code in the digital functions.

I see. I'll just leave it as is. I only asked because I'm expecting to find a few bugs in this core. I am using one pin for both analog and digital at the same time and it seemed weird to have two pin definitions in my sketch for the same physical pin.

I am using one pin for both analog and digital...

The usual approach is to have the A* symbols map to the same physical pin. For example, on the Uno, A0 maps to digital pin 14 / analog pin 0; the same physical pin.

If the core you are using does not provide something similar I would consider that a bug.

It doesn't. It's A/D channel 9 but the symbol A9 doesn't even compile.

The way it's mapped now is also twisted in order to insure that SS,MOSI,MISO,SCK are 10,11,12,13. I guess that makes some sketches work without editing the pin numbers but it sure buggers the pattern. I sometimes get cross-eyed looking at it.

It's A/D channel 9 but the symbol A9 doesn't even compile.

Seems buggy to me.

The way it's mapped now is also twisted in order to insure that SS,MOSI,MISO,SCK are 10,11,12,13. I guess that makes some sketches work without editing the pin numbers but it sure buggers the pattern.

That problem is traditionally solved by including and using four constants with the pin mapping...
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/variants/standard/pins_arduino.h#L38-L41

This is the current mapping:

PA0     ADC0    D0		
PA1     ADC1    D1
PA2     ADC2    D12     MISO
PA3     ADC3    D3
PA4     ADC4    D11     MOSI
PA5     ADC5    D13     SCK
PA6     ADC6    D10     SS
PA7     ADC7    D14

PB0             D4
PB1             D5
PB2             D6
PB3             D7
PB4             D8
PB5     ADC8    D9
PB6     ADC9    D2
PB7     ADC10   D15    RESET

A0=16 ... A7=23
A8-A10 not defined

I think this is a little more logical:

PA0     ADC0    D0	
PA1     ADC1    D1
PA2     ADC2    D2      MISO
PA3     ADC3    D3
PA4     ADC4    D4      MOSI
PA5     ADC5    D5      SCK
PA6     ADC6    D6      SS
PA7     ADC7    D7

PB0             D8
PB1             D9
PB2             D10
PB3             D11
PB4             D12
PB5     ADC8    D13
PB6     ADC9    D14
PB7     ADC10   D15     RESET

A0=0 ... A7=7
A8=13 .. A10=15

But is it up to me?

Does anyone else even use the ATtiny167? :slight_smile:

Not I.