element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Arduino
  • Products
  • More
Arduino
Arduino Forum How to configure an Arduino GPIO as an input
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 34 replies
  • Subscribers 99 subscribers
  • Views 636 views
  • Users 0 members are here
  • arduino best practice
  • arduino circuit
Related

How to configure an Arduino GPIO as an input

colporteur
colporteur over 1 year ago

What would you recommend to reduce the spurious triggers on the input of a Nano?

 

Up to now I have been using Arduino Nano microcontrollers for animation support on a model railroad without issue. With the recent completion of the airport scene, I started to experience issues of Nano's receiving spurious triggers. At first I thought maybe I set the input resistor to high, so I reduced the value from 100K to 47K. I got some relief but it has recently raised it ugly head again. Without the resistor the triggering is unpredictable.

 

Currently there are 5 animations that have button control.

  • Aircraft landing simulation (LED)
  • Hanger building Lighting (LED)
  • Simulate arc welder at bench (LED)
  • Compound gate open/close (28BYJ-48 ULN2003 controller)
  • Hanger aircraft engine startup/shutdown (L293 brushless motor)

 

The compound gate sometimes triggers the Hanger aircraft engine animation and vise versa. The input circuit I am using for the Nano is minimal.  These two animations did share the same twisted pair from the button panel. I have separated the animations button to different pairs and I have some relief. I'm thinking maybe there is a simple way to dampen inputs so they are not as sensitive?

 

I'm hoping someone might have a suggestion/best practice recommendation for connecting inputs, that is designed to eliminate the problem.

 

  • Reply
  • Cancel
  • Cancel

