I tried in my Arduino the negative logic to get a digital input from a switch (with 10 KΩ resistance). If I want to use the positive logic what kind of resitance I have to use? For me seems that 10KOhm it to big and probably a smaller resistor should be used. How do we compute the value of the resistor? The schemes of Negative and Positive Logic from Valvano's notes/ebook - University of Texas gives 10 kΩ in both cases. Is it correct?
1 Answer
From a pure electronics viewpoint, there is no real difference between both circuits, the difference will be handled by your code running on the MCU, as a close switch will either be HIGH
or LOW
depending on the logic you used to wire your switch.
The resistor must be present for 2 reasons:
- to avoid producing a shortcut between
+5V
andGND
when the switch is closed, without it the shortcut would probably damage your power supply circuitry (if both pins are directly taken from Arduino, that would mean damaging Arduino supply circuits) - to make the voltage pin attached to the pin the "winner" of both voltage pins when the switch is closed, ie that pin will provide the level to Arduino input pin
The value of the resistor should be:
- high enough to limit the current between
+5V
andGND
when the switch is closed, preventing damage and limiting overall power consumption of your circuit - not too high to allow providing a voltage level to the input pin
Using 10k means that, applying Ohm's law, when closing the switch, a 0.5mA current would flow from +5V
to GND
, which is generally acceptable in terms of consumption and cwill not damage your power supply.
Note that the input pin is "high impedance" which means it has no voltage level if not connected to anything, and it does not supply or sink any current (or more accurqtely a negligible current only).
Finally, it is good to know that Arduino MCU offers the INPUT_PULLUP
mode for digital pins, where an internal resistor pulls up the pin input to 5V. This enables you to directly connect your switch between the pin and GND
. So you just have to code:
pinMode(XXX, INPUT_PULLUP);
instead of:
pinMode(XXX, INPUT);
For information, according to ATmega328P datasheet (the MCU of Arduino UNO), those internal pullup resistors have a value between 20k and 50k.
-
thanks a lot . As I understood the INPUT_PULLUP is a ~50kΩ embedded resistance (in ATmega328P chip) that protects the digital input. For example if you connect directly the +4 Volt to a digital input the chip is already protected.. right? So the 10 kΩ resistance is just a protection of the shortcut between +5V and GND. Here stackexange - electronics Q says that
digitalWrite(13, HIGH);
Turns internal pull-up on anddigitalWrite(13, LOW);
Turns internal pull-up off.– ggiaCommented Oct 22, 2014 at 12:29 -
Maybe this way of turning internal pull-up on/off works but this is not the recommended way; the preferred (and official) way is just
pinMode(13, INPUT_PULLUP);
Commented Oct 22, 2014 at 17:50 -
INPUT_PULLUP requires 1.0.1 of the IDE. The documentation states that prior to that release the digitalWrite HIGH/LOW mechansim was used. Commented Oct 22, 2014 at 18:17
-
Who's still using pre-1.0.1 of Arduino IDE? For those people, maybe it is time to upgrade. Commented Oct 22, 2014 at 18:59