Understanding the pinMode in the Hardware way...?

Can anyone explain me what is happening inside the Arduino Hardware when the command

pinMode(2,OUTPUT);

is executed?

Noob here Please explain ( :grin: ) using a simple circuit? if possible

Inside the Arduino's processor on each pin is a switch, it switches the pin between the output push pull drivers and the input latch.

books.org/wiki/Digital_Circuits/Latches

See section 14 of the datasheet for the electronics.
The code makes sure the appropriate registers are set - data direction register, pullup register, etc. - and then output is commanded high or low.
You can check that out in the code for digitalWrite.

Can anyone explain me what is happening inside the Arduino Hardware when the command
Code:

pinMode(2,OUTPUT);

is executed?

It would seem that all you need to know is that a digital pin is configured as an OUTPUT as opposed to an
INPUT, with all of the ramifications that implies. (ie; INPUT =HIGH IMPEDANCE, OUTPUT = LOW IMPEDANCE)

What else could you need to know ?

Look at the code for pinMode () - this is open source, you have the source and you
are allowed (encouraged even) to look at it.

The direct register manipulation version of pinMode is to write the relevant bit in
DDRx, the data direction register for the relevant port (replace x by B, C or whatever),
although INPUT_PULLUP mode also affects the bit in PORTx register

The DDR doesn't switch the pin between input and output, it just enables/disables
the output driver FETs - an OUTPUT pin can still be read as an INPUT (even though
you ought to know what state its in!). This is occasionally very useful, for instance
you can use a PWM output to trigger a pin-change interrupt in synchrony with the
waveform generated.

Figure 13-2 in the datasheet shows the pad circuitry for an Arduino pin.

[caveat - this discussion only applies to the ATmega series of microcontrollers, Uno,
Micro, Nano, Leonardo, but not Due/Yun etc ]

an OUTPUT pin can still be read as an INPUT

So you're saying you can do this ?:

pinMode(2,OUTPUT);

int pin2_state = digitalRead(2);

MarkT:
This is occasionally very useful, for instance
you can use a PWM output to trigger a pin-change interrupt in synchrony with the
waveform generated.

That's pretty neat! I'll keep this one in mind.

raschemmel:
So you're saying you can do this ?:

pinMode(2,OUTPUT);

int pin2_state = digitalRead(2);

Absolutly. and it can be useful if you have many sections of code changing the output state of a pin. In reality it is more efficernt to store the output state as a variable and go from there....

Grumpy_Mike:
Inside the Arduino’s processor on each pin is a switch, it switches the pin between the output push pull drivers and the input latch.

That was misleading. It does no such thing.

The pin is always connected to the input circuitry, so no “switching” occurs in this respect. In fact, the output drivers are always connected too, but the “high” driver and the “low” driver are separately controlled. When the pin is defined as an input, both the “high” driver and the “low” driver are kept switched off. When the pin is defined as an output, the “high” driver and the “low” driver are alternately switched on according to whether the output is set to be a high or a low. This is referred to as a “Tri-state driver”.

In short, the output drivers are disabled whilever the pin is defined as an input.

Paul I always thought I was pedantic but you just take the biscuit. What a grand teaching style you have.