10

If I search Google for "arduino nano pin numbers", all results show that the pin number of D2 is 5. But that did not work. I searched for some sample codes, and they were 3 for D3 and 5 for D5, etc. So, in codes, the pin number for Dn is n. Then what are the numbers in these diagrams?

enter image description here

1
  • 1
    are you referring to the physical pin numbers of the module? ... they start at 1 and wrap around
    – jsotola
    Sep 20 at 1:45
9

Every chip datasheet (and also the Arduino boards) uses 2 different ways to refer to the pins:

  • Counting the physical pins in the order as they appear on the chosen package of the chip
  • Referring to the pins by their function

The first way is only for referring to specific pin positions from the outside, for example when you are building the actual circuit. This also includes special pins like Vcc, GND and Reset. Those pins cannot be referenced by the code (wouldn't make much sense). Also the microcontroller on the inside of its package doesn't know in which package it got stuck by the manufacturer. Each package has a different pin order and form factor. So this counting only makes sense in the context of placing this specific package of the chip into a circuit (like where is Vcc, where ground and where do I need to connect to that specific digital pin), but makes no sense to use for the code inside the controller.

For referring to pins from inside the controller (aka in the code) we use the function of the pins. On an Arduino you have mainly digital and analog pins, which can be controlled. So for the convenience of beginners (which are targeted by the Arduino framework) the digital and analog pins are just numbered. Those are the numbers, that are normally written only the Arduino board. So D2 means the third digital pin inside the Arduino framework. A0 is the first analog pin. Where that pin is physically located on the board isn't interesting for the code, nore does the code know the actual location. For the code its just the third digital output hardware and so on.

TL;DR: For building the circuit we must refer to pin positions (like where is Vcc, where is ground) to connect the chip/microcontroller correctly. But the internal code doesn't know, where the physical pins are located and the actual location can also depend on the packaging. So internally we use a different counting system based on the pin function. The Arduino way of counting the digital and analog pins just hides away complexity for the beginner. As reference the numbers are also printed on the Arduino board itself.


In case you want to go deeper into the rabbit hole:

Actually the microcontroller has different hardware for different functions incorporated. For example: The analog pins can be used by the ADC (Analog to Digital Converter) to measure an analog voltage. But they also each have digital input and output hardware, just like any other digital pin. Most pins of a microcontroller are multi purpose. The code has to decide, what hardware gets to be connected to the respective pins. When I use pinMode(2, OUTPUT) I tell the microcontroller to connect the third digital output hardware to its respective physical pin. When I use analogRead(A1) I tell it to connect the second analog channel of the ADC to its respective pin and read an analog voltage through the ADC.

That means, that the code only knows of the function hardware and that it can be connected to their respective pins (whichever these pins are on the outside world). The numbering in digital and analog pins is only an additional abstraction layer to hide the complexity from beginners. Actually the code controls the hardware functions via Special Function Registers (SFR). These are special pieces of memory, which are physically hardwired to the respective hardware in the microcontroller. For the digital output hardware the pins are sorted into groups of up to 8 pins. These groups are called Ports, and are numbered with characters. When you look at this pinout of the Arduino Nano, you see, that the Arduino pin D2 is also named PD2, which means PortD third pin (counting from zero up). Arduino pin D8 is also named PB0, so PortB first pin. Functions like pinMode(), digitalWrite() and digitalRead() all just manipulate the SFRs for the wanted port to set the respective pin. Refer to the Documentation on Port Manipulation for learning how to do it yourself. For beginners it is just easier to use the markings on the board - the Arduino pin number - instead of bearing with the ports and their SFRs directly.

2
  • 1
    Might be clearer to tell all four levels at the start: 1) The pin number of the breakout board, which are numbered 1-30 here 2) The Arduino name for the pin, such as D3 3) The microcontroller datasheet name for the pin, such as PA4 4) The microcontroller package pin number
    – jpa
    Sep 20 at 12:21
  • The additional abstraction is not just to make life easier for beginners. As @RedGrittyBrick notes in their answer, it makes the code more portable. To a lesser degree, it's also makes the code more readable, though some may not agree. And if you're using one of the commercial boards where the pins are marked with the Arduino labels, it's certainly easier to get the wires on the right pin.
    – Llaves
    Sep 20 at 17:10
6

There are at least four ways of referring to pins on any Arduino board:

  1. Board-order: Simple numeric order (1,2,3 ...) of physical location of pins on printed circuit board anticlockwise from the USB end (or some arbitrary end)
  2. Arduino-name: Names used in Arduino source code and/or PCB silk-screen annotation (D1,D2 ... A0,A1 ...).
  3. IC-order: Simple numeric order of corresponding pin on IC according to numbering used on manufacturers data sheet.
  4. IC Function name: Names for functions available on pin (RESET, VCC, INT0, PB0,PB1,PB2 ...) as described in data sheet for IC.

For example:

Nano PCB Pin# Arduino Name ATMega328 TQFP Pin# ATMega328 DIP Pin# ATMega328 function
1 D13 17 19 PB5, SCK, PCINT5
2 3V3 N/A N/A N/A
3 REF 20 21 AREF
4 A0 23 23 PC0, ADC0, PCINT8
5 A1 24 24 PC1, ADC1, PCINT9
etc

There are lots of different Arduino and Arduino-like boards that use the ATMega328, some use different ATMega328 packages (DIP, TQFP, etc) with different pin arrangements. Many arrange the physical PCB pins differently. This is why there isn't a fixed one to one relationship and it is partly why the Arduino IDE provides for it's own simplified naming that it attempts to apply consistently and portably across a variety of different microcontrollers.


Nano Pinout by PigHixx Key by PigHixx

(c) PigHixx w/o permission.

1

Why are the pin numbers of the diagrams and of the code different?

Because the Arduino designers chose to re-use the name "pin" for the chip's programmable signal lines, despite it's more common and already existing use to mean any of the protruding external connections to the device.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.