MicroPython ESP32: Blink LED
Tags: micropython, esp32, arduino, robotics
Published: 2024-06-20 07:49:06


![Blink LED ESP32 MicroPython](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kxodjrcob7h1015egsm.png)

Certainly! Here's a description for your blog along with a diagram and comments explaining the code:

### Blog Description

Welcome to another exciting tutorial on MicroPython programming! Today, we'll be diving into the basics of controlling an LED using an ESP32 microcontroller. In this tutorial, we'll write a simple MicroPython script to make an LED blink on and off at regular intervals. This is a great starting point for anyone new to MicroPython and microcontrollers, as it covers fundamental concepts like pin configuration and timing functions.

### Code Explanation with Comments

```python
from machine import Pin  # Import the Pin class from the machine module
from time import sleep   # Import the sleep function from the time module

# Initialize pin 15 as an output pin
led = Pin(15, Pin.OUT)

# Infinite loop to blink the LED
while True:
    led.on()          # Turn the LED on
    sleep(0.5)        # Wait for 0.5 seconds
    led.off()         # Turn the LED off
    sleep(0.5)        # Wait for 0.5 seconds
```

### Diagram

Here’s a diagram illustrating the connections for this project:

```
ESP32 Microcontroller:
----------------------
        ___________
       |           |
       |           |
       |           |
       |    15     |--------> LED (Anode)
       |           |
       |           |
       |___________|
         |
         |
       GND
```

**Connections:**
- Connect the longer leg (anode) of the LED to GPIO pin 15 of the ESP32.
- Connect the shorter leg (cathode) of the LED to the GND pin of the ESP32.

### Detailed Code Breakdown

1. **Importing Libraries:**
   ```python
   from machine import Pin
   from time import sleep
   ```
   - `from machine import Pin`: This line imports the `Pin` class from the `machine` module, which is used to control the pins of the ESP32.
   - `from time import sleep`: This line imports the `sleep` function from the `time` module, allowing us to introduce delays in our code.

2. **Setting Up the LED Pin:**
   ```python
   led = Pin(15, Pin.OUT)
   ```
   - `led = Pin(15, Pin.OUT)`: This line initializes GPIO pin 15 as an output pin. The `Pin` class is used to configure the pin, and `Pin.OUT` specifies that the pin will be used for output.

3. **Main Loop to Blink the LED:**
   ```python
   while True:
       led.on()    # Turn the LED on
       sleep(0.5)  # Wait for 0.5 seconds
       led.off()   # Turn the LED off
       sleep(0.5)  # Wait for 0.5 seconds
   ```
   - `while True`: This starts an infinite loop that will run forever, continuously executing the code inside the loop.
   - `led.on()`: This turns the LED on by setting the voltage of pin 15 to high.
   - `sleep(0.5)`: This introduces a delay of 0.5 seconds, keeping the LED on for half a second.
   - `led.off()`: This turns the LED off by setting the voltage of pin 15 to low.
   - `sleep(0.5)`: This introduces another delay of 0.5 seconds, keeping the LED off for half a second.

By following this tutorial, you'll have a blinking LED that demonstrates the basics of using GPIO pins with the ESP32 and MicroPython. This foundational knowledge will pave the way for more complex projects in the future. Happy coding!