Control the lighting of LED by push button with ESP32

An ESP32 microcontroller can be used to control an LED by sending a digital signal to a transistor or a relay, which can then switch the power on and off to the LED.

To control an LED with an ESP32, you will need to connect the ESP32 to a transistor or a relay, which in turn will be connected to the LED. The transistor or relay can be connected to a digital pin on the ESP32, which can be programmed to send a signal to the transistor or relay, turning the LED on or off.

Additionally, the ESP32 can be connected to other devices such as a touch screen, or a mobile application to provide a user interface to control the LED. The ESP32 can also be connected to a Wi-Fi network to send and receive data, or to communicate with other devices using various protocols such as HTTP, MQTT, or Bluetooth.

When using an ESP32 to control an LED, it’s important to ensure that the ESP32 is properly programmed, configured, and connected to the transistor or relay, that the LED is properly connected and powered, and that the system is compliant with local laws and regulations. Additionally, it’s important to ensure that the system is properly maintained to ensure proper operation and to prevent breakdowns. It’s also important to consider the environmental impact of the LED and the energy efficiency.

Purpose of this tutorial:

In this tutorial we will control the lighting of an LED via a push button:

1- When the button is pressed, the LED lights up

2- When the button is released, the LED goes out.

Necessary components


The ESP32 is a low-cost, low-power microcontroller with built-in Wi-Fi and Bluetooth capabilities. It is a popular choice for IoT projects and is commonly used for a variety of applications such as home automation, wireless control, and sensor data logging. The ESP32 features a dual-core processor, a rich set of peripherals, and support for a wide range of protocols. It can be programmed using the Arduino IDE and various other programming languages such as C, C++, and MicroPython.

Additionally, the ESP32 has a wide range of features including:

  • A high-performance processor with a clock speed of up to 240 MHz
  • Support for various types of wireless connectivity such as Wi-Fi, Bluetooth, and Bluetooth Low Energy (BLE)
  • Multiple communication interfaces such as I2C, SPI, UART, and I2S
  • A large number of GPIO pins to connect to external devices and sensors
  • A built-in security module for secure communication

The ESP32 is often used in projects where a low-cost, low-power device with Wi-Fi and Bluetooth capabilities is needed, and it is commonly used with other sensors and devices to build IoT projects, home automation systems, wireless control systems, and data logging systems.


A red LED (Light Emitting Diode) is a type of semiconductor device that emits light when a current is passed through it. The light emitted by a red LED is typically in the red portion of the visible spectrum, with a wavelength between 620 and 750 nanometers.

Red LEDs are widely used in a variety of applications, such as indicator lights, automotive lighting, traffic signals, and electronic displays. They are also commonly used in electronic devices such as televisions, DVD players, and digital cameras as a backlight source.

Red LEDs are relatively inexpensive and have long lifetimes, making them a popular choice for many applications. They are also relatively low power, which makes them suitable for battery-powered devices.

LEDs are made of a semiconductor material, typically a crystalline substrate of silicon or gallium arsenide. When a current is applied to the LED, it causes electrons to flow through the semiconductor material, which in turn creates light as a result. The color of the light emitted by an LED is determined by the semiconductor material used and the specific manufacturing process.

LEDs are becoming increasingly popular due to their energy efficiency and long lifespan. They also have a fast response time and can be easily controlled with a microcontroller.


A pushbutton, also known as a switch or button, is a type of input device that is used to activate a circuit or signal. Pushbuttons are typically used to control electronic devices or to provide input to a computer or microcontroller.

Pushbuttons typically consist of a button that can be pressed, and a mechanism that activates a switch when the button is pressed. The switch can be a simple mechanical contact, or it can be an electronic switch such as a transistor or a microcontroller pin.

Connecting wires

Connecting wires refers to the process of physically connecting wires or cables to a device or circuit in order to establish an electrical connection. This can be done by using various connectors such as plugs, sockets, or terminal blocks. The wires are typically color-coded to indicate their function, such as red for power, black for ground, and yellow for signals.

Test plate 

A test plate is a type of circuit board that is used to test electronic components. It typically consists of a flat board made of a non-conductive material, such as plastic or fiberglass, with a number of holes or pads that are used to connect electronic components. The test plate allows you to connect electronic components and test them easily.


We connect pin D23 of the ESP32 board to the anode (+ terminal) of the LED and the GND of the ESP32 board to the cathode (- terminal) of the LED.

We connect the first pin of the push button to the GND pin of the ESP32 board and the second pin of the push button to the D21 pin of the ESP32 board.

Micropython program

Here is the program that controls the ignition of an LED connected to pin D23 of the ESP32 board.

7 commentaires

Family lawyer services 29-09-2323

This design is spectacular! You certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

removal Woodhouse 24-09-2323

Excellent post. Keep writing such kind of info on your blog. Im really impressed by it. Hi there, You've performed a fantastic job. I will definitely digg it and for my part suggest to my friends. I'm sure they'll be benefited from this website.

DBT private clinic 20-09-2323

When some one searches for his required thing, so he/she desires to be available that in detail, so that thing is maintained over here.

General contracting company 18-09-2323

Greetings! Very helpful advice within this post! It is the little changes which will make the biggest changes. Thanks for sharing!

Military Divorce 14-09-2323

Hello! Would you mind if I share your blog with my facebook group? There's a lot of people that I think would really appreciate your content. Please let me know. Thank you

flum flavors 02-09-2323

Howdy! Quick question that's entirely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my iphone. I'm trying to find a theme or plugin that might be able to correct this issue. If you have any suggestions, please share. Many thanks!

Finley 21-08-2222

Are you writing the articles in your website yourself or you outsource them? I am a blogger and having difficulty with content. Other bloggers told me I should use an AI content writer, they are actually pretty good. Here is a sample article some bloggers shared with me.

Leave a comment

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Scroll to Top