Top Replies

  • baldengineer
    baldengineer over 1 year ago +9

    100K and 47K are very weak pull-down (or up) resistors. If you're in a known noisy environment, you probably want something in the 1K to 4.7K range.

  • wolfgangfriedrich
    wolfgangfriedrich over 1 year ago +8

    Try a small capacitor ( 10nF ) in parallel to the resistor. This is called de-bouncing an input.

    Can also be done in software, there might even Arduino libraries for that.

    - W.

  • dougw
    dougw over 1 year ago +7

    It sounds like you are getting cross-talk as well as switch bouncing.

    If you put a resistor in series with the switch and a capacitor at the digital input as wolfgangfriedrich mentioned, there won't be…

  • wolfgangfriedrich
    wolfgangfriedrich over 1 year ago

    Try a small capacitor ( 10nF ) in parallel to the resistor. This is called de-bouncing an input.

    Can also be done in software, there might even Arduino libraries for that.

    - W.

    • Cancel
    • Up +8 Down
    • Reply
    • Cancel
  • dougw
    dougw over 1 year ago

    It sounds like you are getting cross-talk as well as switch bouncing.

    If you put a resistor in series with the switch and a capacitor at the digital input as wolfgangfriedrich mentioned, there won't be any sudden changes in voltage, which will reduce any coupling between signals and significantly smooth out any bouncing.

    • Cancel
    • Up +7 Down
    • Reply
    • Cancel
  • colporteur
    colporteur over 1 year ago in reply to wolfgangfriedrich

    Thanks DW. I understand the goal of de-bounce. I haven't had to use the technique with Pi's and for the most part with the Arduino. I'm thinking it has to do with motors and relays I have started to deploy. I see on the Arduino site there is a de-bounce sketch to eliminate the issue.

     

    I will give both solutions a try. I'm looking for consistent. Maybe the component is best.

    • Cancel
    • Up +5 Down
    • Reply
    • Cancel
  • milosrasic98
    milosrasic98 over 1 year ago

    Hi colporteur

     

    The best option is to of course go for both hardware and software debouncing. Hardware debouncing can be done easily as mentioned already by wolfgangfriedrich , as for the software debouncing here is a simple piece of code which could help:

     

    #define debounceDelay 100
    
    
    int lastButtonState = 0;
    int lastDebounceTime = 0;
    int buttonState;
    
    
    
    
    void setup() {
    
    
      pinMode(BUTTON_CENTER, INPUT);
      
    }
    
    
    
    
    void loop(){
    
    
      int reading = digitalRead(BUTTON_CENTER);
        if (reading != lastButtonState) {
          // reset the debouncing timer
          lastDebounceTime = millis();
        }
        if ((millis() - lastDebounceTime) > debounceDelay) {
          if (reading != buttonState) {
            buttonState = reading;
            if (buttonState == HIGH) {
              //Here you will know that the button was pressed
            }
          }
        }
        lastButtonState = reading;
      
      }

     

         I've found that this approach works great, and it's using millis instead of delay so there isn't any code flow blocking. I've put the debounce delay to 100ms in the code, but you can tweak it to your liking. I found it works great like this, though, maybe a touch slower. For how this code works, when we detect a button reading which was different from the last button reading we just remember at what point that occurred. If we keep the buttons pressed long enough (100ms in our case), lastDebounceTime won't change and our second if statement will be fulfilled. Once the buttons was On for 100ms at least, it still won't register the press until you release the button. Hope this helps you!

     

    Milos

    • Cancel
    • Up +6 Down
    • Reply
    • Cancel
  • colporteur
    colporteur over 1 year ago in reply to milosrasic98

    Thanks MR, I saw a code example https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce  and wondered about the delay value.

     

    I'm a code resurrectionist and not an programmer, so I tend to default to hardware for solution. Your code is slightly different than what I found. What advantage is

    1. #define debounceDelay 100

     

    verses unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

    • Cancel
    • Up +6 Down
    • Reply
    • Cancel
  • baldengineer
    baldengineer over 1 year ago

    100K and 47K are very weak pull-down (or up) resistors. If you're in a known noisy environment, you probably want something in the 1K to 4.7K range.

    • Cancel
    • Up +9 Down
    • Reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago

    ... and if you can afford it in your design, polling is more forgiving than triggering.

    Triggers are excellent for digital signals. For mechanical switches they are problematic by design.

    • Cancel
    • Up +6 Down
    • Reply
    • Cancel
  • dougw
    dougw over 1 year ago in reply to colporteur

    If you want to fully debounce a switch in hardware it is easy to achieve 100% instantaneous debouncing if you use a double throw switch. You don't need any other hardware if you feed 2 digital inputs from the the switch, but you can use a couple of inverters or NAND gates if you want to do it all in hardware.

    • Cancel
    • Up +4 Down
    • Reply
    • Cancel
  • shabaz
    shabaz over 1 year ago

    Hi Sean,

     

    I'd do the same as James mentions, heavily reduce the 47k resistor. Also, you could check with your 'scope if you're picking up electrical noise, since it's highly likely with model railroad that you've got mains wiring nearby, on the floor, for example mains cables and power supplies. Twisted pair might not be very effective for electric field based noise, coax would be better.

    Also, it doesn't help that the threshold for logic high is quite low, perhaps just a fraction of a volt (with some 3.3V logic families, or a bit higher for 5V). There are more elaborate circuits for inputs, that could involve using a transistor and resistors arranged to switch at a higher threshold (e.g. several volts to be a logic '1'). In other words, behave more like (say) RS232 levels instead of 5V CMOS etc. Also, although technical overkill, the simplest way could just be to use an optocoupler if you just wanted a simple quick-to-build (but more expensive) life.

    None of this addresses debouncing, but the methods mentioned by everyone above would work (or just do it in software). There are nice ways to combine hardware and software to have a hybrid approach, but that increases software development time and hardware time. But would be very rugged options. One hybrid approach is to use an I2C I/O expander. They have an interrupt pin. You can trigger on interrupt, and then read the inputs. Apart from the fact that it needs the I2C library, it actually simplifies the software structure since there's no need to do polling.

    • Cancel
    • Up +6 Down
    • Reply
    • Cancel
  • colporteur
    colporteur over 1 year ago in reply to baldengineer

    This suggestion is a first for me. I have seen typically 10K pullup or pulldown but not 4.7K or as low as 1K.

     

    I used a 7ft piece of CAT6e to wire a  button panel to a distribution frame. One conductor provides 5VDC to all the buttons with the seven remaining wires individually connected to a button. The two spurious Nano's were each sharing one conduct from the same pair. I separated them to different pairs and haven't seen the problem.

     

    The twists in the CAT6e cable pairs are pretty intense. I had worked with CAT5 but was taken aback at the construction of the CAT6e. I am using the cable as a cross connect wire which is not as it was designed for. I suspect the intense twists can't but help cross talk.

     

    I like the resistor suggestion because of the simplicity. I will move the simulations to the same pairs again and try the lower resistor value to see what affect it has.

    • Cancel
    • Up +3 Down
    • Reply
    • Cancel
>
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube