Using the code to decipher physical pins

Hi there!

Two questions, please.

First question is this: by looking at the code, how can I figure out which physical pin is referenced?

I have some code and a PCB containing at ATTiny84a QFN.

I want to mock it up on a breadboard using an ATTiny84a DIP.

The pins on both of those chips are laid out differently.

Here is the code:

#define PIN_A 0
#define PIN_B 2
#define PIN_C 3
#define PIN_D 1
#define PIN_EN 10

#define PIN_RAND 4

#define PIN_SWITCH 6  // AKA MOSI

Here are the pinouts

So, which physical pin on the DIP chip is PIN_A? Is it 2, is it 13, is it 5? (And pin 6 is not MOSI?!)

Second question:

If the code says

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

Was the code written for a specific chip, or does the pin get sorted out when the code is compiled?

If the pin in the blink sketch is 13, what happens if I use an ATTiny85 which only has 8 pins?

Thank you.

PS I don't know why I am having such a hard time grasping the concept of pins.

When you select a target platform (a UNO, MEGA, MICRO, or an ESP32 of a certain brand, ...) in the IDE that happens to select a specific variant description file pins_arduino.h for that hardware (a set of macros and values) which gets included and compiled with your code.

Have a look at one of those files, it’s self explaining.

J-M-L:
Have a look at one of those files, it’s self explaining.

I'm still a beginner, but I had a look at some of those files you linked and it sort of makes sense.

So if I am writing a program, I need to know what chip I want to use, obviously, since I will need to know its pinout so I can write the code properly.

So the blink sketch was written originally for the ATMega328 (or was it the ATMega128?). And if I upload it to a ATTiny85, one of the files you referenced will compile the code in such a way that the ATTiny85 can use it.

Thanks.

No.

The blink sketch was written originally for an Arduino. If you COMPILE it for an ATTiny85, one of the files referenced will compile the code in such a way that the ATTiny85 can use it. If you COMPILE it for an Uno (ATMega328p), one of the files referenced will compile the code in such a way that the Uno can use it.

Blink (modern version) is using a pin named LED_BUILTIN

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

If you are using a MEGA, the definition is here and will be on pin 13 (which then is mapped to the right hardware pin).

If you are compiling for arduino-esp32 there are many variants, if you take the d32 you’ll see in its variants/d32/pins_arduino.h that it’s on pin 5 and if you take the doitESP32devkitV1, it’s on pin 2.

If you compile for ATTiny X5, then it’s on pin 1

So you need to understand what platform variant was matching your initial code and go dig in that file (or use a sketch running on that platform to print things out using the macros) to find the mapping.

You need a different diagram, one which also shows the Arduino pin numbers (A0,D1 etc) for the physical packages. Look here and scroll down to the pictures, then it will be clear.

See the colour code to identify the Arduino pins. You’ll note that there is no correspondence between the package pin numbers and the Arduino pin numbers.

edit

If you use a different package than the one illustrated, just match up the port pins.

6v6gt:
See the colour code to identify the Arduino pins.

Thank you, this is exactly what I needed.