12 Replies Latest reply on Jan 17, 2018 5:45 PM by sbkenn

    What are the Arduino IDE pin assignments?


      I am going to write a program for an existing ATmega32 processor and was trying to understand the pin number assignments. As an example the blink program has the following code.


      // Pin 13 has an LED connected on most Arduino boards.

      // give it a name:

      int led = 13;


      But the key word is "MOST" and it is a pin number which has a mapping to a processor I/O pin. On the UNO the LED is on pin 19/PB5. I wanted to understand why this was 13 and wanted a way to use an more understandable translation from code to hardware. For example:


      // LED connected to PB5.

      // give it a name:

      int led = PB5;


      PB5 is defined in one of the iomXX.h files that depend on the processor.

      This would work if PB5 was mapped to the processor's pin used for port B bit 5, but it is not. It is assigned the value of 5.

      PA5, PB5, PC5 and PD5 are all assigned the value of 5.


      I analyzed the pins_arduino.h files to understand the pin mapping so I could define a value for the pins that would map to the processor's pins. For example on a ATmega32 the following definitions give variable names to the pins. This is not the pin on the microprocessor, but the pin number that the Arduino IDE uses to communicate with the functions like digitalWrite(8,HIGH) with set PinB0 high.


      static const uint8_t PinB0 = 8;

      static const uint8_t PinB1 = 9;

      static const uint8_t PinB2 = 10;

      static const uint8_t PinB3 = 11;

      static const uint8_t PinB4 = 12;

      static const uint8_t PinB5 = 13;

      static const uint8_t PinB6 = 14;

      static const uint8_t PinB7 = 15;


      Using these definitions I can write code that I use to wire up my LED.


      // LED connected to PinB5.

      // give it a name:

      int led = PinB5;


      So here is my question. I really wanted the variable name to be PB0, not PinB0 since that is the name the datasheet uses, but that name is already defined in iom32.h.

      What is the reason that PBx is defined in iom32.h?


      Is there some combination of the pin assignments in iom32.h that I should be using that give me the mapping to the pin number, or is there some documentation that defines the mapping?

        • Re: What are the Arduino IDE pin assignments?

          Hi Bill,


          The pins are marked on the board itself. When the sample says pin 13, that's shorthand for D13 = digital 13. When referencing Analogue pins you would use A0, A1, etc.


          Take a look at the picture of the Uno here and you'll see all the I/O ports are marked with numbers:



          More details about the Uno:



          Hope that helps




          1 of 1 people found this helpful
            • Re: What are the Arduino IDE pin assignments?

              Oops, sorry, I see I didn't read your question carefully enough!

              This diagram might help, in that it shows the mappings:



              Not sure if that's what you're looking for...




              1 of 1 people found this helpful
              • Re: What are the Arduino IDE pin assignments?

                Thanks for the fast response. Again let me ask the question (without all the support statements) with different words.


                It isn't about the UNO or any board, it is about the microprocessor and the datasheet. I have a board with a Atmega32 that is not an Arduino board.

                The robot motor pins_arduino.h does create definitions for some of the pins and that is how I have done it also, but I didn't understand why PB5 and PINB5 are already defined because these would have been my first 2 choices for variable names. I was forced to pick a less obvious name pinB5 which is too close to PINB5 and will cause errors that will be very difficult to debug since the compiler will be happy to use PINB5 = 5 instead of pinB5 = 13.


                1. Is there a variable defined that I can use for Arduino calls that relates to the datasheet that I can get from Atmel?

                           I think the answer to this is NO.


                2. The variable I would like as an example is PB5, however it is already defined as something else. Why? What is that definition used for?

                          I think the answer is "it is not used for anything"


                3. Is there some combination of variables already defined that I should be using? I.E. PB5, DDB5, PINB5 etc. None of these seem to lead to a datasheet.

                          Again I think the answer is NO.


                I am hoping someone can either verify my answers or tell me a way to write code that doesn't require a pin map to find the correct pin on the microprocessor.


                I would like to see this:

                     pinMode (PB5, OUTPUT);

                     digitalWrite(PB5, HIGH):

                Not this:

                     pinMode (13, OUTPUT);

                     digitalWrite(13, HIGH);

                1 of 1 people found this helpful
                  • Re: What are the Arduino IDE pin assignments?

                    Hi Bill,


                    I don't know, but I wonder if the other definitions are used for direct port manipulation: http://arduino.cc/en/Reference/PortManipulation

                    So it looks like PB5 can only be used when looking at port B, but that doesn't make sense to me either (like why call it PB5 if it's not specific to port B to begin with? call it pin5 then).

                    I see your point about PB5 - to me, from looking at all those pretty and promising pictures, it seems that PB5 references ONLY the one pin, so it makes no sense to give it a value that doesn't specifically apply to that pin on the chip.

                    And I too was surprised that the Arduino IDE wouldn't allow 13 to be freely interchangeable with PB5. This picture also shows it defined like that: Arduino - PinMapping168

                    And why can't I put D13 instead of 13?

                    Maybe this is a restriction in the Arduino IDE (designed to simplify) ? I know there are other IDEs available to program the chip directly, so maybe that's where the other definitions come into play.

                    In short, I have no answers, just more questions

                    I hope someone else can clarify this for us!



                    2 of 2 people found this helpful
                    • Re: What are the Arduino IDE pin assignments?

                      Just found this: http://forum.arduino.cc/index.php?topic=37067.0


                      Apparently using PORTB5 correctly identifies digital13 (and compiles in my IDE).


                      And I still don't know why any of that is supposed to make sense

                      2 of 2 people found this helpful
                        • Re: What are the Arduino IDE pin assignments?

                          Compiling and running are two different things. To compile, the syntax needs to be correct, to run the program needs to do the correct thing.

                          For example pinMode(13, OUTPUT) compiles and so does pinMode(PB5,OUTPUT), but the first changes digital output D13 and the second changes digital output D5 (because PB5 = 5);


                          The example at http://forum.arduino.cc/index.php?topic=37067.0 is a good example of the use of low level commands. The program write directly to the register and changes the bit.

                          Based on the comments by the author, they did this to create a I2C type of signal but the signal required is slightly different from I2C. The reason for low level commands would be timing. Using a digitalWrite command would create a lot of extra lines of code that need to be executed and may not be tolerated in communicating with this device.


                          First a few definitions used in the program - mostly from included files.

                          #define PORTB _SFR_IO8(0x05) from iom328p.h

                          #define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET) --- from sfr_defs.h

                               This defines the register's address. __SFR_OFFSET is 0x00 or 0x20 depending on the processor

                          In the table below from the Atmega328 processor, you can see that the PORTB register is at address 0x05

                          Atmel 328 Registers.jpg.

                          Now from the code example...

                          #define SETSCK1 PORTB |= (1<<PB5)          

                               1<<PB5 => shift 1 left 5 places => 0010 0000 (space in binary number for ease of reading)

                               |= => OR the register PORTB with the bits - sets the 6th bit


                          #define SETSCK0 PORTB &= ~(1<<PB5)

                               1<<PB5 => shift 1 left 5 places => 0010 0000 (space in binary number for ease of reading)

                              ~(1<<PB5)  => invert the bits => 1101 1111

                               &= => and the register PORTB with the bits - clearing the 6th bit


                          #define SCKOUTP DDRB |= (1<<DDB5)

                               DDRB is the Data Direction Register for port B defined as 0x04. DDB5 = 5.  Setting a bit to a 1 sets the pin to OUTPUT


                          Sorry if I vented too much, but hope it was a little educational.


                          Now back to my gripe..

                          PB5 = 5. It doesn't really have anything to do with port B. It is just bit 5 (but the B doesn't stand for BIT because there is a PC5 also).

                          OK. Now I see a little support for PB5, PC5 etc but they could have all been BIT5 then my problem wouldn't exist.

                          However, this example helps me in that it is only the coder using these definitions and not the libraries.

                          1 of 1 people found this helpful
                    • Re: What are the Arduino IDE pin assignments?

                      Try looking in “pins_arduino.h”


                      for your processor or variant

                      • Re: What are the Arduino IDE pin assignments?

                        Under hardware variants  look into robot motor folder

                        • Re: What are the Arduino IDE pin assignments?

                          I realist that this is an old thread, but I am having difficulty with the same things.  I have, as I suspect that the O.P. has, come from a career of digital hardware rather than programming or hobbyists.  I look at the chip datasheets, and work from there.  To me, the port references, and indeed, the apparent lack of direct access internal registers by address, is illogical.  Some compilers facilitate insertion of some assembly code within the C file, but Arduino doesn't seem to.  I need direct manipulation of port and register bits, and indeed, the port mapping seems to be faulty anyway.  In my case, clearing an interrupt flag with bitClear (TIFR3, 1); doesn't clear the interrupt flag for timer match with OCR3A.  I also need to avoid all the overhead that surrounds digitalwrite, and use bitSet and bitClear for an output pin, but haven't found how to do it.  bitSet (PORTE, 5); compiles, but the hardware doesn't do what I expected, which is to set PE5 as it comes out of the chip.

                            • Re: What are the Arduino IDE pin assignments?

                              Hi Shane,


                              As I understand (been a while since I tried Arduino, so this information is just general), you can directly ignore nearly all Arduino functions (you'll still need the usual starting function) if desired, and gain as much control as desired. To that end, you don't need to use bitSet/bitClear. You can directly write to addresses using C pointers with zero functions. There is no memory management, so nothing stopping that.


                              So, to write to port E and set pin 5 high, it would be something like PORTE |= 0x20; (You'd have to set up the data direction etc in advance first - also I'm assuming PORTE has a meaningful read value - not necessarily the case with all microcontrollers - if it isn't meaningful then you'll need to store in a variable the current contents for the entire port). Also, the bitSet/bitClear that you're using may expand out to the same thing if they are #defines - I have no idea.


                              Anyway, when you directly manipulate like this, the compiler ought to implement it in the most efficient way possible - you'd have to check that the Arduino gcc is configured for such, or if there is any way to tweak it. That might not be possible. Arduino is designed to be easy to use with little options. For advanced users seeking code or speed optimizations, there are non-Arduino environments.


                              Anyway, some additional info on port manipulation using the direct method with the Arduino environment is described here along with recommendations/caveats:


                              1 of 1 people found this helpful
                                • Re: What are the Arduino IDE pin assignments?

                                  Thanks shabaz


                                  In my relatively few years of Arduino programming, I've sometimes wondered if I could justify using direct port manipulation and then, having decided probably not, moved rapidly.


                                  Having read:    https://www.arduino.cc/en/Reference/PortManipulation


                                  I will never, ever wonder again! The answer for me is clearly NO!



                                  2 of 2 people found this helpful
                                    • Re: What are the Arduino IDE pin assignments?

                                      This seems to work, painlessly.  I need to set/clear an output bit, and clear an interrupt flag as quickly as possible.

                                      Port Manipulation

                                      Manipulating ports and pins with AVR code is faster than using Arduino digitalWrite() function. It is also possible to make two pins change states at exactly the same time. You can read about it  here.

                                      Setting Bits in Variables

                                      cbi and sbi are standard (AVR) methods for setting, or clearing, bits in PORT (and other) variables.

                                      You will find them in lots of AVR code, posted in the forum and elsewhere. Both need to be defined within an Arduino sketch, so you'll need to paste in the following #define headers at the top of a sketch.


                                      #define sbi(sfr,bit) (sfr)|= (1<<(bit))

                                      #define cbi(sfr,bit) (sfr)&= ~(1<<(bit))


                                      sfr can be either PORTx or PINx.  Both seem to work for output ports.  Or a register name TIFRx


                                      I have yet to determine how time efficient they are.