Chapter 3 Button

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

Project 3.1 Button RGB

In the previous chapter, we learned about RGB LEDs. In this section, we will further explore how to integrate RGB LEDs with buttons.

Component List

Freenove ESP32-S3 Display x 1

Chapter01_07

USB cable x1

Chapter01_08

Circuit

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

../../../_images/Preface09.png

Sketch

Open “Sketch_03.1_Button_RGB” folder under “Freenove_ESP32_S3_Display\Tutorial_With_Touch\Sketches” and double-click “Sketch_03.1_Button_RGB.ino”.

Sketch_03.1_Button_RGB

The following is the program code:

 1/*
 2* @ File:   Sketch_03.1_Button_RGB.ino
 3* @ Author: [Eason Shen]
 4* @ Date:   [2026-05-15]
 5*/
 6
 7#include <Arduino.h>
 8#include "Freenove_WS2812_Lib_for_ESP32.h"
 9
10#define FNK0104AB_2P8_240x320_ILI9341
11//#define FNK0104N_3P5_320x480_ST77922
12//#define FNK0104S_4P0_320x480_ST7796
13
14#ifdef FNK0104N_3P5_320x480_ST77922
15 #define LEDS_PIN   40
16#else
17 #define LEDS_PIN   42
18#endif
19#define LEDS_COUNT 1
20#define CHANNEL    0
21#define KEY_PIN 0
22
23Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);
24
25uint8_t m_color[5][3] = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 }, { 255, 255, 255 }, { 0, 0, 0 } };
26
27static unsigned int keyCount = 0;
28static unsigned int lastKeyCount = 1;
29
30void buttonInit(void) {
31  pinMode(KEY_PIN, INPUT_PULLUP);
32}
33
34bool readButton(void) {
35  return digitalRead(KEY_PIN);
36}
37
38void switchRGB(int value) {
39  int colorIndex = value % 4;
40  switch (colorIndex) {
41    case 1:
42      strip.setLedColorData(0, m_color[0][0], m_color[0][1], m_color[0][2]);
43      strip.show();
44      break;
45    case 2:
46      strip.setLedColorData(0, m_color[1][0], m_color[1][1], m_color[1][2]);
47      strip.show();
48      break;
49    case 3:
50      strip.setLedColorData(0, m_color[2][0], m_color[2][1], m_color[2][2]);
51      strip.show();
52      break;
53    default:
54      strip.setLedColorData(0, m_color[4][0], m_color[4][1], m_color[4][2]);
55      strip.show();
56      break;
57  }
58}
59
60void setup() {
61  strip.begin();
62  strip.setBrightness(10);
63  buttonInit();
64  Serial.println("ESP32-S3 initialization completed!");
65}
66
67void loop() {
68  if (readButton() == 0) {
69    delay(20);
70    if (readButton() == 0) {
71      keyCount++;
72      while (!readButton());
73    }
74  }
75  if (keyCount != lastKeyCount) {
76    switchRGB(keyCount);
77    lastKeyCount = keyCount;
78  }
79}

Code Explanation

Define the pins for the button and the RGB LED.

1#define FNK0104AB_2P8_240x320_ILI9341
2//#define FNK0104N_3P5_320x480_ST77922
3//#define FNK0104S_4P0_320x480_ST7796

Initialize the RGB LED and the button.

1default:
2  strip.setLedColorData(0, m_color[4][0], m_color[4][1], m_color[4][2]);
3  strip.show();

Control the color of the RGB LED through the button.

 1void setup() {
 2  strip.begin();
 3  strip.setBrightness(10);
 4  buttonInit();
 5  Serial.println("ESP32-S3 initialization completed!");
 6}
 7
 8void loop() {
 9  if (readButton() == 0) {
10    delay(20);
11    if (readButton() == 0) {

Click “Upload” to upload the code to Freenove ESP32-S3 Display.

../../../_images/Chapter03_02.png

The RGB LEDs will cycle through Red -> Green -> Blue -> OFF every 500ms after code upload.

../../../_images/Chapter03_03.png