Logo

dev-resources.site

for different kinds of informations.

MicroPython ESP32: Blink LED

Published at
6/20/2024
Categories
micropython
esp32
arduino
robotics
Author
shemanto_sharkar
Author
16 person written this
shemanto_sharkar
open
MicroPython ESP32: Blink LED

Blink LED ESP32 MicroPython

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



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


Enter fullscreen mode Exit fullscreen mode

Diagram

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



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


Enter fullscreen mode Exit fullscreen mode

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)


Enter fullscreen mode Exit fullscreen mode
  • 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.
  1. 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!
Enter fullscreen mode Exit fullscreen mode
esp32 Article's
30 articles in total
Favicon
ESP32 Weather Dashboard with a WiFi Menu
Favicon
Prototipos rΓ‘pidos con Wokwi, ESP32 y AWS IoT Core
Favicon
Smart Home Security: Advanced Motion Detection with CCTV
Favicon
Matter protocol on a budget
Favicon
Ulanzi TC001 - ESP32 Programming / Custom Arduino firmware
Favicon
Rust on a $2 dev board
Favicon
Sliding Puzzle Next Move Suggesting Simple DL Model with ESP32 TensorFlow Lite
Favicon
Time-for-space reluctance for the Wi-Fi AP scan for ESP8266
Favicon
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 2 : Node, OpenAI, Langchain
Favicon
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 1 : Hardware, PlatformIO & C++
Favicon
ESP32 Smart Garden: Automating Plant Care with IoT
Favicon
Smart Connectivity: Leveraging Wi-Fi and Bluetooth Coexistence in ESP32
Favicon
Building a DIY Smart Home Device: Get S**t Done
Favicon
Top ESP32 Projects To Start in 2024
Favicon
Why You Should Choose MicroPython for Prototyping and Research Work
Favicon
How to build a smart home system with ESP32
Favicon
Advanced ESP32: Exploring Key Features and Versatile Uses
Favicon
Developing IoT Application with ESP32
Favicon
Simple Arduino Framework Photo Frame Implementation with Photos Downloaded from the Internet via DumbDisplay
Favicon
DHT22 with MicroPython on ESP32
Favicon
MicroPython ESP32: Blink LED
Favicon
Using an OLED Display with MicroPython on ESP32
Favicon
Interfacing HC-SR04 Ultrasonic Sensor with ESP32 Using MicroPython
Favicon
Configurar Arduino IDE para ESP32 en Windows 10 πŸ’»
Favicon
Arduino IDE 2 δΈŠε‚³ζͺ”ζ‘ˆεˆ° ESP32/ESP8266 ηš„ε€–ζŽ›
Favicon
ESP32 WiFiManager MQTT ArduinoJson 7
Favicon
How to Code with Lua on ESP32 with XEdge32
Favicon
Tiny E-Ink Picture Display
Favicon
Esp32 Rust Board on Macos M-chip in Docker
Favicon
ESP32 WiFiManager MQTT Spiffs config ArduinoJson 7

Featured ones: