# ESP32 LED Control Program with Push Buttons
# MicroPython Implementation with Detailed Comments
# Import required libraries
from machine import Pin # For GPIO pin control
import time # For time delays
# ========== HARDWARE CONFIGURATION ==========
# Define GPIO pins connected to LEDs (adjust to your wiring)
# Note: ESP32 GPIO numbers may differ from physical pin numbers
LED = [14, 15, 16, 17] # Array of 4 LED pins (GPIO14 to GPIO17)
# Define GPIO pins connected to push buttons
# Buttons should be wired between these pins and GND (active-low)
SW = [18, 19, 21] # Array of 3 switch pins (GPIO18, GPIO19, GPIO21)
# ========== GPIO INITIALIZATION ==========
# Initialize all LED pins as outputs using list comprehension
# Creates a list of Pin objects in output mode
led_pins = [Pin(pin, Pin.OUT) for pin in LED]
# Initialize all switch pins as inputs with internal pull-up resistors
# This means:
# - Button not pressed: pin reads HIGH (1)
# - Button pressed: pin reads LOW (0) because connected to GND
sw_pins = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in SW]
# Initialize all LEDs to OFF state (LOW)
for led in led_pins:
led.value(0) # Set to LOW voltage (0 = OFF)
# ========== LED EFFECT FUNCTIONS ==========
def blink_all(delay_ms=1000):
"""
Blink all LEDs simultaneously
Args:
delay_ms: Duration in milliseconds for both ON and OFF states
"""
# Turn ON all LEDs
for led in led_pins:
led.value(1) # Set HIGH (1 = ON)
time.sleep_ms(delay_ms) # Keep ON for specified time
# Turn OFF all LEDs
for led in led_pins:
led.value(0)
time.sleep_ms(delay_ms) # Keep OFF for specified time
def run_right_to_left(delay_ms=2000):
"""
Light LEDs sequentially from first to last (right to left)
Args:
delay_ms: Time each LED stays ON in milliseconds
"""
for led in led_pins: # Loop in order: LED[0], LED[1], etc.
led.value(1) # Turn ON current LED
time.sleep_ms(delay_ms) # Hold ON state
led.value(0) # Turn OFF before moving to next
def run_left_to_right(delay_ms=4000):
"""
Light LEDs sequentially from last to first (left to right)
Args:
delay_ms: Time each LED stays ON in milliseconds
"""
for led in reversed(led_pins): # Loop in reverse order
led.value(1)
time.sleep_ms(delay_ms)
led.value(0)
# ========== MAIN PROGRAM LOOP ==========
try:
print("Program started. Press buttons to control LEDs.")
while True: # Run forever until KeyboardInterrupt (Ctrl+C)
time.sleep_ms(10) # Small delay to reduce CPU usage
# ----- Button 0 (SW0) Pressed -----
if sw_pins[0].value() == 0: # Active-low check
print("Button 0: All LEDs blinking (1s interval)")
while sw_pins[0].value() == 0: # While button is held
blink_all(1000) # Blink with 1000ms (1s) delay
# ----- Button 1 (SW1) Pressed -----
elif sw_pins[1].value() == 0:
print("Button 1: Left-to-right chase (2s delay)")
while sw_pins[1].value() == 0:
run_right_to_left(2000) # 2000ms (2s) per LED
# ----- Button 2 (SW2) Pressed -----
elif sw_pins[2].value() == 0:
print("Button 2: Right-to-left chase (4s delay)")
while sw_pins[2].value() == 0:
run_left_to_right(4000) # 4000ms (4s) per LED
# ----- No Buttons Pressed -----
else:
# Ensure all LEDs are OFF when no buttons pressed
for led in led_pins:
led.value(0)
time.sleep_ms(10) # Small delay to prevent busy-waiting
# ========== CLEANUP ON EXIT ==========
except KeyboardInterrupt:
# Turn OFF all LEDs when program is stopped (Ctrl+C)
for led in led_pins:
led.value(0)
print("\nProgram stopped. All LEDs turned OFF.")