Micro Switch Counting

Hi there,

I am a complete beginner when it comes to electronics / Arduino and think I have the basics but I am struggling getting my head around how to wire to my breadboard / Arduino Leonardo a basic micro switch and then have the Arduino count how many presses have been detected.

Any help greatly received as I cant really find anything online, in simple terms, that clearly states how to wire to breadboard.

Many thanks

In Arduino IDE look at example called " StateChangeDetection" . Wire one pin of your switch to 5V pin on arduino and the oder to pin 2 . Then open Serial Monitor and watch the counter :slight_smile:

You'll also need to debounce the switch (see http://arduino.cc/en/Tutorial/Debounce) and have a pull-down resistor between pin 2 and ground to prevent the pin from floating.

Thanks both, worked a treat :slight_smile:



Or better yet, wire the switch to ground not 5V and use pinMode(pinnum, INPUT_PULLUP) and do away with the need for an external resistor. HIGHs and LOWs in your code need to be switched, but the so-called "active low" is a more common way of doing things.