element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum What are the Arduino IDE pin assignments?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 12 replies
  • Answers 8 answers
  • Subscribers 392 subscribers
  • Views 8053 views
  • Users 0 members are here
  • pin
  • ide
  • assignment
  • mapping
Related

What are the Arduino IDE pin assignments?

Former Member
Former Member over 11 years ago

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?

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 7 years ago in reply to sbkenn +2 suggested
    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…
  • Former Member
    Former Member over 11 years ago in reply to ntewinkel +1 suggested
    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…
  • ntewinkel
    ntewinkel over 11 years ago in reply to Former Member +1 suggested
    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…
Parents
  • sbkenn
    0 sbkenn over 7 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 7 years ago in reply to sbkenn

    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:

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

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • neilk
    0 neilk over 7 years ago in reply to shabaz

    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!

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • sbkenn
    0 sbkenn over 7 years ago in reply to neilk

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • sbkenn
    0 sbkenn over 7 years ago in reply to neilk

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube