PortD behaving strangely on duemilanove ATMEGA328p


I'm driving an LED on my board. The pin is connected to a 1k resistor, with an LED in series going to ground. I can make the on board LED blink (which is on pin 13, PD7) and I get can my own LED circuit to blink on on pin 12 (PD6), but I can not get it to blink when connected to pin 2 (PD0).

Where am I going wrong? The code I'm using is listed below.


void setup() { pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(2, OUTPUT); }

void loop() { digitalWrite(13, HIGH); // set the LED on digitalWrite(12, HIGH); // set the LED on digitalWrite(2, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off digitalWrite(12, LOW); // set the LED off digitalWrite(2, LOW); // set the LED off delay(1000); // wait for a second }

Code looks fine - I'd suggest a wiring error. Got a picture or anything?

DIP pin 2 is PD0, but Digital Pin 2 is PD2 and that’s DIP pin 4.

So that's odd. When I specify

pinMode(2, OUTPUT);

The value 2 does not refer to a the physical pin number two on the chip? I did not see that in the documentation. Or maybe I'm missing something about your post.



It really cant be the circuit. The wire going to the LED is literally just getting plugged into the header of the board for pin 12, it works, I move it to pin 2 and it doesnt. Thanks though.


Oh sorry people. Runaway_pancake helped me figure this out. I looked at the code in:


and it shows the mapping of digital pins to the DIP pins on the physical device.

Once you know this, the schematic:


also makes more sense. I think this is going to create a real headache when I start programming my other ATMEGA328p, which is a 32-pin TQFP package. The pin mappings on that thing is different than the DIP version of the 328p.

thanks, owen

I thought that digitalWrite (2, HIGH); would output a high on D2. Is that not the case?

Attached the DIP layout, they are where they are. (:

Port D b7-b0 are D7-D0
Port B b6-b0 are D13-D8
Port C b5-b0 are A5-A0

Digital pin2 is D2, b2 of Port D, but it’s on DIP pin4.

Did you programme up a 328p that you’re using on a breadboard or something?


@CrossRoads. Yes, your statement is correct. I was assuming the command digitalWrite (2, HIGH); would refer to a physical pin, pin 2, on the chip. It doesn't. It references the port name.

@runaway_pancake. I am programming a Spark Fun ADNS2620 Mouse Sensor Evaluation Board:


It is basically an optical movement sensor. I'm programming it to detect movement and if it does it will throw a signal to the outside world. It has limited I/O, there is a header that connects to PD0 and PD1, and I was trying to get those pins to go high and low. I got it figured out now, arduino pin names are not the same thing as the physical pins on the atmega chip.

The sensor board is now driving a relay, and that's going to detect any movement as a extra level of safety for part of my laser project. See: nilno.com. Thanks again everyone.



Yes, your statement is correct. I was assuming the command digitalWrite (2, HIGH); would refer to a physical pin, pin 2, on the chip. It doesn't. It references the port name.

I think it is safe to say that nothing in the Arduino documentation will typically refer to anything on the physical chip. This is deliberate as the whole idea behind the Arduino is to get away from the technical aspects of the microprocessor as much as possible. The fact that the first eight digital pins do indeed correspond to the PortD pins is what may have thrown you off.