1 megaohm resistor on input to microcontroller

Discussion in 'General Electronics Chat' started by SPQR, Dec 9, 2011.

  1. SPQR

    Thread Starter Member

    Nov 4, 2011
    379
    48
    Hello again,

    Simple question regarding the connection of a switch to a microcontroller input.

    Below is a simple diagram for connecting a switch to a microcontroller input.
    I've looked around and found many simple circuits like this, but HERE I've also found an interesting circuit that includes a 1 megaΩ resistor between the switch and the microcontroller input.

    I would like to ask you if you feel that the 'protective' effect of the 1 mega Ω resistor is important enough for you to include it routinely in your own projects that connect a 5V input to a microcontroller?

    [​IMG]
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    The resistor as you have drawn provides no protection. The article that you referred to puts the 1M resistor in series with the input pin of the MCU.

    With regards to protecting the MCU, firstly, you must know what voltage is used to power the MCU. Secondly, limit all inputs to the MCU so that they do not exceed the supply voltage range.

    What the article refers to is a phenomenon called "CMOS latchup" which can destroy any CMOS gate structure. All signals to a CMOS gate must be kept within the boundaries of Vss and Vdd.

    I find the value of 1M somewhat excessive. I would choose something between 10K and 100K.
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    It isn't so much for protection as it is to ensure the input is held low until the switch is pressed.
     
  4. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    First, I have never used an arduino; my microcontroller experience is limited to PICAXE's. Secondly, I am not sure what you are trying to do, so my answer may not make sense.

    What a series resistor does is limit current; in the case of a 1 MΩ resistor in series with a 5 V supply, it will limit the current to 5 μA (not milliamps, microamps.) That is very little current - certainly not enough to power a microcontroller, so you would not use such a resistor in series with the power lead.

    If you used it in series with an output from the microcontroller, then it would still be limited to 5 μA (assuming that a logic high is 5V), so once again, that is very little current: not enough for much of anything except a signal. Generally, what one does is use a series resistor on the output to limit the current to something less than the output's maximum. In the case of many microcontrollers, that is 10 mA or more, so a 500 Ω resistor might be a good choice, but 1 M would not.

    Likewise, if you put the 1 MΩ resistor in series with an input, it would limit the input current to 5 μA. My guess is that would be sufficient for the Arduino to recognize it as a logic high, but you would have to check the Arduino specs to be sure. However, the only thing it would accomplish is to protect the Arduino from damage in the case of some high voltage being mistakenly connected to the input. Unless there was a real chance of that happening, I wouldn't bother with it.

    Maybe there will be others with better information.

    ETA: I typed too slowly and didn't see the previous responses until after I posted.
     
    Last edited: Dec 9, 2011
  5. SPQR

    Thread Starter Member

    Nov 4, 2011
    379
    48
    Excellent, I thank you all very much.

    I'm building a camera slider with a stepper motor, and have used five "switches" that connect directly to the Arduino digital input pins.

    The switches are two reed limit switches (for the "begin" and "end" of the slide), and standard push button for "stop", and two standard push buttons for "forward" and reverse" - not really very fancy. The "power" (+5V) for each switch comes directly from the Arduino.

    So probably what I'll do is use the circuit with two resisters and drop the "megaohm" resistor to the 10k to 100k range.
    I'll play around a bit before I solder everything.

    Thanks again!
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    If the MCU is powered from 3.3V than connect the switch to 3.3V, not 5V.

    The normal way is to connect the switch between the input pin and GND. Then use a 100K resistor from the input pin and 3.3V. This is called a PULL-UP resistor. Most MCUs have pull-up resistors already built in which you can enable when required.
     
  7. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    As MrChips already explained how to connect a simple switch,but in practice its a bit different.With mechanical switches their is a problem known as contact bounce.So to avoid it you can use debounce technique hardware or software.In software technique you check the input more then once and in hardware technique you use some sort of external circuit ,the most easiest one is to use a RC circuit to suppress the quick voltage changes caused by the contact bounce.

    Here is on I use...
    [​IMG]
    R1 = 10K
    R2 = 100K
    C1 = 100nF

    Good Luck
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    Forget switch bounce circuitry. This is easily remedied using software. I believe the OP is using these as limit switches so switch bounce is not an issue.
     
  9. SPQR

    Thread Starter Member

    Nov 4, 2011
    379
    48
    Very nice, thanks again.
    I'll play with that schematic also.

    Arduino has a Debounce Routine that I've worked with, but I'm not sure I want to expand the code for three debounce routines.

    And thank you MrChips for the comment on the limit switches not needed debouncing.
    I'd read that before, but you confirmed it!
    Thanks.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    989

    Should be pretty simple. The code probably references a port. You just need to first note what ports your switches are connected then modify the code accordingly.

    Post your code if you need some help.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    For optimum performance, limit switches should be on an external interrupt such as interrupt on change input.
     
  12. SPQR

    Thread Starter Member

    Nov 4, 2011
    379
    48
    What a nice discussion!

    As I mentioned I'm working on a camera slider for a friend and I hope to finish it over the next month or so.
    If appropriate, I'd like to post it on the Completed Projects Collection for comments on hardware and software (I'll post everything).

    In terms of the code for the limit switches, I just infinitely loop through the void() routine, and for every loop it checks if limit switches are "on".
    If they are "on" then the "enable" for the stepper motor driver is turned off and it stops.

    I do an "if-then" for each loop,for each of the switches - during the loop it checks if "forward" is on, then it goes forward, if "backward" is on it goes backward, and there is a "stop" switch that just stops the stepper whenever pressed.

    Again, thanks for the excellent comments.
    I'm working on the aluminum frame today, and hope to have something up and running in a few weeks.
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    989
    Hey as long as it works. But for Rev 2 you might look into using interrupts for your switches as Mr. Chips suggests.
     
Loading...