0

I connected a led to pin 7 (with a small resistance to avoid burning it) and tried the code below, with and without pinMode as output

void setup() {
  pinMode(7, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  digitalWrite(7, HIGH);
  delay(5000);
  digitalWrite(7, LOW);
  delay(200);

  Serial.println("hello world");
}

Why does commenting the line pinMode(7, OUTPUT); make the led dimmer ?

2
  • try pinMode(7, INPUT); and pinMode(7, INPUT_PULLUP);
    – jsotola
    Commented Feb 20 at 20:46
  • not sure what i'm supposed to understand @jsotola it seems pull up is default but what is it?
    – Mah Neh
    Commented Feb 20 at 21:15

1 Answer 1

7

All the pins are configured as INPUT by default on startup. So if you leave out the pinMode(7, OUTPUT);, pin 7 will still be configured as input. An Input doesn't provide any current (or only a very very low leak current), so the LED will be off in this time.

When you write HIGH to a pin, while it is configured as INPUT, you are activating the internal pullup resistor. This is a resistor (typically about 10kOhm) from the pin to Vcc (HIGH level) inside the microcontroller itself. Now a small current - set by the total resistance (your resistor + internal pullup) - can flow through the diode, making it light up very dim.

Configuring a pin as INPUT with the internal pullup resistor activated can be done in one command: pinMode(7, INPUT_PULLUP);, which makes the intent more clear, but does exactly the same under the hood as setting to INPUT and writing HIGH to the pin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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