Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: digitalRead on switch seems random

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Location
    Cologne (DE)
    Posts
    10

    digitalRead on switch seems random

    Hello,

    a bit of a simple question, but I can't seem to find a solution-- I have a limit switch (NO) connected to pin 14 of my teensy LC:
    Code:
    #define S_limitSwitch  14  //  A0
    . When the switch closes, it connects the pin to ground. The pull up resistor is activated:
    Code:
    pinMode(S_limitSwitch, INPUT_PULLUP);
    . When I do
    Code:
    Serial.print("S_limitSwitch: ");
    Serial.println(digitalRead(S_limitSwitch));
    with the switch closed, I get a solid 1 (as supposed), but when it is open, the output is random 0 or 1.
    I checked the switch, it works fine. Am I missing something obvious here? Could it be that the cable length of 1m from teensy to the switch causes the problem?

    thanks, Philip

  2. #2
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    138
    Disable the internal pullup and use an external 4k7 resistor. Your wire is probably acting like an antenna and the internal pullup is not strong enough.
    The resistor goes between pin14 and 3.3V.

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,169
    When the switch is closed you should get a solid 0

    Pete

  4. #4
    Junior Member
    Join Date
    Apr 2019
    Location
    Cologne (DE)
    Posts
    10
    Quote Originally Posted by neurofun View Post
    Disable the internal pullup and use an external 4k7 resistor.
    Thank you so much, that (almost) did the trick! However, with an open switch, every 20-100 digitalReads will return a 0. Although that could be fixed in software, do you have any recommendations for further reading on that behaviour (or a keyword to search for)? I'd really like to understand how that happens. Maybe choosing a different resistor value would help

  5. #5
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    138
    Change the resistor to 2k2 and/or add a 100nF capacitor between pin14 and GND.

  6. #6
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,312
    You might be seeing 'Contact Bounce' or chatter:


    There are various hardware solutions, but a lot of people use a bounce library that does several reads and only after the button has achieved a steady state is it reported as being changed. I use the Bounce2 library which is a rewrite of the original Bounce library. Both are in the standard Teensy release. I recall having issues with the original Bounce library, but I don't remember what they were.


  7. #7
    Junior Member
    Join Date
    Apr 2019
    Location
    Cologne (DE)
    Posts
    10
    @neurofun A 2k2 resistor definitifely improved the readings. No 100nF capacitors at hand but I ordered some and will add one aswell. Thanks!

    @MichaelMeissner Thanks for your suggestions, but the bouncing happens without any contact whatsoever. Since this is a limit switch, bouncing during switching is not really an issue--

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •