Reading double- and long-clicks with capacitive sensor CJMCU-1010

I want a single button to trigger different events by distinguishing single-, double- and long-clicks. As button hardware, I am using the CJMCU-1010 capacitive sensor (based on the AT42QT101x).
I tried several libraries for handling click events, a well known is AceButton. While detecting single-clicks is working, reading double clicks doesn’t work. What is the problem? Is AceButton not meant to work with my button hardware?

My code:

   A demo of a simple AceButton used to handle the events from one button.
   Similar to HelloButton with some additions;
   - more comments
   - prints out the button events to the Serial monitor
   - enables the all button events, including LongPress and RepeatPress
   - suppresses lower-level events when higher-level events are detected
     (e.g. Clicked suppressed Released, DoubleClicked suppresses the
     second Clicked, LongPressed suppressed the Released, etc.)
   - setup() determines if a button was pressed during reboot

#include <AceButton.h>
using namespace ace_button;

// The pin number attached to the button.
const int BUTTON_PIN = 2;

#ifdef ESP32
// Different ESP32 boards use different pins
const int LED_PIN = 2;
const int LED_PIN = LED_BUILTIN;

// LED states. Some microcontrollers wire their built-in LED the reverse.
const int LED_ON = LOW;
const int LED_OFF = HIGH;

// One button wired to the pin at BUTTON_PIN. Automatically uses the default
// ButtonConfig. The alternative is to call the AceButton::init() method in
// setup() below.
AceButton button(BUTTON_PIN);

void handleEvent(AceButton*, uint8_t, uint8_t);

void setup() {
  delay(1000); // some microcontrollers reboot twice
  while (! Serial); // Wait until Serial is ready - Leonardo/Micro
  Serial.println(F("setup(): begin"));

  // initialize built-in LED as an output
  pinMode(LED_PIN, OUTPUT);

  // Button uses the built-in pull up register.

  // Configure the ButtonConfig with the event handler, and enable all higher
  // level events.
  ButtonConfig* buttonConfig = button.getButtonConfig();

  // Check if the button was pressed while booting
  if (button.isPressedRaw()) {
    Serial.println(F("setup(): button was pressed while booting"));

  Serial.println(F("setup(): ready"));

void loop() {
  // Should be called every 4-5ms or faster, for the default debouncing time
  // of ~20ms.

// The event handler for the button.
void handleEvent(AceButton* /* button */, uint8_t eventType,
                 uint8_t buttonState) {

  // Print out a message for all events.
  Serial.print(F("handleEvent(): eventType: "));
  Serial.print(F("; buttonState: "));

  // Control the LED only for the Pressed and Released events.
  // Notice that if the MCU is rebooted while the button is pressed down, no
  // event is triggered and the LED remains off.
  switch (eventType) {
    case AceButton::kEventPressed:
      digitalWrite(LED_PIN, LED_ON);
    case AceButton::kEventReleased:
      digitalWrite(LED_PIN, LED_ON);
    case AceButton::kEventDoubleClicked:
      Serial.println("Doble Click");

Using libraries calls for studying the functions provided by the library.
How long must a short press be? How long must the rest be between 2 pressings? Minimum length for a long pressing?
Have You checked the libray/libraries used for those questions?
What type of button You use should not make much difference. Maybe the bouncing problem is differet, maybe not.

// One button wired to the pin at BUTTON_PIN. Automatically uses the default
// ButtonConfig. The alternative is to call the AceButton::init() method in
// setup() below.
AceButton button(BUTTON_PIN);

The constructor contains optional parameters and defauls

AceButton(uint8_t pin = 0, uint8_t defaultReleasedState = HIGH, uint8_t id = 0);

Your device goes HIGH when pressed and LOW when released.

// Button uses the built-in pull up register.

You also are using INPUT_PULLUP which seems wrong for this sensor. I would think an external pulldown is more appropriate if the sensor does not have active high and low outputs.

I think some study time at the github home of the library would be worth it.GitHub - bxparks/AceButton: An adjustable, compact, event-driven button library for Arduino that debounces and dispatches events to a user-defined event handler.

There are also some special considerations and two methods for registering a double click which are presented and discussed.

Also consider the reaction speed of that sensor - I haven't worked with this particular sensor, but the ones I did work with were rather slow in reacting, especially compared to normal buttons. It takes a while for a capacitive sensor to see "touch" or "no touch", those periods may be so long it's seen as separate clicks instead of a double click.

Thanks to cattledog, I set

AceButton button(BUTTON_PIN, LOW);

After checking more AceButton examples and reading on github, I've found out that I missed to set the double-click feature:


Now, everything works as expected, pinMode(BUTTON_PIN, INPUT); is working fine as well as reaction speed is good.

Great! You see that Your own work payed off.