HOWTO · Arduino

How to Make a Counter in Arduino

Build a button-driven Arduino counter, wire an LED safely, and view counts in the Serial Monitor with an optional reset limit.

On this page

To make a counter in Arduino, connect a button to a digital input and GND, read each press once, increment a variable, and print the value to the Serial Monitor. The example below targets an Uno-compatible board and resets the count after a configurable limit. A built-in or external LED gives press feedback; it does not display the numeric count.

How an Arduino Counter Works

A counter is a variable that stores a number and changes when an event occurs. In this project, the event is a button press. The Arduino repeatedly runs loop(), checks the input, and increases the count once when the button changes from released to pressed. A short debounce interval filters the rapid electrical changes that can occur as a physical switch closes.

A loop can also repeat a fixed number of operations. For example, a for loop can blink an LED five times and then stop; that is a finite loop, while Arduino’s loop() continues running. The main sketch here uses the continuing loop to watch the button.

Wire the Button and LED

With INPUT_PULLUP, connect one side of the push button to digital pin 2 and the other side to GND. The board’s internal pull-up keeps the input HIGH while the button is released; pressing it connects the pin to GND and reads LOW. No external pull-up resistor is needed for this wiring arrangement.

For an external LED, connect digital pin 13 through a current-limiting resistor to the LED’s anode (long leg), then connect its cathode (short leg) to GND. The Uno’s built-in LED is commonly connected to pin 13, but check the pinout for other boards. The sketch uses LED_BUILTIN so it can use the board’s defined built-in LED pin. Keep the LED circuit within the board’s pin current limits.

Write and Upload a Button Counter Sketch

The following complete C++ source works as an Arduino sketch in the Arduino IDE. In a desktop C++ build, its included harness supplies three button presses so the counter logic has a visible result. Set resetAt to the first count at which the counter should return to zero; the debounce interval is 30 milliseconds.

#ifdef ARDUINO
#include <Arduino.h>
#else
// Desktop harness for a deterministic button sequence.
#include <iostream>
using byte = unsigned char;
const int HIGH = 1, LOW = 0, INPUT_PULLUP = 2, OUTPUT = 3;
const int LED_BUILTIN = 13;
unsigned long mockNow = 0;
void pinMode(byte, int) {}
void digitalWrite(byte, int) {}
unsigned long millis() { return mockNow; }
int digitalRead(byte) {
  bool pressed = (mockNow >= 10 && mockNow < 50) ||
                 (mockNow >= 100 && mockNow < 140) ||
                 (mockNow >= 190 && mockNow < 230);
  return pressed ? LOW : HIGH;
}
struct MockSerial {
  void begin(int) {}
  void println(int value) { std::cout << value << '\n'; }
  void println(const char* value) { std::cout << value << '\n'; }
} Serial;
#endif

const byte buttonPin = 2;
const byte ledPin = LED_BUILTIN;
const unsigned long debounceMs = 30;
const int resetAt = 3;

int counter = 0;
int lastRawReading = HIGH;
int stableReading = HIGH;
unsigned long lastChangeTime = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(buttonPin);
  unsigned long now = millis();

  if (reading != lastRawReading) {
    lastRawReading = reading;
    lastChangeTime = now;
  }

  if (now - lastChangeTime >= debounceMs && reading != stableReading) {
    stableReading = reading;
    if (stableReading == LOW) {
      counter++;
      digitalWrite(ledPin, HIGH);
      Serial.println(counter);
      if (counter >= resetAt) {
        counter = 0;
        Serial.println("Counter reset");
      }
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
}

#ifndef ARDUINO
int main() {
  setup();
  for (mockNow = 0; mockNow <= 230; mockNow += 10) loop();
}
#endif
1
2
3
Counter reset

In the desktop harness, the three supplied presses reach resetAt, so the output shows each count followed by the reset message.

In the Arduino IDE, select the connected board and port, upload the sketch, then open Tools > Serial Monitor and set its baud rate to 9600. Press and release the button once for each count. The monitor prints 1, 2, and 3; after the third press it prints Counter reset, and the next press prints 1. The LED is on while the button is stably pressed and turns off after release. On hardware, setup() and loop() use the Arduino core; the desktop harness supplies a fixed input sequence for the same counter logic.

Display and Reset the Count

The Serial Monitor is the numeric display in this example. A single LED can signal that a press was accepted, but it cannot show the count value. To display digits on the device, use a 7-segment module or an LCD and its required wiring and library; those displays are not implemented in this sketch.

The reset is automatic at resetAt: the sketch prints the threshold value, sets the stored count to zero, and continues. Change resetAt to another positive count to choose a different limit. The code does not use a separate reset button. If you add one, give it its own input and define its wiring and behavior explicitly rather than relying on an unused pin.

FAQ

What components do I need?

An Uno-compatible board, a push button, jumper wires, and a USB cable are enough to count presses and view them on a computer. Use the built-in LED, or add an external LED and current-limiting resistor for visual feedback.

Can I use a different display?

Yes. An LCD or 7-segment display can show the numeric value, but each needs suitable connections and display code. The example above sends the number to the Serial Monitor and uses its LED only as a press indicator.

How do I change the reset limit?

Edit resetAt near the top of the sketch. The counter resets immediately after printing the value that reaches that limit.

Can the counter decrement?

Yes. For a decrementing counter, subtract one on an accepted press and decide what lower limit should do. That change is not part of the sketch above.

Can I add more buttons?

Yes. Assign each additional button a pin, configure its input, and give it a distinct action, such as decrementing or manually resetting. Wire each button according to the input mode you choose; the main example only handles one button.