ESP32 MicroPython Switch

This tutorial instructs you how to use the ON/OFF switch with a ESP32 and MicroPython. In detail, you will learn:

ESP32 MicroPython ON/OFF Switch

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Wires
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of ON/OFF Switch

A switch marked ON/OFF moves from ON to OFF, or from OFF to ON, when pressed. It remains in the new position after you release it. Press it once more to switch it back.

Pinout

There are two primary kinds of ON/OFF switches: the two-pin switch and the three-pin switch.

In this guide, we use a switch with two pins. You do not need to worry about which pin is which.

ON/OFF Switch Pinout

How It Works

Here are two ways to use an ON/OFF switch. Below, you will find the wiring instructions for the ON/OFF switch and the status readings on the ESP32 for each way:

pin 1 pin 2 ESP32 Input Pin's State
1 GND ESP32 Input Pin (with pull-up) HIGH OFF, LOW ON
2 VCC ESP32 Input Pin (with pull-down) HIGH ON, LOW OFF

We just have to choose one of the two methods. The rest of the guide will use the first method.

Wiring Diagram

  • How to connect ESP32 and switch using breadboard
The wiring diagram between ESP32 MicroPython ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and switch

We recommend using a Soldering Iron to securely solder the wires and the ON/OFF switch pin. After that, cover them with Heat Shrink Tube for protection.

ESP32 MicroPython Code - ON/OFF Switch

An ON/OFF switch, similar to a button, requires debouncing. Debouncing can make the code more complex. Fortunately, the DIYables_MicroPython_Button library helps by providing a debouncing feature and includes an internal pull-up resistor, which makes programming easier.

※ NOTE THAT:

There are two use cases:

  • First: If the switch is ON, do something. If the switch is OFF, do the opposite.
  • Second: If the switch goes from ON to OFF or from OFF to ON, do something.

Let's learn how to program ESP32 one by one.

ESP32 - Read state of ON/OFF Switch

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-switch """ from DIYables_MicroPython_Button import Button import time # Initialize the on/off switch connected to GPIO19 pin of the ESP32 switch = Button(19) # Set debounce time to 50 milliseconds switch.set_debounce_time(50) while True: switch.loop() # Update the button state state = switch.get_state() if state == 1: # GPIO HIGH print("The switch: OFF") else: print("The switch: ON")

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ON/OFF switch to the ESP32 according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • On Thonny IDE, navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Button”, then find the Button library created by DIYables.
  • Click on DIYables-MicroPython-Button, then click Install button to install Button library.
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Move the switch to the ON position.
  • Check out the message in the Shell at the bottom of Thonny.
  • Switch to the OFF position.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The switch: OFF The switch: OFF The switch: OFF The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: OFF The switch: OFF The switch: OFF The switch: OFF The switch: OFF
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

ESP32 - Detect Event from ON/OFF Switch

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-switch """ from DIYables_MicroPython_Button import Button import time # Initialize the on/off switch connected to GPIO19 pin of the ESP32 switch = Button(19) # Set debounce time to 50 milliseconds switch.set_debounce_time(50) while True: switch.loop() # Update the button state if switch.is_pressed(): print("The switch: OFF -> ON") if switch.is_released(): print("The switch: ON -> OFF")

Detailed Instructions

  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your ESP32
  • Move switch between ON/OFF several time.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The switch: OFF -> ON The switch: ON -> OFF The switch: OFF -> ON The switch: ON -> OFF
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!