ATMEGA32U4 pin mapping

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mkrell007
 
Posts: 16
Joined: Thu May 02, 2013 11:16 am

ATMEGA32U4 pin mapping

Post by mkrell007 »

I've been working with the ATMEGA32U4 for a little over a month now after ordering the LED Belt kit. So far, I've learned a ton. I've got it up and running and now I've now begun to build further upon the basic sketches and am now adding additional functionality in the form of buttons to the project.

The quandary I am now encountering is that I have now checked quite a few of the references for the ATMEGA32U4 breakout board and cannot find an exact pinout diagram that maps to the actual pin numbers one would use in the code. I've got all of the docs on Adafruit from the product page (http://www.ladyada.net/products/atmega32u4breakout/); I've got the additional pinout posted a while back (http://www.adafruit.com/blog/2012/06/15 ... -breakout/); I've also fetch the Atmel documents and perused them (http://www.atmel.com/devices/atmega32u4 ... =documents).

What I am looking for, exactly, is essentially where I can find the integer values for each pin. Am I reading the documents incorrectly? Maybe these are staring right in front of me and I'm not seeing them. Example directly from sketch - this sets the dataPin and clockPin up. I've been considering moving these off the ISP6 interface to play around with the other inputs (and I'll be powering the LED strand with its own battery pack).

#if defined(USB_SERIAL) || defined(USB_SERIAL_ADAFRUIT)
// this is for teensyduino support
int dataPin = 2;
int clockPin = 1;
#else
// these are the pins we use for the LED belt kit using
// the Leonardo pinouts
int dataPin = 16;
int clockPin = 15;
#endif
LPD8806 strip = LPD8806(36, dataPin, clockPin);

Ultimate, in my current sketch I have soldered a button, pull down resister, etc, to the C7 pin and set the pin to 9 :
// Set up the switches
// Switch 1 - C7/ICP3/CLK0/OC4A on Atmega32u4 breakout board, maps to internal pin 9
const int button1Pin = 9; // the number of the pushbutton pin

This actually seems to work fine (although I may literally have just guessed right, or am observing some outcome unrelated to the actual switch value changing) and I can see the value change when the switch is flipped. But, the problem... I see in other examples, namely the GPS Dog Collar (http://learn.adafruit.com/gps-dog-collar/code), that pin 9 is actually mapped to a different pin, E.G. :

#define OLED_CS 9 //Atmega32u4 Breakout Pin B5

Is there a consistent way to figure out what numeric pin values map to the actual board? Is there a best practices approach? Or is it, literally, count the pins and 5V is pin 1, A/AREF is pin 2, F0/ADC0 is pin 3, etc.

Sorry for the newbie questions, but I'm now mapping in SDA and SCL for an I2C component, and digging through the docs it also appears those are also on pin 8 and pin 9, respectively. I can't quite figure this out, and there has to be a document somewhere or an obvious way to brute force this to figure it out. Alternatively, instead of typing to the integer value of the pin can I use the logical name (ICP3, etc) to specify the pin instead?

-Mike

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: ATMEGA32U4 pin mapping

Post by adafruit_support_rick »

mkrell007 wrote:Is there a consistent way to figure out what numeric pin values map to the actual board? Is there a best practices approach? Or is it, literally, count the pins and 5V is pin 1, A/AREF is pin 2, F0/ADC0 is pin 3, etc.
I think you are getting confused by the word "pin".
For programming purposes, pin numbering has nothing at all to do with the physical pins on the board. It is nothing at all like the pin numbering on an IC.

Pin numbers in the Arduino software are related to the I/O pins on an Arduino: Digital 0 through 13, Analog 0 through 5, etc. Those numbers have no relation whatsoever to the actual pin numbering on the Arduino microcontroller.

The picture you want is this one:
Image
The Arduino pin numbers are indicated inblue.

Don't be confused by the markings on the board itself: "D5" on the board is not Arduino "Digital 5". The markings on the board refer to the way Atmel identifies the I/O pins, so "D5" refers to Port D Bit 5, "B7" is Port B Bit 7, etc.

Also note that some pins in the picture have more than one blue pin number. That means you can use them as one or the other, either analog or digital.

User avatar
mkrell007
 
Posts: 16
Joined: Thu May 02, 2013 11:16 am

Re: ATMEGA32U4 pin mapping

Post by mkrell007 »

Thanks, that clears some things up. I continued reading this morning and pieced together that the physical 'pin' on the breakout board isn't the actual pin value to use in-code, so going back to the docs does help a bit, and your comment helped clear that up fully - I shouldn't use the breakout board's pin number, so to speak, it logically doesn't connect to the same pin on the Arduino. I now feel somewhat foolish for thinking that was how it might have worked.

I'm still trying to make a connection with regards to how to actually write code here that uses specific pin numbers, though. It looks like I should fall back to mapping directly to the Arduino pins for the code. So, the coding examples of this :

Example :
#define OLED_CS 9 //Atmega32u4 Breakout Pin B5

This indicates "9", and the rest of the example uses this. So, that maps to B5, which is Arduino pin 9.

With that logic, to define the SDA and SCL pins, I would use :
#define SDA 2 //Atmega32u4 Breakout Pin D1 => Arduino pin 2
#define SCL 3 //Atmega32u4 Breakout Pin D0 => Arduino pin 3

Then, the proper was to map my switch on C7 would be :
#define SW1 13 //Atmega32u4 Breakout Pin C7 => Arduino pin 13

But, then I start to get somewhat confused. Tracing C7 on the Adafruit ATMEGA32U4 to the Leonardo this maps to pin 32 on the physical chip. Checking the Atmel document, it confirms on the diagram that pin 32 maps to PC7 (ICP3/CLK0/OC4A). So, shouldn't my sketch then refer to this pin specifically? E.G., define pin 32 instead of pin 13?

I guess this all comes down to a basic misunderstanding I am having with regards to how to translate the physical pins on the chip to the pin values to use in the Arduino sketches.

-Mike

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: ATMEGA32U4 pin mapping

Post by adafruit_support_rick »

mkrell007 wrote:But, then I start to get somewhat confused. Tracing C7 on the Adafruit ATMEGA32U4 to the Leonardo this maps to pin 32 on the physical chip. Checking the Atmel document, it confirms on the diagram that pin 32 maps to PC7 (ICP3/CLK0/OC4A). So, shouldn't my sketch then refer to this pin specifically? E.G., define pin 32 instead of pin 13?
You're thinking too hard. Even Atmel doesn't do that. For example, you can get the ATmega328 in four different chip packages. two of the packages have 28 pins, and the other two have 32 pins. But they're all the same processor. This sort of thing is typical, and that's why Atmel talks in terms of ports instead of physical pins on the chip
Arduino takes it a step further and gets rid of the idea of ports as well. You can run arduino code on a half-dozen or more different processors. Each of those processors has a different number of ports, different names for those ports, and a different number of actual pins on the chip. Nobody would get anything done if they had to keep track of all of that when moving a sketch from a Uno to a Leonardo or to a Mega. The IDE does that for you - that's why you have to select a Board-Type.

User avatar
mkrell007
 
Posts: 16
Joined: Thu May 02, 2013 11:16 am

Re: ATMEGA32U4 pin mapping

Post by mkrell007 »

Hehe, I do tend to over-analyze and I was driving too deep. So the IDE takes care of this via the board selection and just does the appropriate assignments at compile time. That's good to know, but part of me would like to understand more granularly how this is being handled.

I'll play around with this, then, and try to figure this out. I guess that's why breadboarding is so important - code it, prototype, make sure it works, then solder.

Thanks, this gives me a working understanding - so, a basic pin associations workflow example. Again, this comes back to the C7 example.

#define SW1 13 //Atmega32u4 Breakout Pin C7 => Arduino pin 13

I come up with the value of "13" here, as this is digital input 13. With SDA/SCL, is it just as simple then? AKA, these map to pins 2 and 3, respectively?

As an aside, I modified my code a bit last night and retested this successfully with pin 13. Oddly enough, it functioned the same as when I referenced pin 9 in my initial code. And, the switch when it changed states properly changed values. That's really odd to me, but maybe I have something else jacked. I'll be cleaning up the circuit tonight to eliminate possible floating inputs and iron out all the switches, etc.

Thanks again for the feedback :). It is helping quite a bit!

-Mike

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: ATMEGA32U4 pin mapping

Post by adafruit_support_rick »

mkrell007 wrote:With SDA/SCL, is it just as simple then? AKA, these map to pins 2 and 3, respectively?
Yup. Although, if you're using the Wire library, you don't need to know that. The system takes care of all of that for you. You weren't planning to write your own I2C driver, were you?

User avatar
mkrell007
 
Posts: 16
Joined: Thu May 02, 2013 11:16 am

Re: ATMEGA32U4 pin mapping

Post by mkrell007 »

Oh no, not writing my own driver here. I'm sampling accelerometer data for a motion-based project. And, on this and the button issues I can say last night was a full-on success :). Got both the accelerometer and buttons soldered, coded, and tested with a reference sketch. i2c is working and I'm able to read the data. So the prototype hardware is about to get put into a case and ready for testing.

Thanks again for clearing this up :).

-Mike

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: ATMEGA32U4 pin mapping

Post by adafruit_support_rick »

Cool! :D

User avatar
smayhew
 
Posts: 1
Joined: Tue Sep 30, 2014 4:57 pm

Re: ATMEGA32U4 pin mapping

Post by smayhew »

If you are using the Teensy version of the Arduino IDE with board type "Atmega32u4 breakout" then the pins are mapped by name in the file hardware/teensy/cores/core_pins.h. Here's a synopsis:


////////////////////////////////////
// Teensy 2.0
////////////////////////////////////
#if defined(__AVR_ATmega32U4__)
#define CORE_NUM_TOTAL_PINS 25
#define CORE_NUM_DIGITAL 13
#define CORE_NUM_ANALOG 12
#define CORE_NUM_PWM 7
#define CORE_NUM_INTERRUPT 4
#define PIN_B0 0
#define PIN_B1 1
#define PIN_B2 2
#define PIN_B3 3
#define PIN_B7 4
#define PIN_D0 5
#define PIN_D1 6
#define PIN_D2 7
#define PIN_D3 8
#define PIN_C6 9
#define PIN_C7 10
#define PIN_D6 11
#define PIN_D7 12
#define PIN_B4 13
#define PIN_B5 14
#define PIN_B6 15
#define PIN_F7 16
#define PIN_F6 17
#define PIN_F5 18
#define PIN_F4 19
#define PIN_F1 20
#define PIN_F0 21
#define PIN_D4 22
#define PIN_D5 23
#define PIN_E6 24

These def's are included in your Sketch build (though magic in the java code in the IDE I guess)... So you can reference by name's that match the silkscreen on the board.

If you really want to know how deep the rabbit hole goes, look at pins_teensy.c in the same folder, the mapping to internal Atmega32u4's PORT B, C, D, E and F io ports may come visible... If so please share.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”