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.

  • 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.