Skip to content

Lesson02: BOOT Button Serial Output

1. Learning Objectives

In this lesson, you will learn how to read the onboard BOOT button and print press, single-click, double-click, and long-press events through the serial port. Power and Reset button events are not printed in this lesson: the Reset button resets the program; a single click of the Power button turns the display on or off, while a long press shuts down the device. The encoder button will be covered in the encoder lesson.

2. Prerequisites

  • Completed the ESP32-S3 environment setup in Lesson01.
  • Additional library: OneButton. This project uses version 2.6.1.

How to add the library files:

3. Arduino IDE Instructions

  1. Open Lesson02_Boot_Button.ino in the Arduino IDE. lesson_1

  2. Confirm that the board, Flash, PSRAM, and partition scheme settings are exactly the same as in Lesson01. lesson_2

  3. Confirm that the OneButton library is installed or that the shared libraries folder exists. lesson_3

  4. Select the actual serial port and click Verify to compile. (Connect the hardware using a USB data cable.) lesson_4

  5. Click Upload to flash the program, then open Serial Monitor and set the baud rate to 115200. lesson_5

  6. Press the BOOT button and observe the event messages in the serial output. lesson_6

4. Hardware Instructions

  1. Connect the development board to the computer using a USB data cable, and keep Serial Monitor open.

lesson_7

  1. Briefly press the BOOT button and check whether the BOOT button event is printed in the serial output.

lesson_8

  1. Quickly press the BOOT button twice in succession and observe the double-click event.

  2. Press and hold the BOOT button for approximately 1 second or longer, and observe the long-press start and long-press end events.

  3. Do not operate the Reset or Power buttons in this lesson. Reset resets the program; a single click of Power turns the display on or off, while a long press shuts down the device.

lesson_9

5. Key Code Explanation

constexpr uint8_t kBootButtonPin = 0;
OneButton bootButton(kBootButtonPin, true, true);

kBootButtonPin refers to GPIO0, which is connected to the BOOT button on the ESP32-S3 module. In the OneButton constructor, the first true indicates that the button is active-low, and the second true enables the internal pull-up resistor. As a result, the input is HIGH when the button is not pressed and is pulled LOW when the button is pressed.

bootButton.setDebounceMs(30);
bootButton.setClickMs(400);
bootButton.setPressMs(1000);

Mechanical buttons produce contact bounce, so setDebounceMs(30) filters out glitches shorter than 30 ms. setClickMs(400) defines the detection window for single and double clicks, while setPressMs(1000) defines the long-press threshold. Together, these three timing settings determine how button events are recognized.

bootButton.attachPress(onBootPressed);
bootButton.attachClick(onBootClicked);
bootButton.attachDoubleClick(onBootDoubleClicked);
bootButton.attachLongPressStart(onBootLongPressStarted);
bootButton.attachLongPressStop(onBootLongPressStopped);

This code binds different button events to different callback functions. After the student presses the BOOT button, OneButton automatically determines the event type based on timing and state, then calls the corresponding function to print information to the serial output.

void loop() {
  bootButton.tick();
  delay(5);
}

OneButton is not driven by hardware interrupts, so it must be called frequently in loop(). Each call to tick() reads the button state once and updates the event state machine. delay(5) keeps the loop’s sampling frequency sufficiently high while preventing the CPU from busy-waiting.

Download the Lesson 02 code