The ATmega2560 is the same AVR family microcontroller as the ATmega328P. The code should do the same.
However, the ATmega328P can deal with drops and peaks in the voltage and wrong use of the pins and can give peak currents with a pin beyond of what is in the datasheet (to some limit). The ATmega2560 can deal with nothing.
What is the 4.3V ? The ATmega2560 must have at least 4.5V, but try to bring it between 4.8 and 5.1V.
Turns out the pin assignment was wrong, and I don't know how my physical pins don't look like what they should be on the atmeg2560!
I ended up changing the status of each pin and check all the pins on the board to find what it is assigned to:
software
physical
0
4
1
3
2
6
3
7
4
1
5
5
7
16
8
17
9
18
10
23
11
24
12
25
13
26
14
64
15
63
16
13
17
-
18
46
19
-
20
44
21
43
22
78
23
-
24
76
25
75
26
74
27
73
28
72
29
71
30
60
31
59
32
58
33
57
34
56
35
55
36
54
37
53
38
26
39
-
40
52
41
51
42
42
43
41
44
40
45
39
46
38
47
37
48
36
49
35
50
22
51
21
52
20
53
19
54
97
55
96
56
95
57
94
58
93
59
92
60
91
61
90
62
89
63
88
64
87
65
86
66
85
67
84
68
83
69
82
If someone can explain how is this happening I would appreciate it!
Although the physical pin number is important when designing a PCB, once you start thinking about firmware, you should focus focus on the port and bit of the I/O pins as this is the only thing that is relevant.
Arbitrary integers are assigned to each of the I/O pins and these integers are the pin numbers used in the Arduino core API functions. It is essential to understand that these integers are only arbitrary identifiers and don't necessarily have any correlation at all to physical pin number or port/bit.
You can see the Arduino pin number mapping for the Arduino Mega 2560 board here in its core variant file:
Here is the port mapping:
And here is the mapping for the pin's bit within the mapped port:
The Arduino pin number is the index of the arrays.
So for example, the 0th element of the array (Arduino pin 0) is PORTE:
and bit 0:
If you look at the pinout in the ATmega2560 datasheet for the IC package you are using, you can see which physical pin is PE0.