This question already has an answer here:

I am using an Arduino UNO.

On any digital/analog pin, if an LED (with resistor) is connected and we use digitalWrite(pin,HIGH) the LED lights up rather dimly.

On the other hand, if we use pinMode(pin,OUTPUT) and then use digitalWrite, the LED lights up to maximum intensity.

I checked that in both cases, the voltage on the pin is 5V. Any explanation for this ? Thanks.

marked as duplicate by Juraj, sempaiscuba, Greenonline, Gerben, VE7JRO Dec 1 '18 at 15:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This is not a duplicate question. I am specifically asking the effect of declaring a pin as OUTPUT versus direct use of digitalWrite on the pin. – Frost Dec 1 '18 at 12:02

This is due to a specific feature of the AVR microcontrollers, where the same register (PORTB, PORTC or PORTD) is used for controlling both the output value of the pin and its optional internal pullup resistor. The command digitalWrite(pin,HIGH) sets to 1 the bit of this register corresponding to the selected pin.

  • If the pin is set to INPUT (which is the default), the effect of digitalWrite(pin,HIGH) is to turn on the internal pullup.

  • If the pin is set to OUTPUT, the same command sets the output to HIGH.

The internal pullup is about 30 kΩ, which lets flow a very small current through the LED. You may notice that the voltage at the pin is 5 V if nothing is connected to it, but significantly less when the LED is connected. Notice also that the preferred way of enabling the pullup is pinMode(pin,INPUT_PULLUP).

Not the answer you're looking for? Browse other questions tagged or ask your own question.