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 ?
1 Answer 1
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.
pinMode(7, INPUT);
andpinMode(7, INPUT_PULLUP);