Leonardo/Pro Micro pin numbers mismatched

I am working on making some custom 12-key macro keypads based off of the Arduino Leonardo/Pro Micro architecture. I ordered some custom PCBs, and they have finally arrived. I used an Arduino Uno as an ISP to flash the bootloader onto them, and besides me having flipped the USB data pins (which I have now fixed), everything seems to be working fine.

I have programmed a blink sketch onto them, and I have tested them with the Arduino IDE. Everything is working as expected except the pin numbers. My circuit uses 7 pins for the button matrix, 3 output, 4 input with pullup resistors, and, I selected them so they would be pins 2-8 in the IDE. The matrix is connected to pins 19, 18, 25, 31, 27, 1, and 28 respectively on the microcontroller, and those seem to line up with Arduino breakout pins 2-8 from what I have looked up. That said, the numbers 2-8 have not worked when I try to set the states of these pins.

The part that is confusing me is that I tried some other numbers, and I have gotten the numbers 24, 25, 26, and 27 to work in the IDE, mapping to the pins I thought would be numbered 4, 6, 8, and 9 (which will be used for LEDs on my board) respectively. Does anyone know what might be going on?

I would be happy to share more info, and I can provide the Gerber files (I don't have an accurate schematic), but seeing as this is already getting long and I don't know what information will be useful, I'll leave it at this for now. If you would like more info, please ask, and I'll try to get it to you as soon as possible. Thank you so much for the help!

The pin mapping is listed here:
https://github.com/arduino/ArduinoCore-avr/blob/1.8.3/variants/leonardo/pins_arduino.h#L275-L311

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
	_BV(2), // D0 - PD2
	_BV(3),	// D1 - PD3
	_BV(1), // D2 - PD1
	_BV(0),	// D3 - PD0
	_BV(4),	// D4 - PD4
	_BV(6), // D5 - PC6
	_BV(7), // D6 - PD7
	_BV(6), // D7 - PE6
	
	_BV(4), // D8 - PB4
	_BV(5),	// D9 - PB5
	_BV(6), // D10 - PB6
	_BV(7),	// D11 - PB7
	_BV(6), // D12 - PD6
	_BV(7), // D13 - PC7
	
	_BV(3),	// D14 - MISO - PB3
	_BV(1),	// D15 - SCK - PB1
	_BV(2),	// D16 - MOSI - PB2
	_BV(0),	// D17 - SS - PB0
	
	_BV(7),	// D18 - A0 - PF7
	_BV(6), // D19 - A1 - PF6
	_BV(5), // D20 - A2 - PF5
	_BV(4), // D21 - A3 - PF4
	_BV(1), // D22 - A4 - PF1
	_BV(0), // D23 - A5 - PF0
	
	_BV(4), // D24 / D4 - A6 - PD4
	_BV(7), // D25 / D6 - A7 - PD7
	_BV(4), // D26 / D8 - A8 - PB4
	_BV(5), // D27 / D9 - A9 - PB5
	_BV(6), // D28 / D10 - A10 - PB6
	_BV(6), // D29 / D12 - A11 - PD6
	_BV(5), // D30 / TX Led - PD5
};

You can check the datasheet for the package of your ATmega32U4 to match the physical pin numbers against the port/bit notations listed there.

1 Like

Thank you so much! This worked. I really appreciate it! Have a great day.

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

Note that if you create your own "board" type, you can re-map the pins however you want (note that some functions (pwm, I2C, serial, etc) follow the internal port/bits, so if you move those pins the functions will move too.)

That's really interesting! I never knew you could do that, and since I am possibly going to make another version in the future, I'll look into some more details. Thank you so much for the tip!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.