# How to use button to activate a function

**URL:** <http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856>\
**Category:** How to\
**Created:** [February 6, 2023, 9:59pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856 "2023-02-06T21:59:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlwaysLearning](http://forum.squareline.io/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@AlwaysLearning](http://forum.squareline.io/u/AlwaysLearning)\
**Post date:** [February 6, 2023, 9:59pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/1 "2023-02-06T21:59:51Z")

</div>

Hi everyone,

Working through how to use SLS.  
I’m using SLS 1.2.0 with ESP32, TFT screen and Arduino IDE (MAC)

I’d like to learn how to assign a button action to call a function.  
As a first step, how to toggle a LED on/off.

As an example of a simple button & blink function:

```auto
void loop() {
  last_button_state = button_state; // save the last state
  button_state = digitalRead(BUTTON_PIN); // read new state

  if (last_button_state == HIGH && button_state == LOW) {
    Serial.println("The button is pressed");

    // toggle state of LED
    led_state = !led_state;

    // control LED arccoding to the toggled state
    digitalWrite(LED_PIN, led_state);
  }
}

```

Thanks for any guidance!

---

<div class="post-metadata">

**Author:** ![hans](http://forum.squareline.io/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@hans](http://forum.squareline.io/u/hans)\
**Post date:** [February 7, 2023, 12:40am UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/2 "2023-02-07T00:40:10Z")

</div>

There is a [`YouTube`](https://youtu.be/6uKf5Bj0xcc?t=375) tutorial about this.

---

<div class="post-metadata">

**Author:** ![christine](http://forum.squareline.io/user_avatar/forum.squareline.io/christine/32/731_2.png) [@christine](http://forum.squareline.io/u/christine)\
**Post date:** [February 7, 2023, 7:33am UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/3 "2023-02-07T07:33:38Z")

</div>

Set the events of a button. Here are the specs you need to fill in

Name: as you like  
Tigger: `CLICKED`  
Action: `CALL FUNCTION`

Then press ADD (green button)

And fill in:  
Function name: as you like

---

<div class="post-metadata">

**Author:** ![williangomes.a](http://forum.squareline.io/letter_avatar_proxy/v4/letter/w/9de053/32.png) [@williangomes.a](http://forum.squareline.io/u/williangomes.a)\
**Post date:** [February 20, 2023, 8:50pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/4 "2023-02-20T20:50:35Z")

</div>

Hello, thank you guys for the post, someone, could please show an complete example in how to press a button in the interface and call a function in the code ? to turn on a LED for example, i’m a very begginer :(.

---

<div class="post-metadata">

**Author:** ![dronecz](http://forum.squareline.io/letter_avatar_proxy/v4/letter/d/49beb7/32.png) [@dronecz](http://forum.squareline.io/u/dronecz)\
**Post date:** [February 20, 2023, 11:23pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/5 "2023-02-20T23:23:30Z")

</div>

Do you mean in SLS or on real HW?

---

<div class="post-metadata">

**Author:** ![williangomes.a](http://forum.squareline.io/letter_avatar_proxy/v4/letter/w/9de053/32.png) [@williangomes.a](http://forum.squareline.io/u/williangomes.a)\
**Post date:** [February 21, 2023, 5:31pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/6 "2023-02-21T17:31:33Z")

</div>

Real hardware, i’m using an esp32.

---

<div class="post-metadata">

**Author:** ![dronecz](http://forum.squareline.io/letter_avatar_proxy/v4/letter/d/49beb7/32.png) [@dronecz](http://forum.squareline.io/u/dronecz)\
**Post date:** [February 21, 2023, 11:37pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/7 "2023-02-21T23:37:35Z")

</div>

With screen or just some devkit with onboard LED?

---

<div class="post-metadata">

**Author:** ![williangomes.a](http://forum.squareline.io/letter_avatar_proxy/v4/letter/w/9de053/32.png) [@williangomes.a](http://forum.squareline.io/u/williangomes.a)\
**Post date:** [February 22, 2023, 12:33am UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/8 "2023-02-22T00:33:04Z")

</div>

With a screen, controller ili9341, touch xpt2046, i’m able to use the touch, and to call a function, but I dont know, for example, how to touch a button in the screen and turn on a GPIO, or how to touch a phisical button and change a state in the screen.

---

<div class="post-metadata">

**Author:** ![dronecz](http://forum.squareline.io/letter_avatar_proxy/v4/letter/d/49beb7/32.png) [@dronecz](http://forum.squareline.io/u/dronecz)\
**Post date:** [February 23, 2023, 1:14am UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/9 "2023-02-23T01:14:25Z")

</div>

Try this 🙂 [Simple\_button.zip](http://forum.squareline.io/uploads/short-url/5mXmPQabkwOD7cNAgqGla2nILUj.zip) (12.3 KB)

Inside is SLS project and exported files with Arduino sketch. I hope it helps.

---

<div class="post-metadata">

**Author:** ![williangomes.a](http://forum.squareline.io/letter_avatar_proxy/v4/letter/w/9de053/32.png) [@williangomes.a](http://forum.squareline.io/u/williangomes.a)\
**Post date:** [February 23, 2023, 1:14pm UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/10 "2023-02-23T13:14:16Z")

</div>

Than you very much for your help, I will open the files, I do eletronics design, If I could help in something let me know !! 😀

---

<div class="post-metadata">

**Author:** ![iainmac](http://forum.squareline.io/letter_avatar_proxy/v4/letter/i/a9a28c/32.png) [@iainmac](http://forum.squareline.io/u/iainmac)\
**Post date:** [April 6, 2025, 6:27am UTC](http://forum.squareline.io/t/how-to-use-button-to-activate-a-function/856/11 "2025-04-06T06:27:54Z")

</div>

@dronecz 🙏Thank you as well. Spent the best part of a week trying to get this working. Your “simple button” example has now let me understand callbacks/events 🙏
