Using Time Module to Delay LED Blink in MicroPython for ESP32 and ESP8266

Have you ever wanted to control the blink rate of an LED in your MicroPython projects? In this guide, we’ll show you how to use the time module to create delays, allowing you to control the Using Time Module to Delay LED Blink in MicroPython for ESP32 and ESP82666. The time module is essential for creating time-based actions like LED blinking. This project is perfect for beginners looking to understand how to introduce delays in their code.

What is the Time Module?

The time module in MicroPython provides functions for working with time, such as adding delays. The time.sleep() function is used to pause the execution of code for a specified number of seconds, which can be used to create regular blinking intervals for an LED.

Purpose of Using Time Module in This Project

In this project, we aim to:

  • Blink an LED at regular intervals using the time module.
  • Understand how to control the timing of hardware components using code.
  • Learn how to use the time.sleep() function to introduce delays.

MicroPython Syntax for Using Time Module:

Function Syntax Example
Import the time module import time import time
Set delay between actions time.sleep(seconds) time.sleep(1) (Waits 1 second)
Initialize an LED led = machine.Pin(pin, machine.Pin.OUT) led = machine.Pin(5, machine.Pin.OUT)
Set LED state led.value(state) led.value(1) (Turn LED on)

By using the time module, you can control how long the LED stays on or off.

Components Needed:

  • ESP32 or ESP8266
  • 1 LED
  • 1 Resistor (220Ω)
  • Breadboard
  • Jumper Wires

Circuit Diagram and Connection Table:

Circuit Diagram:

               LED          ESP32/ESP8266

               —–          ————

               Anode  ——> GPIO5 (Pin 5)

               Cathode ——> GND

 

Circuit Connection Table:

Component Connection ESP32/ESP8266 Pin Explanation
LED Anode Connect to GPIO5 GPIO5 on ESP32/ESP8266 Controls the LED ON/OFF state
LED Cathode Connect to GND GND on ESP32/ESP8266 Completes the LED circuit

Warnings:

  • Make sure the LED has a 220Ω resistor to protect it from excessive current.
  • Double-check connections before powering the circuit to avoid damaging components.

Using the Time Module in MicroPython

MicroPython Syntax for Time Module:

  • import time: Imports the time module to use time-based functions like sleep.
  • time.sleep(seconds): Pauses the program for a given number of seconds. For example, time.sleep(1) introduces a 1-second delay.

MicroPython Code to Blink LED at Regular Intervals

import machine

import time

 

# Initialize the LED (connected to GPIO5)

led = machine.Pin(5, machine.Pin.OUT)

 

# Set the blink interval in seconds

blink_interval = 1  # LED will blink every 1 second

 

# Main loop to control LED blinking

while True:

    # Turn LED ON

    led.value(1)

    print(“LED is ON”)

    

    # Wait for the interval

    time.sleep(blink_interval)

    

    # Turn LED OFF

    led.value(0)

    print(“LED is OFF”)

    

    # Wait for the interval again

    time.sleep(blink_interval)

 

Explanation of the Code:

  1. Import the time module: We import the time module to use the time.sleep() function for introducing delays.
  2. Initialize the LED: We set up the LED to GPIO5 on the ESP32 or ESP8266, configuring it as an output device.
  3. Set the Blink Interval: We define a blink_interval of 1 second, meaning the LED will turn on and off every 1 second.
  4. Main Loop: The code continuously turns the LED on, waits for the set interval, turns the LED off, and waits again, creating a blinking effect.

Running the Code and Observing the Output

  1. Upload the code to your ESP32/ESP8266 using Thonny or another MicroPython IDE.
  2. Observe the LED blinking: The LED will turn on for 1 second, then off for 1 second, repeatedly.

Check the console output: The serial monitor will display:
LED is ON

LED is OFF

Expanding the Project:

  • Change the Blink Interval: Modify the blink_interval to make the LED blink faster or slower.
  • Multiple LEDs: Add more LEDs and control them with different intervals using the time module.
  • User Input for Blink Speed: Allow users to input the blink interval and adjust the LED blink speed dynamically.

Common Problems and Solutions:

  1. Problem: The LED does not blink.
    • Solution: Check the wiring, especially the connections to GPIO5 and the ground pin. Ensure the 220Ω resistor is in place.
  2. Problem: The LED blinks too fast or too slow.
    • Solution: Adjust the blink_interval to a suitable value. You can experiment with values like 0.5 seconds for a faster blink.

FAQ:

Q: Can I change the blink speed while the program is running?
A: You can add user input functionality to change the blink_interval dynamically during execution, or use buttons to control the speed.

Q: What if I want to blink multiple LEDs at different intervals?
A: You can add more LEDs to different GPIO pins and use separate time.sleep() functions to control each LED independently.

Q: How do I stop the LED from blinking?
A: You can use a button to break the loop or stop the script manually from your MicroPython IDE.

Conclusion:

In this project, you learned how to control the blinking of an LED using the time module in MicroPython for ESP32 and ESP8266. By using time.sleep(), you introduced regular delays to create a blinking effect. This project helps you understand how to control hardware timing in MicroPython and is an excellent foundation for more complex time-based projects.