Mega 2560 - Wrong pins

Hi, I have an interesting problem with my Arduino Mega, it seems the hardware definitions for pins point to the wrong place.
For example, trying to read or write to pins 0, 1, 2, 3 actually points to pins 21, 20, 19, 18 (in this order), A0 points to A14...
Direct port manipulation works perfectly for reading and writing on all pins so I really don't understand this issue.

This board has had a different firmware at some point, Hoodloader, but it was having this exact problem so I used Arduino as ISP to burn the original firmware through the IDE so it has that now, but the problem persists.

Thanks for any tips.

sketch, picture ?

There are port numbers , physical PIN numbers and Arduino pin numbers .
Need some more info - compiling to correct board , code, connections etc

If I put “ pinMode( 4, OUTPUT) ;

Then the pin marked as 4 on the PCB is “activated” for writing to.

Are you trying to use physical pin numbers?!?

Pin 1 of the chip (PG5) is Arduino pin 4
Pin 2 of the chip (PE0) is Arduino pin 0
Pin 3 of the chip (PE1) is Arduino pin 1

Ok, let's take this code which is supposed to toggle pin 13, the built-in LED.

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop() {
  digitalWrite(13, !digitalRead(13));
  delay(1000);
}

Instead of toggling pin 13 (PB7) , it is toggling pin 11 (PB5), as verified with a logic analyzer, and of course the built-in LED doesn't do anything.

void setup() {
  DDRB |= B10000000;
  PORTB &= B00000000;
}

void loop() {
  PORTB ^= B10000000;
  delay(1000);
}

This, on the other hand, with direct port manipulation, toggles the LED as expected.

The same is true for reading GPIO with PINx, regular digitalRead points to a different pin than printed on the silkscreen.

image
Board info

edit: also tried changing "Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h" with the one from GitHub but nothing is different.

That is very strange. It isn't likely a hardware error since Direct Register access works expected. My best guess would be something has gone very wrong with your Arduino install. I would re-install.

Phew, that fixed it. Perhaps it was using the pin definitions from another board =))

Thank you

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