Table of Contents

How to Toggle an LED with a Push Button with ESP32 and PictoBlox

ESP32 Toggle LED State
Example Description
Build your own smart switch! Learn how to wire a push button and an LED to an ESP32 board, and program it using PictoBlox’s Upload Mode. This lesson covers the fundamentals of digital inputs, tracking variable states, and conditional logic so you can start creating real-world DIY automation projects.

Introduction

Push buttons are incredibly popular electronic components found in everything from your TV remote to gaming controllers! Learn what a push button is, how state-toggling logic works, how to interface an LED and a push button with the ESP32 board, and program them with ease in PictoBlox – our Scratch blocks-based graphical programming platform with advanced hardware interaction abilities. Once you’re equipped with the basic knowledge of tracking variable states, reading digital inputs, and handling signal debouncing, we’ll show you how to build a smart light switch and other exciting DIY automation projects using your ESP32.

To work in PictoBlox, you’ll first need to download it from HERE.

Ready? Set. Go!

Prerequisites

  • ESP32 Board
  • Laptop or Computer
  • PictoBlox installed on the system
  • Push Switch

How to connect with ESP32

  1. First, open PictoBlox.
  2. Click on the Board tab on the toolbar and select ESP32.Select ESP32 Board
  3. Next, click on the Connect button and select the appropriate serial port.
  4. In the top right corner, there is a toggle button named Mode. This button is used to switch between the two working modes of PictoBlox, namely the stage mode and the upload mode.
  5. Switch to Upload mode by toggling this button. You’ll observe some changes in the UI.

Connection Diagram

Connection Diagram - ESP32 LED using Switch

LED and Push Button Connection

  1. Connect the Ground Pin of the Push Button to GND in ESP32.
  2. Connect the Digital Input of the Push Button to PWM D4 Digital Pin in ESP32.

Step-by-Step Block Coding Guide

  1. Go to the ESP32 palette and drag the ‘when ESP32 starts up’ block into the scripting area.
  2. Go to the Variables palette and create a new variable by clicking on Make a Variable.Set its name as ‘pressed‘.
  3. Create another Variable named ‘State‘ by clicking on Make a Variable from Variables palette.
  4. Underneath when ESP32 starts up’ add a ‘set () to ()‘ block. Use the pressed variable and set its value to ‘0‘.
  5. Add another ‘set () to ()‘ block. Use the State variable and set its value to ‘0‘.Initial Script - ESP32 Toggle LED State
  6. Create a new block named ‘Change_State’ by clicking on Make a Block in My Blocks palette. A ‘define (Change_State)’ block will appear in your Scripting Area.
  7. Underneath the ‘Change_State‘ block, add the ‘if () then else’ block from the Control palette.
  8. Inside the if condition, add the ‘equals to‘ block from the Operators palette.
  9. Add the State variable in the left side of ‘equals to‘ block. For the right side, set the value to ‘0‘.If condition - Define Change State
  10. Add the ‘set digital pin () output as ()‘ block from the ESP32 palette. Use the Digital pin ‘2‘ and set its output as ‘HIGH‘.
  11. Add the ‘set () to ()’ block from the Variables palette. Use the ‘State‘ variable and set its value to ‘1‘.
  12. Inside the else, add the ‘set digital pin () output as ()‘ block from the ESP32 palette. Use the Digital pin ‘2‘ and set its output as ‘LOW‘.
  13. Add the ‘set () to ()’ block from the Variables palette. Use the ‘State‘ variable and set its value to ‘0’.Define State Change Script
  14. Underneath the ‘set () to ()‘ block with State variable, add the ‘forever’ block from the Control palette.
  15. Inside the ‘forever‘ block, add the ‘if () then else‘ block from the Control palette.
  16. Inside the if condition, add the ‘read status of digital pin ()‘ block from the ESP32 palette. Set its value to ‘4‘.
  17. Inside the if block, add the ‘if () then‘ block from the Control palette.If condition - ESP32 Toggle LED State
  18. Inside the newly added if condition, add the ‘equals to‘ block from the Operators palette.
  19. Add the pressed variable in the left side of ‘equals to‘ block. For the right side, set the value to ‘0‘.If condition continued - ESP32 Toggle LED State
  20. Add the Change_State block from the My Blocks palette.
  21. Add the set () to () block from Control palette. Use the pressed variable and set its value to ‘1’
  22. Inside the else, add the set () to () block. Use the pressed variable and set its value to ‘0’.Else condition - ESP32 Toggle LED State
  23. To upload the code, click on the Upload Code button.

Uploading

Output

Conclusion

In this lesson, we learned how to build a smart light switch by tracking variable states and interfacing a push button alongside an LED with the ESP32 board in PictoBlox’s Upload mode. We moved beyond simple direct polling by creating a custom function block  to toggle the LED output on Pin 2 between HIGH and LOW based on the memory of our system variables. Through this hands-on project, you have gained practical experience with state-toggling logic—a vital tool you can now use to build advanced smart home systems and interactive automation projects!