I've got an Arduino mega compatible board, called Ethermega, from Freetronics (ATmega2560).
Schematic here.
In setup, I've written this code:
Serial.begin(9600);
pinMode(4, INPUT_PULLUP);
digitalWrite(4, HIGH); // Optional, I tried with and without
while (1)
{
if (digitalRead(4) == HIGH)
{
Serial.write("Up!\n");
}
}
(Don't worry about the fact I'm not using loop)
When I connect input 4 with 5V, I see "Up!". Rest of the time, I see nothing.
Considering I have enabled the internal pull-up, I'm expecting it should be up/high by default, and I would have to connect it to GND to get it low...
Another thing that puzzles me is I can find heaps of examples that use a tactile switch and no pullup/pulldown at all...
So what am I missing or doing wrong? Seems like whatever I do, I've got some kind of pulldown enabled...
1 Answer 1
As @Juraj mentions, pin 4 is the CS pin for the SD card. That pin has a volage divider of 1kΩ + 2.2kΩ on it (a total of 3.2kΩ) to ground. So yes, you do have a pulldown on that pin. 3.2kΩ is about 1/10th of the value of the typical pullup resistor internal to the chip, so pulls the output low enough to be seen as LOW unless you manually override it.
You can't use that pin as an active low input on that board - choose another, or use it as active high.
pinMode(x, INPUT); digitalWrite(x, HIGH);
is the same aspinMode(x, INPUT_PULLUP);
. In general most cores will emulate that for you (I know chipKIT does) but you're right, it's best not to rely on it.