Chapter 02 Button

The Boot button on the Freenove ESP32-S3 Display can be configured as a regular input button after the program starts.

Project 2.1 Button

Component List

Freenove ESP32-S3 Display x 1

Chapter01_02

USB cable x1

Chapter01_03

Circuit

Connect Freenove ESP32-S3 Display to the computer with USB cable.

../../../_images/Chapter01_04.png

Sketch

Open “Sketch_02.1_Button” folder under “Freenove_ESP32_S3_Display_FNK0115\Sketches” and double-click “Sketch_02.1_Button.ino”.

Sketch_02.1_Button

The following is the program code:

 1/*
 2* @ File:   Sketch_02.1_Button.ino
 3* @ Author: [Eason Shen]
 4* @ Date:   [2026-07-03]
 5*/
 6
 7// Define button pin
 8#define BUTTON_PIN 0
 9
10volatile int interruptCounter = 0;
11int lastValue = 0;
12
13// ISR
14void IRAM_ATTR handleInterrupt() {
15  static unsigned long last_interrupt_time = 0;
16  unsigned long interrupt_time = millis();
17
18  // If the interval between two interrupts exceeds 80ms, it is considered a valid key press.
19  if (interrupt_time - last_interrupt_time > 80) {
20    interruptCounter++;
21  }
22  last_interrupt_time = interrupt_time;
23}
24
25// Button initialization function
26void buttonInit(void) {
27  pinMode(BUTTON_PIN, INPUT_PULLUP);
28  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleInterrupt, FALLING);
29}
30
31void setup() {
32  Serial.begin(115200);
33  while(!Serial);
34  buttonInit();
35  Serial.println("ESP32-S3 initialization completed!");
36}
37
38void loop() {
39  if (interruptCounter != lastValue) {
40    Serial.printf("interruptCounter: %d\r\n", interruptCounter);
41    lastValue = interruptCounter;
42  }
43}

Code Explanation

Define the button pin:

1#define BUTTON_PIN 0

Button initialization function, which sets the pin to input pull-up mode and configures the interrupt to trigger on a falling edge:

1// Button initialization function
2void buttonInit(void) {
3  pinMode(BUTTON_PIN, INPUT_PULLUP);
4  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleInterrupt, FALLING);
5}

External interrupt function, which is used for button debounce:

 1void IRAM_ATTR handleInterrupt() {
 2  static unsigned long last_interrupt_time = 0;
 3  unsigned long interrupt_time = millis();
 4
 5  // If the interval between two interrupts exceeds 80ms, it is considered a valid key press.
 6  if (interrupt_time - last_interrupt_time > 80) {
 7    interruptCounter++;
 8  }
 9  last_interrupt_time = interrupt_time;
10}

Click “Upload” to upload the code to Freenove_ESP32_S3_Display.

When the button is pressed, the Serial Monitor will print the total number of button presses.

../../../_images/Chapter02_03.png

Please note: This chapter does not involve the use of the screen. After the code for this chapter, the screen may not light up, which is normal and not a hardware malfunction. If you need to verify whether the screen is functioning properly, please refer to Chapter 10 for testing.